-
Notifications
You must be signed in to change notification settings - Fork 318
/
types.go
155 lines (140 loc) · 3.25 KB
/
types.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package types
import (
"encoding/json"
"strconv"
"github.com/pkg/errors"
)
var (
// ErrUnsupportedType is returned if the type is not implemented
ErrUnsupportedType = errors.New("unsupported type")
)
// StrIntArray string array to be used on JSON UnmarshalJSON
type StrIntArray []string
// UnmarshalJSON convert JSON object array of string or
// a string format strings to a golang string array
func (sa *StrIntArray) UnmarshalJSON(data []byte) error {
var jsonObj interface{}
err := json.Unmarshal(data, &jsonObj)
if err != nil {
return errors.Wrap(err, "unmarshal str int array")
}
switch obj := jsonObj.(type) {
case string:
*sa = StrIntArray([]string{obj})
return nil
case int:
*sa = StrIntArray([]string{strconv.Itoa(obj)})
return nil
case []interface{}:
s := make([]string, 0, len(obj))
for _, v := range obj {
switch value := v.(type) {
case string:
s = append(s, value)
case int:
s = append(s, strconv.Itoa(value))
case float64:
s = append(s, strconv.Itoa(int(value)))
default:
return ErrUnsupportedType
}
}
*sa = StrIntArray(s)
return nil
}
return ErrUnsupportedType
}
// StrArray string array to be used on JSON UnmarshalJSON
type StrArray []string
// UnmarshalJSON convert JSON object array of string or
// a string format strings to a golang string array
func (sa *StrArray) UnmarshalJSON(data []byte) error {
var jsonObj interface{}
err := json.Unmarshal(data, &jsonObj)
if err != nil {
return err
}
switch obj := jsonObj.(type) {
case string:
*sa = StrArray([]string{obj})
return nil
case []interface{}:
s := make([]string, 0, len(obj))
for _, v := range obj {
value, ok := v.(string)
if !ok {
return ErrUnsupportedType
}
s = append(s, value)
}
*sa = StrArray(s)
return nil
}
return ErrUnsupportedType
}
type LifecycleHook map[string][]string
func (l *LifecycleHook) UnmarshalJSON(data []byte) error {
*l = make(map[string][]string)
var jsonObj interface{}
err := json.Unmarshal(data, &jsonObj)
if err != nil {
return err
}
switch obj := jsonObj.(type) {
case string:
// Anonymous string command
(*l)[""] = []string{obj}
return nil
case []interface{}:
// Anonymous array of strings command
cmd := make([]string, 0)
for _, v := range obj {
value, ok := v.(string)
if !ok {
return ErrUnsupportedType
}
cmd = append(cmd, value)
}
(*l)[""] = cmd
return nil
case map[string]interface{}:
for k, v := range obj {
value, ok := v.(string)
if ok {
// Named string command
(*l)[k] = []string{value}
} else {
// Named array of strings command
stringArrayValue, ok := v.([]interface{})
if !ok {
return ErrUnsupportedType
}
cmd := make([]string, 0)
for _, v := range stringArrayValue {
cmd = append(cmd, v.(string))
}
(*l)[k] = cmd
}
}
return nil
}
return ErrUnsupportedType
}
type StrBool string
// UnmarshalJSON parses fields that may be numbers or booleans.
func (f *StrBool) UnmarshalJSON(data []byte) error {
var jsonObj interface{}
err := json.Unmarshal(data, &jsonObj)
if err != nil {
return err
}
switch obj := jsonObj.(type) {
case string:
*f = StrBool(obj)
return nil
case bool:
*f = StrBool(strconv.FormatBool(obj))
return nil
}
return ErrUnsupportedType
}