Skip to content

Commit 2d3126b

Browse files
authored
Config: Implement missing MarshalJSON for structs having custom UnmarshalJSON (#4585)
* conf: implement MarshalJSON for FakeDNSConfig * conf: Rewrite MarshalJSON for PortList decouple PortRange from PortList. * conf: implement MarshalJSON for HostAddress * conf: Add MarshalJSON comments and use pointers.
1 parent 0dbab7b commit 2d3126b

File tree

4 files changed

+63
-11
lines changed

4 files changed

+63
-11
lines changed

infra/conf/cfgcommon/duration/duration.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ import (
88

99
type Duration int64
1010

11+
// MarshalJSON implements encoding/json.Marshaler.MarshalJSON
1112
func (d *Duration) MarshalJSON() ([]byte, error) {
1213
dr := time.Duration(*d)
1314
return json.Marshal(dr.String())
1415
}
1516

17+
// UnmarshalJSON implements encoding/json.Unmarshaler.UnmarshalJSON
1618
func (d *Duration) UnmarshalJSON(b []byte) error {
1719
var v interface{}
1820
if err := json.Unmarshal(b, &v); err != nil {

infra/conf/common.go

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ func (v StringList) Len() int {
2323
return len(v)
2424
}
2525

26+
// UnmarshalJSON implements encoding/json.Unmarshaler.UnmarshalJSON
2627
func (v *StringList) UnmarshalJSON(data []byte) error {
2728
var strarray []string
2829
if err := json.Unmarshal(data, &strarray); err == nil {
@@ -43,10 +44,12 @@ type Address struct {
4344
net.Address
4445
}
4546

46-
func (v Address) MarshalJSON() ([]byte, error) {
47+
// MarshalJSON implements encoding/json.Marshaler.MarshalJSON
48+
func (v *Address) MarshalJSON() ([]byte, error) {
4749
return json.Marshal(v.Address.String())
4850
}
4951

52+
// UnmarshalJSON implements encoding/json.Unmarshaler.UnmarshalJSON
5053
func (v *Address) UnmarshalJSON(data []byte) error {
5154
var rawStr string
5255
if err := json.Unmarshal(data, &rawStr); err != nil {
@@ -81,6 +84,7 @@ func (v Network) Build() net.Network {
8184

8285
type NetworkList []Network
8386

87+
// UnmarshalJSON implements encoding/json.Unmarshaler.UnmarshalJSON
8488
func (v *NetworkList) UnmarshalJSON(data []byte) error {
8589
var strarray []Network
8690
if err := json.Unmarshal(data, &strarray); err == nil {
@@ -169,6 +173,19 @@ func (v *PortRange) Build() *net.PortRange {
169173
}
170174
}
171175

176+
// MarshalJSON implements encoding/json.Marshaler.MarshalJSON
177+
func (v *PortRange) MarshalJSON() ([]byte, error) {
178+
return json.Marshal(v.String())
179+
}
180+
181+
func (port *PortRange) String() string {
182+
if port.From == port.To {
183+
return strconv.Itoa(int(port.From))
184+
} else {
185+
return fmt.Sprintf("%d-%d", port.From, port.To)
186+
}
187+
}
188+
172189
// UnmarshalJSON implements encoding/json.Unmarshaler.UnmarshalJSON
173190
func (v *PortRange) UnmarshalJSON(data []byte) error {
174191
port, err := parseIntPort(data)
@@ -203,20 +220,21 @@ func (list *PortList) Build() *net.PortList {
203220
return portList
204221
}
205222

206-
func (v PortList) MarshalJSON() ([]byte, error) {
207-
return json.Marshal(v.String())
223+
// MarshalJSON implements encoding/json.Marshaler.MarshalJSON
224+
func (v *PortList) MarshalJSON() ([]byte, error) {
225+
portStr := v.String()
226+
port, err := strconv.Atoi(portStr)
227+
if err == nil {
228+
return json.Marshal(port)
229+
} else {
230+
return json.Marshal(portStr)
231+
}
208232
}
209233

210234
func (v PortList) String() string {
211235
ports := []string{}
212236
for _, port := range v.Range {
213-
if port.From == port.To {
214-
p := strconv.Itoa(int(port.From))
215-
ports = append(ports, p)
216-
} else {
217-
p := fmt.Sprintf("%d-%d", port.From, port.To)
218-
ports = append(ports, p)
219-
}
237+
ports = append(ports, port.String())
220238
}
221239
return strings.Join(ports, ",")
222240
}
@@ -277,7 +295,8 @@ type Int32Range struct {
277295
To int32
278296
}
279297

280-
func (v Int32Range) MarshalJSON() ([]byte, error) {
298+
// MarshalJSON implements encoding/json.Marshaler.MarshalJSON
299+
func (v *Int32Range) MarshalJSON() ([]byte, error) {
281300
return json.Marshal(v.String())
282301
}
283302

@@ -289,6 +308,7 @@ func (v Int32Range) String() string {
289308
}
290309
}
291310

311+
// UnmarshalJSON implements encoding/json.Unmarshaler.UnmarshalJSON
292312
func (v *Int32Range) UnmarshalJSON(data []byte) error {
293313
defer v.ensureOrder()
294314
var str string

infra/conf/dns.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type NameServerConfig struct {
2525
TimeoutMs uint64 `json:"timeoutMs"`
2626
}
2727

28+
// UnmarshalJSON implements encoding/json.Unmarshaler.UnmarshalJSON
2829
func (c *NameServerConfig) UnmarshalJSON(data []byte) error {
2930
var address Address
3031
if err := json.Unmarshal(data, &address); err == nil {
@@ -163,6 +164,18 @@ type HostAddress struct {
163164
addrs []*Address
164165
}
165166

167+
// MarshalJSON implements encoding/json.Marshaler.MarshalJSON
168+
func (h *HostAddress) MarshalJSON() ([]byte, error) {
169+
if (h.addr != nil) != (h.addrs != nil) {
170+
if h.addr != nil {
171+
return json.Marshal(h.addr)
172+
} else if h.addrs != nil {
173+
return json.Marshal(h.addrs)
174+
}
175+
}
176+
return nil, errors.New("unexpected config state")
177+
}
178+
166179
// UnmarshalJSON implements encoding/json.Unmarshaler.UnmarshalJSON
167180
func (h *HostAddress) UnmarshalJSON(data []byte) error {
168181
addr := new(Address)
@@ -208,6 +221,11 @@ func getHostMapping(ha *HostAddress) *dns.Config_HostMapping {
208221
}
209222
}
210223

224+
// MarshalJSON implements encoding/json.Marshaler.MarshalJSON
225+
func (m *HostsWrapper) MarshalJSON() ([]byte, error) {
226+
return json.Marshal(m.Hosts)
227+
}
228+
211229
// UnmarshalJSON implements encoding/json.Unmarshaler.UnmarshalJSON
212230
func (m *HostsWrapper) UnmarshalJSON(data []byte) error {
213231
hosts := make(map[string]*HostAddress)

infra/conf/fakedns.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ type FakeDNSConfig struct {
2020
pools []*FakeDNSPoolElementConfig
2121
}
2222

23+
// MarshalJSON implements encoding/json.Marshaler.MarshalJSON
24+
func (f *FakeDNSConfig) MarshalJSON() ([]byte, error) {
25+
if (f.pool != nil) != (f.pools != nil) {
26+
if f.pool != nil {
27+
return json.Marshal(f.pool)
28+
} else if f.pools != nil {
29+
return json.Marshal(f.pools)
30+
}
31+
}
32+
return nil, errors.New("unexpected config state")
33+
}
34+
2335
// UnmarshalJSON implements encoding/json.Unmarshaler.UnmarshalJSON
2436
func (f *FakeDNSConfig) UnmarshalJSON(data []byte) error {
2537
var pool FakeDNSPoolElementConfig

0 commit comments

Comments
 (0)