diff options
author | Slack Coder <slackcoder@server.ky> | 2024-04-08 15:29:11 -0500 |
---|---|---|
committer | Slack Coder <slackcoder@server.ky> | 2024-07-18 11:47:49 -0500 |
commit | ae748859be8d6d3ed3c0929770f0c287ab6d6460 (patch) | |
tree | 284d3bf3d7fa496a9d334391eac996affc5a01b9 /internal/service/service_json.go | |
parent | c2267767ca8ed06018d26a45b483c44b7c4234cf (diff) | |
download | mirror-0.0.1-dev.tar.xz (sig) |
Port to Golangv0.0.1-dev
Diffstat (limited to 'internal/service/service_json.go')
-rw-r--r-- | internal/service/service_json.go | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/internal/service/service_json.go b/internal/service/service_json.go new file mode 100644 index 0000000..571eb30 --- /dev/null +++ b/internal/service/service_json.go @@ -0,0 +1,64 @@ +package service + +import ( + "encoding/json" + "net/url" +) + +type JsonURL struct { + *url.URL +} + +func (m JsonURL) MarshalJSON() ([]byte, error) { + str := m.String() + return json.Marshal(str) +} + +func (m *JsonURL) UnmarshalJSON(buf []byte) error { + var str string + + err := json.Unmarshal(buf, &str) + if err != nil { + return err + } + + m.URL, err = url.Parse(str) + return err +} + +func (m Mirror) MarshalJSON() ([]byte, error) { + type Alias Mirror + + m2 := struct { + *Alias + From JsonURL `json:"from"` + To JsonURL `json:"to"` + }{ + Alias: (*Alias)(&m), + From: JsonURL{m.From}, + To: JsonURL{m.To}, + } + + return json.Marshal(m2) +} + +func (m *Mirror) UnmarshalJSON(buf []byte) error { + type Alias Mirror + + var m2 struct { + *Alias + From JsonURL `json:"from"` + To JsonURL `json:"to"` + } + + m2.Alias = (*Alias)(m) + err := json.Unmarshal(buf, &m2) + if err != nil { + return err + } + + m.From = m2.From.URL + m.To = m2.To.URL + + return nil +} |