-
Notifications
You must be signed in to change notification settings - Fork 77
/
jsonpath.go
466 lines (409 loc) · 9.59 KB
/
jsonpath.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
package jsonpath
import (
"strconv"
"strings"
json "github.com/nspcc-dev/go-ordered-json"
)
type (
// pathTokenType represents a single JSONPath token.
pathTokenType byte
// pathParser combines a JSONPath and a position to start parsing from.
pathParser struct {
s string
i int
depth int
}
)
const (
pathInvalid pathTokenType = iota
pathRoot
pathDot
pathLeftBracket
pathRightBracket
pathAsterisk
pathComma
pathColon
pathIdentifier
pathString
pathNumber
)
const (
maxNestingDepth = 6
maxObjects = 1024
)
// Get returns substructures of value selected by path.
// The result is always non-nil unless the path is invalid.
func Get(path string, value interface{}) ([]interface{}, bool) {
if path == "" {
return []interface{}{value}, true
}
p := pathParser{
depth: maxNestingDepth,
s: path,
}
typ, _ := p.nextToken()
if typ != pathRoot {
return nil, false
}
objs := []interface{}{value}
for p.i < len(p.s) {
var ok bool
switch typ, _ := p.nextToken(); typ {
case pathDot:
objs, ok = p.processDot(objs)
case pathLeftBracket:
objs, ok = p.processLeftBracket(objs)
}
if !ok || maxObjects < len(objs) {
return nil, false
}
}
if objs == nil {
objs = []interface{}{}
}
return objs, true
}
func (p *pathParser) nextToken() (pathTokenType, string) {
var (
typ pathTokenType
value string
ok = true
numRead = 1
)
if p.i >= len(p.s) {
return pathInvalid, ""
}
switch c := p.s[p.i]; c {
case '$':
typ = pathRoot
case '.':
typ = pathDot
case '[':
typ = pathLeftBracket
case ']':
typ = pathRightBracket
case '*':
typ = pathAsterisk
case ',':
typ = pathComma
case ':':
typ = pathColon
case '\'':
typ = pathString
value, numRead, ok = p.parseString()
default:
switch {
case c == '_' || ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'):
typ = pathIdentifier
value, numRead, ok = p.parseIdent()
case c == '-' || ('0' <= c && c <= '9'):
typ = pathNumber
value, numRead, ok = p.parseNumber()
default:
return pathInvalid, ""
}
}
if !ok {
return pathInvalid, ""
}
p.i += numRead
return typ, value
}
// parseString parses a JSON string surrounded by single quotes.
// It returns the number of characters consumed and true on success.
func (p *pathParser) parseString() (string, int, bool) {
var end int
for end = p.i + 1; end < len(p.s); end++ {
if p.s[end] == '\'' {
return p.s[p.i : end+1], end + 1 - p.i, true
}
}
return "", 0, false
}
// parseIdent parses an alphanumeric identifier.
// It returns the number of characters consumed and true on success.
func (p *pathParser) parseIdent() (string, int, bool) {
var end int
for end = p.i + 1; end < len(p.s); end++ {
c := p.s[end]
if c != '_' && !('a' <= c && c <= 'z') &&
!('A' <= c && c <= 'Z') && !('0' <= c && c <= '9') {
break
}
}
return p.s[p.i:end], end - p.i, true
}
// parseNumber parses an integer number.
// Only string representation is returned, size-checking is done on the first use.
// It also returns the number of characters consumed and true on success.
func (p *pathParser) parseNumber() (string, int, bool) {
var end int
for end = p.i + 1; end < len(p.s); end++ {
c := p.s[end]
if c < '0' || '9' < c {
break
}
}
return p.s[p.i:end], end - p.i, true
}
// processDot handles `.` operator.
// It either descends 1 level down or performs recursive descent.
func (p *pathParser) processDot(objs []interface{}) ([]interface{}, bool) {
typ, value := p.nextToken()
switch typ {
case pathAsterisk:
return p.descend(objs)
case pathDot:
return p.descendRecursive(objs)
case pathIdentifier:
return p.descendByIdent(objs, value)
default:
return nil, false
}
}
// descend descends 1 level down.
// It flattens arrays and returns map values for maps.
func (p *pathParser) descend(objs []interface{}) ([]interface{}, bool) {
if p.depth <= 0 {
return nil, false
}
p.depth--
var values []interface{}
for i := range objs {
switch obj := objs[i].(type) {
case []interface{}:
if maxObjects < len(values)+len(obj) {
return nil, false
}
values = append(values, obj...)
case json.OrderedObject:
if maxObjects < len(values)+len(obj) {
return nil, false
}
for i := range obj {
values = append(values, obj[i].Value)
}
}
}
return values, true
}
// descendRecursive performs recursive descent.
func (p *pathParser) descendRecursive(objs []interface{}) ([]interface{}, bool) {
typ, val := p.nextToken()
if typ != pathIdentifier {
return nil, false
}
var values []interface{}
for len(objs) > 0 {
newObjs, _ := p.descendByIdentAux(objs, false, val)
if maxObjects < len(values)+len(newObjs) {
return nil, false
}
values = append(values, newObjs...)
objs, _ = p.descend(objs)
}
return values, true
}
// descendByIdent performs map's field access by name.
func (p *pathParser) descendByIdent(objs []interface{}, names ...string) ([]interface{}, bool) {
return p.descendByIdentAux(objs, true, names...)
}
func (p *pathParser) descendByIdentAux(objs []interface{}, checkDepth bool, names ...string) ([]interface{}, bool) {
if checkDepth {
if p.depth <= 0 {
return nil, false
}
p.depth--
}
var values []interface{}
for i := range objs {
obj, ok := objs[i].(json.OrderedObject)
if !ok {
continue
}
for j := range names {
for k := range obj {
if obj[k].Key == names[j] {
if maxObjects < len(values)+1 {
return nil, false
}
values = append(values, obj[k].Value)
break
}
}
}
}
return values, true
}
// descendByIndex performs array access by index.
func (p *pathParser) descendByIndex(objs []interface{}, indices ...int) ([]interface{}, bool) {
if p.depth <= 0 {
return nil, false
}
p.depth--
var values []interface{}
for i := range objs {
obj, ok := objs[i].([]interface{})
if !ok {
continue
}
for _, j := range indices {
if j < 0 {
j += len(obj)
}
if 0 <= j && j < len(obj) {
if maxObjects < len(values)+1 {
return nil, false
}
values = append(values, obj[j])
}
}
}
return values, true
}
// processLeftBracket processes index expressions which can be either
// array/map access, array sub-slice or union of indices.
func (p *pathParser) processLeftBracket(objs []interface{}) ([]interface{}, bool) {
typ, value := p.nextToken()
switch typ {
case pathAsterisk:
typ, _ := p.nextToken()
if typ != pathRightBracket {
return nil, false
}
return p.descend(objs)
case pathColon:
return p.processSlice(objs, 0)
case pathNumber:
subTyp, _ := p.nextToken()
switch subTyp {
case pathColon:
index, err := strconv.ParseInt(value, 10, 32)
if err != nil {
return nil, false
}
return p.processSlice(objs, int(index))
case pathComma:
return p.processUnion(objs, pathNumber, value)
case pathRightBracket:
index, err := strconv.ParseInt(value, 10, 32)
if err != nil {
return nil, false
}
return p.descendByIndex(objs, int(index))
default:
return nil, false
}
case pathString:
subTyp, _ := p.nextToken()
switch subTyp {
case pathComma:
return p.processUnion(objs, pathString, value)
case pathRightBracket:
s := strings.Trim(value, "'")
err := json.Unmarshal([]byte(`"`+s+`"`), &s)
if err != nil {
return nil, false
}
return p.descendByIdent(objs, s)
default:
return nil, false
}
default:
return nil, false
}
}
// processUnion processes union of multiple indices.
// firstTyp is assumed to be either pathNumber or pathString.
func (p *pathParser) processUnion(objs []interface{}, firstTyp pathTokenType, firstVal string) ([]interface{}, bool) {
items := []string{firstVal}
for {
typ, val := p.nextToken()
if typ != firstTyp {
return nil, false
}
items = append(items, val)
typ, _ = p.nextToken()
if typ == pathRightBracket {
break
} else if typ != pathComma {
return nil, false
}
}
switch firstTyp {
case pathNumber:
values := make([]int, len(items))
for i := range items {
index, err := strconv.ParseInt(items[i], 10, 32)
if err != nil {
return nil, false
}
values[i] = int(index)
}
return p.descendByIndex(objs, values...)
case pathString:
for i := range items {
s := strings.Trim(items[i], "'")
err := json.Unmarshal([]byte(`"`+s+`"`), &items[i])
if err != nil {
return nil, false
}
}
return p.descendByIdent(objs, items...)
default:
panic("token in union must be either number or string")
}
}
// processSlice processes a slice with the specified start index.
func (p *pathParser) processSlice(objs []interface{}, start int) ([]interface{}, bool) {
typ, val := p.nextToken()
switch typ {
case pathNumber:
typ, _ := p.nextToken()
if typ != pathRightBracket {
return nil, false
}
index, err := strconv.ParseInt(val, 10, 32)
if err != nil {
return nil, false
}
return p.descendByRange(objs, start, int(index))
case pathRightBracket:
return p.descendByRange(objs, start, 0)
default:
return nil, false
}
}
// descendByRange is similar to descend but skips maps and returns sub-slices for arrays.
func (p *pathParser) descendByRange(objs []interface{}, start, end int) ([]interface{}, bool) {
if p.depth <= 0 {
return nil, false
}
p.depth--
var values []interface{}
for i := range objs {
arr, ok := objs[i].([]interface{})
if !ok {
continue
}
subStart := start
if subStart < 0 {
subStart += len(arr)
}
subEnd := end
if subEnd <= 0 {
subEnd += len(arr)
}
if subEnd > len(arr) {
subEnd = len(arr)
}
if subEnd <= subStart {
continue
}
if maxObjects < len(values)+subEnd-subStart {
return nil, false
}
values = append(values, arr[subStart:subEnd]...)
}
return values, true
}