forked from gdey/jsonpath
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparse.go
542 lines (496 loc) · 11.7 KB
/
parse.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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
package jsonpath
import (
"errors"
"fmt"
"go/token"
"go/types"
"io"
"reflect"
"regexp"
"sort"
"strconv"
"strings"
)
type Applicator interface {
Apply(v interface{}) (interface{}, error)
}
type node interface {
Applicator
SetNext(v node)
}
var (
MapTypeError = errors.New("Expected Type to be a Map.")
ArrayTypeError = errors.New("Expected Type to be an Array.")
SyntaxError = errors.New("Bad Syntax.")
NotSupportedError = errors.New("Not Supported")
NotFound = errors.New("Not Found")
IndexOutOfBounds = errors.New("Out of Bounds")
)
func applyNext(nn node, v interface{}) (interface{}, error) {
if nn == nil {
return v, nil
}
return nn.Apply(v)
}
// RootNode is always the top node. It does not really do anything other then
// delegate to the next node, and acts as a starting point.
// Every other node type embeds this node To get the NextNode functions.
type RootNode struct {
// The next Node in the sequence.
NextNode node
}
// Set the next node.
func (r *RootNode) SetNext(n node) {
r.NextNode = n
}
// Apply is the main workhourse, each node type will apply it's filtering rules
// to the provided value, returning the filtered result.
// It is expected that the node will call's it's Next Nodes Apply method as
// need by the rules of the Node.
func (r *RootNode) Apply(v interface{}) (interface{}, error) {
return applyNext(r.NextNode, v)
}
// MapSelection is a the basic filter for a Map type key. It will look at the
// in coming v and try to turn it into a map[string]interface{} value. If it
// successeeds it will then apply the NextNode to that interface value, or it
// will return the value if it has no NextNode.
type MapSelection struct {
Key string
RootNode
}
func (m *MapSelection) Apply(v interface{}) (interface{}, error) {
mv, ok := v.(map[string]interface{})
if !ok {
return v, MapTypeError
}
nv, ok := mv[m.Key]
if !ok {
return nil, NotFound
}
return applyNext(m.NextNode, nv)
}
// ArrySelection is a the basic filter for a Array type key. It is like MapSelection but for Arrays.
type ArraySelection struct {
Key int
RootNode
}
func (a *ArraySelection) Apply(v interface{}) (interface{}, error) {
arv, ok := v.([]interface{})
if !ok {
return v, ArrayTypeError
}
// Check to see if the value is in bounds for the array.
if a.Key < 0 || a.Key >= len(arv) {
return nil, IndexOutOfBounds
}
return applyNext(a.NextNode, arv[a.Key])
}
// WildCardSelection is a filter that grabs all the values and returns an Array of them
// It applies it's NextNode on each value.
type WildCardSelection struct {
RootNode
}
func (w *WildCardSelection) Apply(v interface{}) (interface{}, error) {
switch tv := v.(type) {
case map[string]interface{}:
var ret []interface{}
var keys []string
for key := range tv {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
rval, err := applyNext(w.NextNode, tv[key])
// Don't add anything that causes an error or returns nil.
if err == nil || rval != nil {
ret = flattenAppend(ret, rval)
}
}
return ret, nil
case []interface{}:
var ret []interface{}
for _, val := range tv {
rval, err := applyNext(w.NextNode, val)
// Don't add anything that causes an error or returns nil.
if err == nil || rval != nil {
ret = flattenAppend(ret, rval)
}
}
return ret, nil
default:
return applyNext(w.NextNode, v)
}
}
type WildCardKeySelection struct {
RootNode
}
func (w *WildCardKeySelection) Apply(v interface{}) (interface{}, error) {
switch tv := v.(type) {
case map[string]interface{}:
var ret []interface{}
var keys []string
for key, _ := range tv {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
rval, err := applyNext(w.NextNode, key)
// Don't add anything that causes an error or returns nil.
if err == nil || rval != nil {
ret = flattenAppend(ret, rval)
}
}
return ret, nil
default:
return applyNext(w.NextNode, v)
}
}
type WildCardFilterSelection struct {
RootNode
Key string
}
func (w *WildCardFilterSelection) Apply(v interface{}) (interface{}, error) {
var ret []interface{}
switch arv := v.(type) {
case map[string]interface{}:
rval, err := w.filter(arv)
if err == nil && rval != nil {
ret = append(ret, rval)
}
case []interface{}:
for _, val := range arv {
rval, err := w.filter(val)
// Don't add anything that causes an error or returns nil.
if err == nil && rval != nil {
ret = append(ret, rval)
}
}
default:
return v, ArrayTypeError
}
return ret, nil
}
func (w *WildCardFilterSelection) GetConditionsFromKey() ([]string, error) {
if w.Key == "" {
return nil, SyntaxError
}
conditions := []string{}
re1 := regexp.MustCompile(`(\s+\|\|\s+)`)
// split by || condition
orConditions := re1.Split(w.Key, -1)
for _, orCondition := range orConditions {
// if the orCondition contains an && condition and the terms of the end condition are between parentheses
// append to conditions
conditions = append(conditions, orCondition)
}
return conditions, nil
}
func (w *WildCardFilterSelection) filter(val interface{}) (interface{}, error) {
_, ok := val.(map[string]interface{})
if !ok {
return val, MapTypeError
}
conditions, err := w.GetConditionsFromKey()
if err != nil {
return nil, err
}
shouldKeep := false
for _, condition := range conditions {
//re, err := regexp.Compile(`[\S]+`)
re, err := regexp.Compile(`([@$.\w]+) ([<=>!~]{1,2}) ([']?[\w\W\d\s]+[']?)`)
if err != nil {
return val, err
}
simpleRe, err := regexp.Compile(`([@$.\w]+)$`)
if err != nil {
return val, err
}
//ops := re.FindAllString(w.Key, -1)
match := re.FindAllStringSubmatch(condition, -1)
if match == nil || len(match) == 0 {
match = simpleRe.FindAllStringSubmatch(condition, -1)
if match == nil || len(match) == 0 {
return val, SyntaxError
}
}
wa, _ := Parse(strings.Replace(match[0][1], "@", "$", 1))
subv, _ := wa.Apply(val)
if subv == nil {
continue
}
if len(match[0]) == 2 {
shouldKeep = true
} else if len(match[0]) == 4 {
op := match[0][2]
if op == "=~" || op == "!~" {
isOk, _ := cmp_wildcard(subv, match[0][3], op)
if !isOk {
continue
} else {
shouldKeep = true
}
}
isOk, _ := cmp_any(subv, match[0][3], op)
if !isOk {
continue
} else {
shouldKeep = true
}
}
}
if !shouldKeep {
return nil, nil
}
rval, err := applyNext(w.NextNode, val)
return rval, err
}
// DescentSelection is a filter that recursively descends applying it's NextNode and
// corrlating the results.
type DescentSelection struct {
RootNode
}
func isNil(i interface{}) bool {
vi := reflect.ValueOf(i)
if !vi.IsValid() {
return true
}
switch vi.Kind() {
case reflect.Chan, reflect.Interface, reflect.Func, reflect.Slice, reflect.Map, reflect.Ptr:
return vi.IsNil()
default:
return false
}
}
func flattenAppend(src []interface{}, values ...interface{}) []interface{} {
for _, value := range values {
av, ok := value.([]interface{})
if ok {
if len(av) > 0 {
src = flattenAppend(src, av...)
}
} else {
src = append(src, value)
}
}
return src
}
func (d *DescentSelection) Apply(v interface{}) (interface{}, error) {
var ret []interface{}
rval, err := applyNext(d.NextNode, v)
// Ignore errors here.
if err == nil && !isNil(rval) {
ret = flattenAppend(ret, rval)
}
switch tv := v.(type) {
default:
return ret, nil
case map[string]interface{}:
var keys []string
for key, _ := range tv {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
rval, err := d.Apply(tv[key])
// Don't add anything that causes an error or returns nil.
if err == nil && !isNil(rval) {
ret = flattenAppend(ret, rval)
}
}
return ret, nil
case []interface{}:
for _, val := range tv {
rval, err := d.Apply(val)
// Don't add anything that causes an error or returns nil.
if err == nil && !isNil(rval) {
ret = flattenAppend(ret, rval)
}
}
return ret, nil
}
}
// The first thing we need to do is transform a dot–notation to a bracket–notation.
func minNotNeg1(a int, bs ...int) int {
m := a
for _, b := range bs {
if a == -1 || (b != -1 && b < m) {
m = b
}
}
return m
}
func normalize(s string) string {
if s == "" || s == "$." {
return "$"
}
r := "$"
// first thing to do is read passed the $
if s[0] == '$' {
s = s[1:]
}
for len(s) > 0 {
// Grab all the bracketed entries
for len(s) > 0 && s[0] == '[' {
n := strings.Index(s, "]")
r += s[0 : n+1]
s = s[n+1:]
}
if len(s) <= 0 || s == "." {
break
}
if s[0] == '.' {
if s[1] == '.' {
r += "[..]"
s = s[2:]
} else {
s = s[1:]
}
}
if len(s) == 0 {
break
}
if s[0] == '*' {
r += "[*]"
if len(s) == 1 {
break
}
s = s[1:]
}
if s[0] == '@' {
r += "[@]"
if len(s) == 1 {
break
}
s = s[1:]
}
n := minNotNeg1(strings.Index(s, "["), strings.Index(s, "."))
if n == 0 {
continue
}
if n != -1 {
r += `["` + s[:n] + `"]`
s = s[n:]
} else {
r += `["` + s + `"]`
s = ""
}
}
return r
}
func getNode(s string) (node, string, error) {
var rs string
if len(s) == 0 {
return nil, s, io.EOF
}
n := strings.Index(s, "]")
if n == -1 {
return nil, s, SyntaxError
}
if len(s) > n {
rs = s[n+1:]
}
switch s[:2] {
case "[\"":
return &MapSelection{Key: s[2 : n-1]}, rs, nil
case "[*":
return &WildCardSelection{}, rs, nil
case "[@":
return &WildCardKeySelection{}, rs, nil
case "[.":
return &DescentSelection{}, rs, nil
case "[?", "[(":
return &WildCardFilterSelection{Key: s[3 : n-1]}, rs, nil
default: // Assume it's a array index otherwise.
i, err := strconv.Atoi(s[1:n])
if err != nil {
return nil, rs, SyntaxError
}
return &ArraySelection{Key: i}, rs, nil
}
}
// Parse parses the JSONPath and returns a object that can be applied to
// a structure to filter it down.
func Parse(s string) (Applicator, error) {
var nn node
var err error
s = normalize(s)
rt := RootNode{}
// Remove the starting '$'
s = s[1:]
var c node
c = &rt
for len(s) > 0 {
nn, s, err = getNode(s)
if err != nil {
return nil, err
}
c.SetNext(nn)
c = nn
}
return &rt, nil
}
func cmp_wildcard(obj1, obj2 interface{}, op string) (bool, error) {
var sobj1 string
switch obj1.(type) {
case string:
sobj1 = strings.ReplaceAll(obj1.(string), "'", "")
sobj1 = fmt.Sprintf("%v", sobj1)
default:
sobj1 = fmt.Sprintf("%v", obj1)
}
var sobj2 string
switch obj1.(type) {
case string:
sobj2 = strings.ReplaceAll(obj2.(string), "'", "")
sobj2 = fmt.Sprintf("^%v$", sobj2)
default:
sobj2 = fmt.Sprintf("^%v$", obj2)
}
re, err := regexp.Compile(sobj2)
if err != nil {
return false, err
}
switch op {
case "=~":
return re.MatchString(sobj1), nil
case "!~":
return !re.MatchString(sobj1), nil
}
return false, SyntaxError
}
func cmp_any(obj1, obj2 interface{}, op string) (bool, error) {
switch op {
case "<", "<=", "==", ">=", ">", "!=":
default:
return false, fmt.Errorf("op should only be <, <=, ==, !=, >= and >")
}
var sobj1 string
switch obj1.(type) {
case string:
sobj1 = strings.ReplaceAll(obj1.(string), "'", "")
sobj1 = fmt.Sprintf("\"%v\"", sobj1)
default:
sobj1 = fmt.Sprintf("%v", obj1)
}
var sobj2 string
switch obj1.(type) {
case string:
sobj2 = strings.ReplaceAll(obj2.(string), "'", "")
sobj2 = fmt.Sprintf("\"%v\"", sobj2)
default:
sobj2 = fmt.Sprintf("%v", obj2)
}
exp := fmt.Sprintf("%v %s %v", sobj1, op, sobj2)
fset := token.NewFileSet()
res, err := types.Eval(fset, nil, 0, exp)
if err != nil {
return false, err
}
if res.IsValue() == false || (res.Value.String() != "false" && res.Value.String() != "true") {
return false, fmt.Errorf("result should only be true or false")
}
if res.Value.String() == "true" {
return true, nil
}
return false, nil
}