-
Notifications
You must be signed in to change notification settings - Fork 76
/
jsonq.go
304 lines (277 loc) · 8.12 KB
/
jsonq.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package jsonq
import (
"fmt"
"strconv"
)
// JsonQuery is an object that enables querying of a Go map with a simple
// positional query language.
type JsonQuery struct {
blob map[string]interface{}
}
// stringFromInterface converts an interface{} to a string and returns an error if types don't match.
func stringFromInterface(val interface{}) (string, error) {
switch val.(type) {
case string:
return val.(string), nil
}
return "", fmt.Errorf("Expected string value for String, got \"%v\"\n", val)
}
// boolFromInterface converts an interface{} to a bool and returns an error if types don't match.
func boolFromInterface(val interface{}) (bool, error) {
switch val.(type) {
case bool:
return val.(bool), nil
}
return false, fmt.Errorf("Expected boolean value for Bool, got \"%v\"\n", val)
}
// floatFromInterface converts an interface{} to a float64 and returns an error if types don't match.
func floatFromInterface(val interface{}) (float64, error) {
switch val.(type) {
case float64:
return val.(float64), nil
case int:
return float64(val.(int)), nil
case string:
fval, err := strconv.ParseFloat(val.(string), 64)
if err == nil {
return fval, nil
}
}
return 0.0, fmt.Errorf("Expected numeric value for Float, got \"%v\"\n", val)
}
// intFromInterface converts an interface{} to an int and returns an error if types don't match.
func intFromInterface(val interface{}) (int, error) {
switch val.(type) {
case float64:
return int(val.(float64)), nil
case string:
ival, err := strconv.ParseFloat(val.(string), 64)
if err == nil {
return int(ival), nil
}
case int:
return val.(int), nil
}
return 0, fmt.Errorf("Expected numeric value for Int, got \"%v\"\n", val)
}
// objectFromInterface converts an interface{} to a map[string]interface{} and returns an error if types don't match.
func objectFromInterface(val interface{}) (map[string]interface{}, error) {
switch val.(type) {
case map[string]interface{}:
return val.(map[string]interface{}), nil
}
return map[string]interface{}{}, fmt.Errorf("Expected json object for Object, got \"%v\"\n", val)
}
// arrayFromInterface converts an interface{} to an []interface{} and returns an error if types don't match.
func arrayFromInterface(val interface{}) ([]interface{}, error) {
switch val.(type) {
case []interface{}:
return val.([]interface{}), nil
}
return []interface{}{}, fmt.Errorf("Expected json array for Array, got \"%v\"\n", val)
}
// NewQuery creates a new JsonQuery obj from an interface{}.
func NewQuery(data interface{}) *JsonQuery {
j := new(JsonQuery)
j.blob = data.(map[string]interface{})
return j
}
// Bool extracts a bool the JsonQuery
func (j *JsonQuery) Bool(s ...string) (bool, error) {
val, err := rquery(j.blob, s...)
if err != nil {
return false, err
}
return boolFromInterface(val)
}
// Float extracts a float from the JsonQuery
func (j *JsonQuery) Float(s ...string) (float64, error) {
val, err := rquery(j.blob, s...)
if err != nil {
return 0.0, err
}
return floatFromInterface(val)
}
// Int extracts an int from the JsonQuery
func (j *JsonQuery) Int(s ...string) (int, error) {
val, err := rquery(j.blob, s...)
if err != nil {
return 0, err
}
return intFromInterface(val)
}
// String extracts a string from the JsonQuery
func (j *JsonQuery) String(s ...string) (string, error) {
val, err := rquery(j.blob, s...)
if err != nil {
return "", err
}
return stringFromInterface(val)
}
// Object extracts a json object from the JsonQuery
func (j *JsonQuery) Object(s ...string) (map[string]interface{}, error) {
val, err := rquery(j.blob, s...)
if err != nil {
return map[string]interface{}{}, err
}
return objectFromInterface(val)
}
// Array extracts a []interface{} from the JsonQuery
func (j *JsonQuery) Array(s ...string) ([]interface{}, error) {
val, err := rquery(j.blob, s...)
if err != nil {
return []interface{}{}, err
}
return arrayFromInterface(val)
}
// Interface extracts an interface{} from the JsonQuery
func (j *JsonQuery) Interface(s ...string) (interface{}, error) {
val, err := rquery(j.blob, s...)
if err != nil {
return nil, err
}
return val, nil
}
// ArrayOfStrings extracts an array of strings from some json
func (j *JsonQuery) ArrayOfStrings(s ...string) ([]string, error) {
array, err := j.Array(s...)
if err != nil {
return []string{}, err
}
toReturn := make([]string, len(array))
for index, val := range array {
toReturn[index], err = stringFromInterface(val)
if err != nil {
return toReturn, err
}
}
return toReturn, nil
}
// ArrayOfInts extracts an array of ints from some json
func (j *JsonQuery) ArrayOfInts(s ...string) ([]int, error) {
array, err := j.Array(s...)
if err != nil {
return []int{}, err
}
toReturn := make([]int, len(array))
for index, val := range array {
toReturn[index], err = intFromInterface(val)
if err != nil {
return toReturn, err
}
}
return toReturn, nil
}
// ArrayOfFloats extracts an array of float64s from some json
func (j *JsonQuery) ArrayOfFloats(s ...string) ([]float64, error) {
array, err := j.Array(s...)
if err != nil {
return []float64{}, err
}
toReturn := make([]float64, len(array))
for index, val := range array {
toReturn[index], err = floatFromInterface(val)
if err != nil {
return toReturn, err
}
}
return toReturn, nil
}
// ArrayOfBools extracts an array of bools from some json
func (j *JsonQuery) ArrayOfBools(s ...string) ([]bool, error) {
array, err := j.Array(s...)
if err != nil {
return []bool{}, err
}
toReturn := make([]bool, len(array))
for index, val := range array {
toReturn[index], err = boolFromInterface(val)
if err != nil {
return toReturn, err
}
}
return toReturn, nil
}
// ArrayOfObjects extracts an array of map[string]interface{} (objects) from some json
func (j *JsonQuery) ArrayOfObjects(s ...string) ([]map[string]interface{}, error) {
array, err := j.Array(s...)
if err != nil {
return []map[string]interface{}{}, err
}
toReturn := make([]map[string]interface{}, len(array))
for index, val := range array {
toReturn[index], err = objectFromInterface(val)
if err != nil {
return toReturn, err
}
}
return toReturn, nil
}
// ArrayOfArrays extracts an array of []interface{} (arrays) from some json
func (j *JsonQuery) ArrayOfArrays(s ...string) ([][]interface{}, error) {
array, err := j.Array(s...)
if err != nil {
return [][]interface{}{}, err
}
toReturn := make([][]interface{}, len(array))
for index, val := range array {
toReturn[index], err = arrayFromInterface(val)
if err != nil {
return toReturn, err
}
}
return toReturn, nil
}
// Matrix2D is an alias for ArrayOfArrays
func (j *JsonQuery) Matrix2D(s ...string) ([][]interface{}, error) {
return j.ArrayOfArrays(s...)
}
// Recursively query a decoded json blob
func rquery(blob interface{}, s ...string) (interface{}, error) {
var (
val interface{}
err error
)
val = blob
for _, q := range s {
val, err = query(val, q)
if err != nil {
return nil, err
}
}
switch val.(type) {
case nil:
return nil, fmt.Errorf("Nil value found at %s\n", s[len(s)-1])
}
return val, nil
}
// query a json blob for a single field or index. If query is a string, then
// the blob is treated as a json object (map[string]interface{}). If query is
// an integer, the blob is treated as a json array ([]interface{}). Any kind
// of key or index error will result in a nil return value with an error set.
func query(blob interface{}, query string) (interface{}, error) {
index, err := strconv.Atoi(query)
// if it's an integer, then we treat the current interface as an array
if err == nil {
switch blob.(type) {
case []interface{}:
default:
return nil, fmt.Errorf("Array index on non-array %v\n", blob)
}
if len(blob.([]interface{})) > index {
return blob.([]interface{})[index], nil
}
return nil, fmt.Errorf("Array index %d on array %v out of bounds\n", index, blob)
}
// blob is likely an object, but verify first
switch blob.(type) {
case map[string]interface{}:
default:
return nil, fmt.Errorf("Object lookup \"%s\" on non-object %v\n", query, blob)
}
val, ok := blob.(map[string]interface{})[query]
if !ok {
return nil, fmt.Errorf("Object %v does not contain field %s\n", blob, query)
}
return val, nil
}