forked from simonnilsson/ask
/
ask.go
260 lines (240 loc) · 6.88 KB
/
ask.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// Package ask provides a simple way of accessing nested properties in maps and arrays.
// Works great in combination with encoding/json and other packages that "Unmarshal" arbitrary data into Go data-types.
// Inspired by the get function in the lodash javascript library.
package ask
import (
"math"
"reflect"
"regexp"
"strconv"
"strings"
)
var tokenMatcher = regexp.MustCompile("([^[]+)?(?:\\[(\\d+)])?")
var mapType = reflect.TypeOf(map[string]interface{}{})
var sliceType = reflect.TypeOf([]interface{}{})
var splitCache = make(map[string][]string)
// Answer holds result of call to For, use one of its methods to extract a value.
type Answer struct {
value interface{}
}
// For is used to select a path from source to return as answer.
func For(source interface{}, path string) *Answer {
parts, ok := splitCache[path]
if !ok {
parts = strings.Split(path, ".")
splitCache[path] = parts
}
current := source
for _, part := range parts {
match := tokenMatcher.FindStringSubmatch(strings.TrimSpace(part))
if len(match) == 3 {
if match[1] != "" {
current = accessMap(current, match[1])
if current == nil {
return &Answer{}
}
}
if match[2] != "" {
current = accessSlice(current, match[2])
if current == nil {
return &Answer{}
}
}
}
}
return &Answer{value: current}
}
func accessMap(source interface{}, key string) interface{} {
val := reflect.ValueOf(source)
if val.IsValid() && val.Type().ConvertibleTo(mapType) {
return val.Convert(mapType).Interface().(map[string]interface{})[key]
}
return nil
}
func accessSlice(source interface{}, indexStr string) interface{} {
val := reflect.ValueOf(source)
if val.IsValid() && val.Type().ConvertibleTo(sliceType) {
s := val.Convert(sliceType).Interface().([]interface{})
index, _ := strconv.Atoi(indexStr)
if index >= 0 && index < len(s) {
return s[index]
}
}
return nil
}
// Path does the same thing as For but uses existing answer as source.
func (a *Answer) Path(path string) *Answer {
return For(a.value, path)
}
// Exists returns a boolean indicating if the answer exists (not nil).
func (a *Answer) Exists() bool {
return a.value != nil
}
// Value returns the raw value as type interface{}, can be nil if no value is available.
func (a *Answer) Value() interface{} {
return a.value
}
// Slice attempts asserting answer as a []interface{}.
// The first return value is the result, and the second indicates if the operation was successful.
// If not successful the first return value will be set to the d parameter.
func (a *Answer) Slice(d []interface{}) ([]interface{}, bool) {
val := reflect.ValueOf(a.value)
if val.IsValid() && val.CanConvert(sliceType) {
return val.Convert(sliceType).Interface().([]interface{}), true
}
return d, false
}
// Map attempts asserting answer as a map[string]interface{}.
// The first return value is the result, and the second indicates if the operation was successful.
// If not successful the first return value will be set to the d parameter.
func (a *Answer) Map(d map[string]interface{}) (map[string]interface{}, bool) {
val := reflect.ValueOf(a.value)
if val.IsValid() && val.CanConvert(mapType) {
return val.Convert(mapType).Interface().(map[string]interface{}), true
}
return d, false
}
// String attempts asserting answer as a string.
// The first return value is the result, and the second indicates if the operation was successful.
// If not successful the first return value will be set to the d parameter.
func (a *Answer) String(d string) (string, bool) {
res, ok := a.value.(string)
if ok {
return res, ok
}
return d, false
}
// Bool attempts asserting answer as a bool.
// The first return value is the result, and the second indicates if the operation was successful.
// If not successful the first return value will be set to the d parameter.
func (a *Answer) Bool(d bool) (bool, bool) {
res, ok := a.value.(bool)
if ok {
return res, ok
}
return d, false
}
// Int attempts asserting answer as a int64. Casting from other number types will be done if necessary.
// The first return value is the result, and the second indicates if the operation was successful.
// If not successful the first return value will be set to the d parameter.
func (a *Answer) Int(d int64) (int64, bool) {
switch vt := a.value.(type) {
case int:
return int64(vt), true
case int8:
return int64(vt), true
case int16:
return int64(vt), true
case int32:
return int64(vt), true
case int64:
return vt, true
case uint:
if vt <= uint(math.MaxInt64) {
return int64(vt), true
}
case uint8:
return int64(vt), true
case uint16:
return int64(vt), true
case uint32:
return int64(vt), true
case uint64:
if vt <= uint64(math.MaxInt64) {
return int64(vt), true
}
case float32:
if vt >= float32(math.MinInt64) && vt <= float32(math.MaxInt64) {
return int64(vt), true
}
case float64:
if vt >= float64(math.MinInt64) && vt <= float64(math.MaxInt64) {
return int64(vt), true
}
}
return d, false
}
// Uint attempts asserting answer as a uint64. Casting from other number types will be done if necessary.
// The first return value is the result, and the second indicates if the operation was successful.
// If not successful the first return value will be set to the d parameter.
func (a *Answer) Uint(d uint64) (uint64, bool) {
switch vt := a.value.(type) {
case int:
if vt >= 0 {
return uint64(vt), true
}
case int8:
if vt >= 0 {
return uint64(vt), true
}
case int16:
if vt >= 0 {
return uint64(vt), true
}
case int32:
if vt >= 0 {
return uint64(vt), true
}
case int64:
if vt >= 0 {
return uint64(vt), true
}
case uint:
return uint64(vt), true
case uint8:
return uint64(vt), true
case uint16:
return uint64(vt), true
case uint32:
return uint64(vt), true
case uint64:
return vt, true
case float32:
if vt >= 0 {
if vt > float32(math.MaxUint64) {
return math.MaxUint64, true
}
return uint64(vt), true
}
case float64:
if vt >= 0 {
if vt > float64(math.MaxUint64) {
return math.MaxUint64, true
}
return uint64(vt), true
}
}
return d, false
}
// Float attempts asserting answer as a float64. Casting from other number types will be done if necessary.
// The first return value is the result, and the second indicates if the operation was successful.
// If not successful the first return value will be set to the d parameter.
func (a *Answer) Float(d float64) (float64, bool) {
switch vt := a.value.(type) {
case int:
return float64(vt), true
case int8:
return float64(vt), true
case int16:
return float64(vt), true
case int32:
return float64(vt), true
case int64:
return float64(vt), true
case uint:
return float64(vt), true
case uint8:
return float64(vt), true
case uint16:
return float64(vt), true
case uint32:
return float64(vt), true
case uint64:
return float64(vt), true
case float32:
return float64(vt), true
case float64:
return vt, true
}
return d, false
}