aboutsummaryrefslogtreecommitdiffsponsor
path: root/internal/service/service_json.go
blob: 571eb3019ecd67d9e4e6568e7e6dfe6393b675e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
}