Skip to content

Commit 98fa059

Browse files
authored
chore(upcloud): move IPAddressSlice type into ip_address.go (#243)
1 parent 8ad0064 commit 98fa059

2 files changed

Lines changed: 23 additions & 23 deletions

File tree

upcloud/ip_address.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,29 @@ func (s *IPAddresses) UnmarshalJSON(b []byte) error {
4040
return nil
4141
}
4242

43+
// IPAddressSlice is a slice of IPAddress.
44+
// It exists to allow for a custom JSON unmarshaller.
45+
type IPAddressSlice []IPAddress
46+
47+
// UnmarshalJSON is a custom unmarshaller that deals with
48+
// deeply embedded values.
49+
func (i *IPAddressSlice) UnmarshalJSON(b []byte) error {
50+
type localIPAddress IPAddress
51+
v := struct {
52+
IPAddresses []localIPAddress `json:"ip_address"`
53+
}{}
54+
err := json.Unmarshal(b, &v)
55+
if err != nil {
56+
return err
57+
}
58+
59+
for _, ip := range v.IPAddresses {
60+
(*i) = append((*i), IPAddress(ip))
61+
}
62+
63+
return nil
64+
}
65+
4366
// IPAddress represents an IP address
4467
type IPAddress struct {
4568
Access string `json:"access"`

upcloud/server.go

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -116,29 +116,6 @@ type Server struct {
116116
Zone string `json:"zone"`
117117
}
118118

119-
// IPAddressSlice is a slice of IPAddress.
120-
// It exists to allow for a custom JSON unmarshaller.
121-
type IPAddressSlice []IPAddress
122-
123-
// UnmarshalJSON is a custom unmarshaller that deals with
124-
// deeply embedded values.
125-
func (i *IPAddressSlice) UnmarshalJSON(b []byte) error {
126-
type localIPAddress IPAddress
127-
v := struct {
128-
IPAddresses []localIPAddress `json:"ip_address"`
129-
}{}
130-
err := json.Unmarshal(b, &v)
131-
if err != nil {
132-
return err
133-
}
134-
135-
for _, ip := range v.IPAddresses {
136-
(*i) = append((*i), IPAddress(ip))
137-
}
138-
139-
return nil
140-
}
141-
142119
// ServerStorageDeviceSlice is a slice of ServerStorageDevices.
143120
// It exists to allow for a custom JSON unmarshaller.
144121
type ServerStorageDeviceSlice []ServerStorageDevice

0 commit comments

Comments
 (0)