-
Notifications
You must be signed in to change notification settings - Fork 8
/
label.go
43 lines (35 loc) · 886 Bytes
/
label.go
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
package upcloud
import "encoding/json"
// Label represents a key-value pair label in a response.
type Label struct {
Key string `json:"key"`
Value string `json:"value"`
}
// LabelSlice is a slice of Labels
// It exists to allow for a custom unmarshaller.
type LabelSlice []Label
// UnmarshalJSON is a custom unmarshaller that deals with
// deeply embedded values.
func (ls *LabelSlice) UnmarshalJSON(b []byte) error {
wrapper := struct {
Labels []Label `json:"label"`
}{}
err := json.Unmarshal(b, &wrapper)
if err != nil {
return err
}
(*ls) = wrapper.Labels
return nil
}
// MarshalJSON is a custom marshaller that deals with
// deeply embedded values.
func (ls LabelSlice) MarshalJSON() ([]byte, error) {
wrapper := struct {
Labels []Label `json:"label"`
}{}
if ls == nil {
ls = make(LabelSlice, 0)
}
wrapper.Labels = ls
return json.Marshal(wrapper)
}