-
Notifications
You must be signed in to change notification settings - Fork 110
/
attribute_map.go
201 lines (172 loc) · 4.58 KB
/
attribute_map.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package config
import (
"github.com/pkg/errors"
)
// An AttributeMap is a convenience wrapper for pulling out
// typed information from a map.
type AttributeMap map[string]interface{}
// Has returns whether or not the given name is in the map.
func (am AttributeMap) Has(name string) bool {
_, has := am[name]
return has
}
// IntSlice attempts to return a slice of ints present in the map with
// the given name; returns an empty slice otherwise.
func (am AttributeMap) IntSlice(name string) []int {
if am == nil {
return []int{}
}
x := am[name]
if x == nil {
return []int{}
}
if slice, ok := x.([]interface{}); ok {
ints := make([]int, 0, len(slice))
for _, v := range slice {
if i, ok := v.(int); ok {
ints = append(ints, i)
} else if i, ok := v.(float64); ok && i == float64(int64(i)) {
ints = append(ints, int(i))
} else {
panic(errors.Errorf("values in (%s) need to be ints but got %T", name, v))
}
}
return ints
}
panic(errors.Errorf("wanted a []float64 for (%s) but got (%v) %T", name, x, x))
}
// Float64Slice attempts to return a slice of ints present in the map with
// the given name; returns an empty slice otherwise.
func (am AttributeMap) Float64Slice(name string) []float64 {
if am == nil {
return []float64{}
}
x := am[name]
if x == nil {
return []float64{}
}
if slice, ok := x.([]interface{}); ok {
float64s := make([]float64, 0, len(slice))
for _, v := range slice {
if i, ok := v.(float64); ok {
float64s = append(float64s, i)
} else {
panic(errors.Errorf("values in (%s) need to be float64 but got %T", name, v))
}
}
return float64s
}
panic(errors.Errorf("wanted a []int for (%s) but got (%v) %T", name, x, x))
}
// StringSlice attempts to return a slice of strings present in the map with
// the given name; returns an empty slice otherwise.
func (am AttributeMap) StringSlice(name string) []string {
if am == nil {
return []string{}
}
x := am[name]
if x == nil {
return []string{}
}
if slice, ok := x.([]interface{}); ok {
strings := make([]string, 0, len(slice))
for _, v := range slice {
if s, ok := v.(string); ok {
strings = append(strings, s)
} else {
panic(errors.Errorf("values in (%s) need to be strings but got %T", name, v))
}
}
return strings
}
if slice, ok := x.([]string); ok {
return slice
}
panic(errors.Errorf("wanted a []string for (%s) but got (%v) %T", name, x, x))
}
// String attempts to return a string present in the map with
// the given name; returns an empty string otherwise.
func (am AttributeMap) String(name string) string {
if am == nil {
return ""
}
x := am[name]
if x == nil {
return ""
}
if s, ok := x.(string); ok {
return s
}
panic(errors.Errorf("wanted a string for (%s) but got (%v) %T", name, x, x))
}
// Int attempts to return an integer present in the map with
// the given name; returns the given default otherwise.
func (am AttributeMap) Int(name string, def int) int {
if am == nil {
return def
}
x, has := am[name]
if !has {
return def
}
if v, ok := x.(int); ok {
return v
}
if v, ok := x.(float64); ok && v == float64(int64(v)) {
return int(v)
}
panic(errors.Errorf("wanted an int for (%s) but got (%v) %T", name, x, x))
}
// Float64 attempts to return a float64 present in the map with
// the given name; returns the given default otherwise.
func (am AttributeMap) Float64(name string, def float64) float64 {
if am == nil {
return def
}
x, has := am[name]
if !has {
return def
}
if v, ok := x.(float64); ok {
return v
}
panic(errors.Errorf("wanted a float for (%s) but got (%v) %T", name, x, x))
}
// Bool attempts to return a boolean present in the map with
// the given name; returns the given default otherwise.
func (am AttributeMap) Bool(name string, def bool) bool {
if am == nil {
return def
}
x, has := am[name]
if !has {
return def
}
if v, ok := x.(bool); ok {
return v
}
panic(errors.Errorf("wanted a bool for (%s) but got (%v) %T", name, x, x))
}
// BoolSlice attempts to return a slice of bools present in the map with
// the given name; returns an empty slice otherwise.
func (am AttributeMap) BoolSlice(name string, def bool) []bool {
if am == nil {
return []bool{}
}
x := am[name]
if x == nil {
return []bool{}
}
if slice, ok := x.([]interface{}); ok {
bools := make([]bool, 0, len(slice))
for _, v := range slice {
if b, ok := v.(bool); ok {
bools = append(bools, b)
} else {
panic(errors.Errorf("values in (%s) need to be bools but got %T", name, v))
}
}
return bools
}
panic(errors.Errorf("wanted a []bool for (%s) but got (%v) %T", name, x, x))
}