-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathcover.html
More file actions
1814 lines (1672 loc) · 90.2 KB
/
cover.html
File metadata and controls
1814 lines (1672 loc) · 90.2 KB
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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>jsonpath: Go Coverage Report</title>
<style>
body {
background: black;
color: rgb(80, 80, 80);
}
body, pre, #legend span {
font-family: Menlo, monospace;
font-weight: bold;
}
#topbar {
background: black;
position: fixed;
top: 0; left: 0; right: 0;
height: 42px;
border-bottom: 1px solid rgb(80, 80, 80);
}
#content {
margin-top: 50px;
}
#nav, #legend {
float: left;
margin-left: 10px;
}
#legend {
margin-top: 12px;
}
#nav {
margin-top: 10px;
}
#legend span {
margin: 0 5px;
}
.cov0 { color: rgb(192, 0, 0) }
.cov1 { color: rgb(128, 128, 128) }
.cov2 { color: rgb(116, 140, 131) }
.cov3 { color: rgb(104, 152, 134) }
.cov4 { color: rgb(92, 164, 137) }
.cov5 { color: rgb(80, 176, 140) }
.cov6 { color: rgb(68, 188, 143) }
.cov7 { color: rgb(56, 200, 146) }
.cov8 { color: rgb(44, 212, 149) }
.cov9 { color: rgb(32, 224, 152) }
.cov10 { color: rgb(20, 236, 155) }
</style>
</head>
<body>
<div id="topbar">
<div id="nav">
<select id="files">
<option value="file0">github.com/oliveagle/jsonpath/jsonpath.go (90.9%)</option>
</select>
</div>
<div id="legend">
<span>not tracked</span>
<span class="cov0">not covered</span>
<span class="cov8">covered</span>
</div>
</div>
<div id="content">
<pre class="file" id="file0" style="display: none">// Copyright 2015, 2021; oliver, DoltHub Authors
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
package jsonpath
import (
"errors"
"fmt"
"go/token"
"go/types"
"reflect"
"regexp"
"sort"
"strconv"
"strings"
)
var ErrGetFromNullObj = errors.New("get attribute from null object")
var ErrKeyError = errors.New("key error: %s not found in object")
func JsonPathLookup(obj interface{}, jpath string) (interface{}, error) <span class="cov8" title="1">{
c, err := Compile(jpath)
if err != nil </span><span class="cov8" title="1">{
return nil, err
}</span>
<span class="cov8" title="1">return c.Lookup(obj)</span>
}
type Compiled struct {
path string
steps []step
}
type step struct {
op string
key string
args interface{}
}
func MustCompile(jpath string) *Compiled <span class="cov8" title="1">{
c, err := Compile(jpath)
if err != nil </span><span class="cov8" title="1">{
panic(err)</span>
}
<span class="cov8" title="1">return c</span>
}
func Compile(jpath string) (*Compiled, error) <span class="cov8" title="1">{
tokens, err := tokenize(jpath)
if err != nil </span><span class="cov8" title="1">{
return nil, err
}</span>
<span class="cov8" title="1">if len(tokens) == 0 </span><span class="cov8" title="1">{
return nil, fmt.Errorf("empty path")
}</span>
<span class="cov8" title="1">if tokens[0] != "@" && tokens[0] != "$" </span><span class="cov0" title="0">{
return nil, fmt.Errorf("$ or @ should in front of path")
}</span>
<span class="cov8" title="1">tokens = tokens[1:]
res := Compiled{
path: jpath,
steps: make([]step, len(tokens)),
}
for i, token := range tokens </span><span class="cov8" title="1">{
op, key, args, err := parse_token(token)
if err != nil </span><span class="cov8" title="1">{
return nil, err
}</span>
<span class="cov8" title="1">res.steps[i] = step{op, key, args}</span>
}
<span class="cov8" title="1">return &res, nil</span>
}
func (c *Compiled) String() string <span class="cov8" title="1">{
return fmt.Sprintf("Compiled lookup: %s", c.path)
}</span>
func (c *Compiled) Lookup(obj interface{}) (interface{}, error) <span class="cov8" title="1">{
var err error
for i, s := range c.steps </span><span class="cov8" title="1">{
// "key", "idx"
switch s.op </span>{
case "key":<span class="cov8" title="1">
obj, err = get_key(obj, s.key)
if err != nil </span><span class="cov8" title="1">{
return nil, err
}</span>
case "idx":<span class="cov8" title="1">
if len(s.key) > 0 </span><span class="cov8" title="1">{
// no key `$[0].test`
obj, err = get_key(obj, s.key)
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
}
<span class="cov8" title="1">if len(s.args.([]int)) > 1 </span><span class="cov8" title="1">{
res := []interface{}{}
for _, x := range s.args.([]int) </span><span class="cov8" title="1">{
//fmt.Println("idx ---- ", x)
tmp, err := get_idx(obj, x)
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov8" title="1">res = append(res, tmp)</span>
}
<span class="cov8" title="1">obj = res</span>
} else<span class="cov8" title="1"> if len(s.args.([]int)) == 1 </span><span class="cov8" title="1">{
//fmt.Println("idx ----------------3")
obj, err = get_idx(obj, s.args.([]int)[0])
if err != nil </span><span class="cov8" title="1">{
return nil, err
}</span>
} else<span class="cov0" title="0"> {
//fmt.Println("idx ----------------4")
return nil, fmt.Errorf("cannot index on empty slice")
}</span>
case "range":<span class="cov8" title="1">
if len(s.key) > 0 </span><span class="cov8" title="1">{
// no key `$[:1].test`
obj, err = get_key(obj, s.key)
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
}
<span class="cov8" title="1">if argsv, ok := s.args.([2]interface{}); ok == true </span><span class="cov8" title="1">{
obj, err = get_range(obj, argsv[0], argsv[1])
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
} else<span class="cov0" title="0"> {
return nil, fmt.Errorf("range args length should be 2")
}</span>
case "filter":<span class="cov8" title="1">
obj, err = get_key(obj, s.key)
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov8" title="1">obj, err = get_filtered(obj, obj, s.args.(string))
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
case "recursive":<span class="cov8" title="1">
obj = getAllDescendants(obj)
// Heuristic: if next step is key, exclude slices from candidates to avoid double-matching
// (once as container via implicit map, once as individual elements)
if i+1 < len(c.steps) && c.steps[i+1].op == "key" </span><span class="cov8" title="1">{
if candidates, ok := obj.([]interface{}); ok </span><span class="cov8" title="1">{
filtered := []interface{}{}
for _, cand := range candidates </span><span class="cov8" title="1">{
// Filter out Slices (but keep Maps and others)
// because get_key on Slice iterates children, which are already in candidates
v := reflect.ValueOf(cand)
if v.Kind() != reflect.Slice </span><span class="cov8" title="1">{
filtered = append(filtered, cand)
}</span>
}
<span class="cov8" title="1">obj = filtered</span>
}
}
case "func":<span class="cov8" title="1">
// Handle function calls like length()
// For function calls like $.length(), the key is the function name (e.g., "length")
// For path-based function calls like $.store.book.length(), the key is empty
// and we need to evaluate the function on the current object
if len(s.key) > 0 </span><span class="cov8" title="1">{
// This case handles paths like $.store.book.length() where the function
// is called on the result of the previous path step
obj, err = eval_func(obj, s.key)
if err != nil </span><span class="cov8" title="1">{
return nil, err
}</span>
} else<span class="cov0" title="0"> {
// This case handles direct function calls like $.length() or @.length()
obj, err = eval_func(obj, s.key)
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
}
default:<span class="cov0" title="0">
return nil, fmt.Errorf("unsupported jsonpath operation: %s", s.op)</span>
}
}
<span class="cov8" title="1">return obj, nil</span>
}
func tokenize(query string) ([]string, error) <span class="cov8" title="1">{
tokens := []string{}
// token_start := false
// token_end := false
token := ""
quoteChar := rune(0)
// fmt.Println("-------------------------------------------------- start")
for idx, x := range query </span><span class="cov8" title="1">{
if quoteChar != 0 </span><span class="cov8" title="1">{
if x == quoteChar </span><span class="cov8" title="1">{
quoteChar = 0
}</span> else<span class="cov8" title="1"> {
token += string(x)
}</span>
<span class="cov8" title="1">continue</span>
} else<span class="cov8" title="1"> if x == '"' </span><span class="cov8" title="1">{
if token == "." </span><span class="cov8" title="1">{
token = ""
}</span>
<span class="cov8" title="1">quoteChar = x
continue</span>
}
<span class="cov8" title="1">token += string(x)
// //fmt.Printf("idx: %d, x: %s, token: %s, tokens: %v\n", idx, string(x), token, tokens)
if idx == 0 </span><span class="cov8" title="1">{
if token == "$" || token == "@" </span><span class="cov8" title="1">{
tokens = append(tokens, token[:])
token = ""
continue</span>
} else<span class="cov8" title="1"> {
return nil, fmt.Errorf("should start with '$'")
}</span>
}
<span class="cov8" title="1">if token == "." </span><span class="cov8" title="1">{
continue</span>
} else<span class="cov8" title="1"> if token == ".." </span><span class="cov8" title="1">{
if len(tokens) == 0 || tokens[len(tokens)-1] != ".." </span><span class="cov8" title="1">{
tokens = append(tokens, "..")
}</span>
<span class="cov8" title="1">token = "."
continue</span>
} else<span class="cov8" title="1"> {
// fmt.Println("else: ", string(x), token)
if strings.Contains(token, "[") </span><span class="cov8" title="1">{
// fmt.Println(" contains [ ")
if x == ']' && !strings.HasSuffix(token, "\\]") </span><span class="cov8" title="1">{
if token[0] == '.' </span><span class="cov8" title="1">{
tokens = append(tokens, token[1:])
}</span> else<span class="cov8" title="1"> {
tokens = append(tokens, token[:])
}</span>
<span class="cov8" title="1">token = ""
continue</span>
}
} else<span class="cov8" title="1"> {
// fmt.Println(" doesn't contains [ ")
if x == '.' </span><span class="cov8" title="1">{
if token[0] == '.' </span><span class="cov8" title="1">{
tokens = append(tokens, token[1:len(token)-1])
}</span> else<span class="cov8" title="1"> {
tokens = append(tokens, token[:len(token)-1])
}</span>
<span class="cov8" title="1">token = "."
continue</span>
}
}
}
}
<span class="cov8" title="1">if quoteChar != 0 </span><span class="cov8" title="1">{
token = string(quoteChar) + token
}</span>
<span class="cov8" title="1">if len(token) > 0 </span><span class="cov8" title="1">{
if token[0] == '.' </span><span class="cov8" title="1">{
token = token[1:]
if token != "*" </span><span class="cov8" title="1">{
tokens = append(tokens, token[:])
}</span> else<span class="cov8" title="1"> if len(tokens) > 0 && tokens[len(tokens)-1] == ".." </span>{<span class="cov8" title="1">
// $..* means recursive descent with scan, * is redundant after ..
// Don't add * as separate token
}</span> else<span class="cov8" title="1"> if tokens[len(tokens)-1] != "*" </span><span class="cov8" title="1">{
tokens = append(tokens, token[:])
}</span>
} else<span class="cov8" title="1"> {
if token != "*" </span><span class="cov8" title="1">{
tokens = append(tokens, token[:])
}</span> else<span class="cov0" title="0"> if len(tokens) > 0 && tokens[len(tokens)-1] == ".." </span>{<span class="cov0" title="0">
// $..* means recursive descent with scan, * is redundant after ..
// Don't add * as separate token
}</span> else<span class="cov0" title="0"> if tokens[len(tokens)-1] != "*" </span><span class="cov0" title="0">{
tokens = append(tokens, token[:])
}</span>
}
}
// fmt.Println("finished tokens: ", tokens)
// fmt.Println("================================================= done ")
<span class="cov8" title="1">return tokens, nil</span>
}
/*
op: "root", "key", "idx", "range", "filter", "scan"
*/
func parse_token(token string) (op string, key string, args interface{}, err error) <span class="cov8" title="1">{
if token == "$" </span><span class="cov8" title="1">{
return "root", "$", nil, nil
}</span>
<span class="cov8" title="1">if token == "*" </span><span class="cov8" title="1">{
return "scan", "*", nil, nil
}</span>
<span class="cov8" title="1">if token == ".." </span><span class="cov8" title="1">{
return "recursive", "..", nil, nil
}</span>
<span class="cov8" title="1">bracket_idx := strings.Index(token, "[")
if bracket_idx < 0 </span><span class="cov8" title="1">{
// Check for function call like length()
if strings.HasSuffix(token, "()") </span><span class="cov8" title="1">{
funcName := strings.TrimSuffix(token, "()")
return "func", funcName, nil, nil
}</span>
<span class="cov8" title="1">return "key", token, nil, nil</span>
} else<span class="cov8" title="1"> {
key = token[:bracket_idx]
tail := token[bracket_idx:]
if len(tail) < 3 </span><span class="cov8" title="1">{
err = fmt.Errorf("len(tail) should >=3, %v", tail)
return
}</span>
<span class="cov8" title="1">tail = tail[1 : len(tail)-1]
//fmt.Println(key, tail)
if strings.HasPrefix(tail, "?") </span><span class="cov8" title="1">{
// filter -------------------------------------------------
op = "filter"
// Remove leading ? - the content is everything after ?
filterContent := tail[1:]
// Handle filters like [?( @.isbn )] - remove outer parentheses if present
filterContent = strings.TrimSpace(filterContent)
if strings.HasPrefix(filterContent, "(") && strings.HasSuffix(filterContent, ")") </span><span class="cov8" title="1">{
// Remove outer parentheses
inner := filterContent[1 : len(filterContent)-1]
filterContent = strings.TrimSpace(inner)
}</span>
<span class="cov8" title="1">args = filterContent
return</span>
} else<span class="cov8" title="1"> if strings.Contains(tail, ":") </span><span class="cov8" title="1">{
// range ----------------------------------------------
op = "range"
tails := strings.Split(tail, ":")
if len(tails) != 2 </span><span class="cov0" title="0">{
err = fmt.Errorf("only support one range(from, to): %v", tails)
return
}</span>
<span class="cov8" title="1">var frm interface{}
var to interface{}
if frm, err = strconv.Atoi(strings.Trim(tails[0], " ")); err != nil </span><span class="cov8" title="1">{
if strings.Trim(tails[0], " ") == "" </span><span class="cov8" title="1">{
err = nil
}</span>
<span class="cov8" title="1">frm = nil</span>
}
<span class="cov8" title="1">if to, err = strconv.Atoi(strings.Trim(tails[1], " ")); err != nil </span><span class="cov8" title="1">{
if strings.Trim(tails[1], " ") == "" </span><span class="cov8" title="1">{
err = nil
}</span>
<span class="cov8" title="1">to = nil</span>
}
<span class="cov8" title="1">args = [2]interface{}{frm, to}
return</span>
} else<span class="cov8" title="1"> if tail == "*" </span><span class="cov8" title="1">{
op = "range"
args = [2]interface{}{nil, nil}
return
}</span> else<span class="cov8" title="1"> {
// idx ------------------------------------------------
op = "idx"
res := []int{}
for _, x := range strings.Split(tail, ",") </span><span class="cov8" title="1">{
if i, err := strconv.Atoi(strings.Trim(x, " ")); err == nil </span><span class="cov8" title="1">{
res = append(res, i)
}</span> else<span class="cov0" title="0"> {
return "", "", nil, err
}</span>
}
<span class="cov8" title="1">args = res</span>
}
}
<span class="cov8" title="1">return op, key, args, nil</span>
}
func filter_get_from_explicit_path(obj interface{}, path string) (interface{}, error) <span class="cov8" title="1">{
steps, err := tokenize(path)
//fmt.Println("f: steps: ", steps, err)
//fmt.Println(path, steps)
if err != nil </span><span class="cov8" title="1">{
return nil, err
}</span>
<span class="cov8" title="1">if steps[0] != "@" && steps[0] != "$" </span><span class="cov0" title="0">{
return nil, fmt.Errorf("$ or @ should in front of path")
}</span>
<span class="cov8" title="1">steps = steps[1:]
xobj := obj
//fmt.Println("f: xobj", xobj)
for _, s := range steps </span><span class="cov8" title="1">{
op, key, args, err := parse_token(s)
if err != nil </span><span class="cov8" title="1">{
return nil, err
}</span>
// "key", "idx"
<span class="cov8" title="1">switch op </span>{
case "key":<span class="cov8" title="1">
xobj, err = get_key(xobj, key)
if err != nil </span><span class="cov8" title="1">{
return nil, err
}</span>
case "idx":<span class="cov8" title="1">
if len(args.([]int)) != 1 </span><span class="cov0" title="0">{
return nil, fmt.Errorf("don't support multiple index in filter")
}</span>
<span class="cov8" title="1">xobj, err = get_key(xobj, key)
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov8" title="1">xobj, err = get_idx(xobj, args.([]int)[0])
if err != nil </span><span class="cov8" title="1">{
return nil, err
}</span>
case "func":<span class="cov8" title="1">
// Handle function calls like length()
xobj, err = get_key(xobj, key)
if err != nil </span><span class="cov8" title="1">{
return nil, err
}</span>
<span class="cov8" title="1">xobj, err = eval_func(xobj, key)
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
default:<span class="cov0" title="0">
return nil, fmt.Errorf("unsupported jsonpath operation %s in filter", op)</span>
}
}
<span class="cov8" title="1">return xobj, nil</span>
}
func get_key(obj interface{}, key string) (interface{}, error) <span class="cov8" title="1">{
if reflect.TypeOf(obj) == nil </span><span class="cov8" title="1">{
return nil, ErrGetFromNullObj
}</span>
<span class="cov8" title="1">value := reflect.ValueOf(obj)
switch value.Kind() </span>{
case reflect.Map:<span class="cov8" title="1">
// if obj came from stdlib json, its highly likely to be a map[string]interface{}
// in which case we can save having to iterate the map keys to work out if the
// key exists
if jsonMap, ok := obj.(map[string]interface{}); ok </span><span class="cov8" title="1">{
val, exists := jsonMap[key]
if !exists </span><span class="cov8" title="1">{
return nil, fmt.Errorf("key error: %s not found in object", key)
}</span>
<span class="cov8" title="1">return val, nil</span>
}
<span class="cov8" title="1">for _, kv := range value.MapKeys() </span><span class="cov8" title="1">{
//fmt.Println(kv.String())
if kv.String() == key </span><span class="cov8" title="1">{
return value.MapIndex(kv).Interface(), nil
}</span>
}
<span class="cov0" title="0">return nil, fmt.Errorf("key error: %s not found in object", key)</span>
case reflect.Slice:<span class="cov8" title="1">
// slice we should get from all objects in it.
// if key is empty, return the slice itself (for root array filtering)
if key == "" </span><span class="cov8" title="1">{
return obj, nil
}</span>
<span class="cov8" title="1">res := []interface{}{}
for i := 0; i < value.Len(); i++ </span><span class="cov8" title="1">{
tmp, _ := get_idx(obj, i)
if v, err := get_key(tmp, key); err == nil </span><span class="cov8" title="1">{
res = append(res, v)
}</span>
}
<span class="cov8" title="1">return res, nil</span>
case reflect.Ptr:<span class="cov8" title="1">
// Unwrap pointer
realValue := value.Elem()
if !realValue.IsValid() </span><span class="cov0" title="0">{
return nil, fmt.Errorf("null pointer")
}</span>
<span class="cov8" title="1">return get_key(realValue.Interface(), key)</span>
case reflect.Interface:<span class="cov0" title="0">
// Unwrap interface value
realValue := value.Elem()
return get_key(realValue.Interface(), key)</span>
case reflect.Struct:<span class="cov8" title="1">
for i := 0; i < value.NumField(); i++ </span><span class="cov8" title="1">{
valueField := value.Field(i)
structField := value.Type().Field(i)
// Embeded struct
if valueField.Kind() == reflect.Struct && structField.Anonymous </span><span class="cov8" title="1">{
v, _ := get_key(valueField.Interface(), key)
if v != nil </span><span class="cov8" title="1">{
return v, nil
}</span>
} else<span class="cov8" title="1"> {
if structField.Name == key </span><span class="cov8" title="1">{
return valueField.Interface(), nil
}</span>
<span class="cov8" title="1">if tag := structField.Tag.Get("json"); tag != "" </span><span class="cov8" title="1">{
values := strings.Split(tag, ",")
for _, tagValue := range values </span><span class="cov8" title="1">{
// In the following cases json tag names should not be checked:
// ",omitempty", "-", "-,"
if (tagValue == "" && len(values) == 2) || tagValue == "-" </span><span class="cov8" title="1">{
break</span>
}
<span class="cov8" title="1">if tagValue != "omitempty" && tagValue == key </span><span class="cov8" title="1">{
return valueField.Interface(), nil
}</span>
}
}
}
}
<span class="cov8" title="1">return nil, fmt.Errorf("key error: %s not found in struct", key)</span>
default:<span class="cov8" title="1">
return nil, fmt.Errorf("object is not map")</span>
}
}
func get_idx(obj interface{}, idx int) (interface{}, error) <span class="cov8" title="1">{
switch reflect.TypeOf(obj).Kind() </span>{
case reflect.Slice:<span class="cov8" title="1">
length := reflect.ValueOf(obj).Len()
if idx >= 0 </span><span class="cov8" title="1">{
if idx >= length </span><span class="cov8" title="1">{
return nil, fmt.Errorf("index out of range: len: %v, idx: %v", length, idx)
}</span>
<span class="cov8" title="1">return reflect.ValueOf(obj).Index(idx).Interface(), nil</span>
} else<span class="cov8" title="1"> {
// < 0
_idx := length + idx
if _idx < 0 </span><span class="cov8" title="1">{
return nil, fmt.Errorf("index out of range: len: %v, idx: %v", length, idx)
}</span>
<span class="cov8" title="1">return reflect.ValueOf(obj).Index(_idx).Interface(), nil</span>
}
default:<span class="cov8" title="1">
return nil, fmt.Errorf("object is not Slice")</span>
}
}
func get_range(obj, frm, to interface{}) (interface{}, error) <span class="cov8" title="1">{
switch reflect.TypeOf(obj).Kind() </span>{
case reflect.Slice:<span class="cov8" title="1">
length := reflect.ValueOf(obj).Len()
_frm := 0
_to := length
if frm == nil </span><span class="cov8" title="1">{
frm = 0
}</span>
<span class="cov8" title="1">if to == nil </span><span class="cov8" title="1">{
to = length
}</span>
<span class="cov8" title="1">if fv, ok := frm.(int); ok == true </span><span class="cov8" title="1">{
if fv < 0 </span><span class="cov8" title="1">{
_frm = length + fv
}</span> else<span class="cov8" title="1"> {
_frm = fv
}</span>
}
<span class="cov8" title="1">if tv, ok := to.(int); ok == true </span><span class="cov8" title="1">{
if tv < 0 </span><span class="cov8" title="1">{
_to = length + tv + 1
}</span> else<span class="cov8" title="1"> {
_to = tv
}</span>
}
<span class="cov8" title="1">if _frm < 0 || _frm >= length </span><span class="cov8" title="1">{
return nil, fmt.Errorf("index [from] out of range: len: %v, from: %v", length, frm)
}</span>
// Clamp _to to valid range [0, length] per RFC 9535
<span class="cov8" title="1">if _to < 0 </span><span class="cov8" title="1">{
_to = 0
}</span>
<span class="cov8" title="1">if _to > length </span><span class="cov8" title="1">{
_to = length
}</span>
//fmt.Println("_frm, _to: ", _frm, _to)
<span class="cov8" title="1">res_v := reflect.ValueOf(obj).Slice(_frm, _to)
return res_v.Interface(), nil</span>
case reflect.Map:<span class="cov8" title="1">
// For wildcard [*] on maps, return all values
var res []interface{}
if jsonMap, ok := obj.(map[string]interface{}); ok </span><span class="cov8" title="1">{
for _, v := range jsonMap </span><span class="cov8" title="1">{
res = append(res, v)
}</span>
<span class="cov8" title="1">return res, nil</span>
}
<span class="cov0" title="0">keys := reflect.ValueOf(obj).MapKeys()
for _, k := range keys </span><span class="cov0" title="0">{
res = append(res, reflect.ValueOf(obj).MapIndex(k).Interface())
}</span>
<span class="cov0" title="0">return res, nil</span>
default:<span class="cov8" title="1">
return nil, fmt.Errorf("object is not Slice")</span>
}
}
func regFilterCompile(rule string) (*regexp.Regexp, error) <span class="cov8" title="1">{
runes := []rune(rule)
if len(runes) <= 2 </span><span class="cov8" title="1">{
return nil, errors.New("empty rule")
}</span>
<span class="cov8" title="1">if runes[0] != '/' || runes[len(runes)-1] != '/' </span><span class="cov8" title="1">{
return nil, errors.New("invalid syntax. should be in `/pattern/` form")
}</span>
<span class="cov8" title="1">runes = runes[1 : len(runes)-1]
return regexp.Compile(string(runes))</span>
}
func get_filtered(obj, root interface{}, filter string) ([]interface{}, error) <span class="cov8" title="1">{
lp, op, rp, err := parse_filter(filter)
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov8" title="1">res := []interface{}{}
switch reflect.TypeOf(obj).Kind() </span>{
case reflect.Slice:<span class="cov8" title="1">
if op == "=~" </span><span class="cov8" title="1">{
// regexp
pat, err := regFilterCompile(rp)
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov8" title="1">for i := 0; i < reflect.ValueOf(obj).Len(); i++ </span><span class="cov8" title="1">{
tmp := reflect.ValueOf(obj).Index(i).Interface()
ok, err := eval_reg_filter(tmp, root, lp, pat)
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov8" title="1">if ok == true </span><span class="cov8" title="1">{
res = append(res, tmp)
}</span>
}
} else<span class="cov8" title="1"> {
for i := 0; i < reflect.ValueOf(obj).Len(); i++ </span><span class="cov8" title="1">{
tmp := reflect.ValueOf(obj).Index(i).Interface()
ok, err := eval_filter(tmp, root, lp, op, rp)
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov8" title="1">if ok == true </span><span class="cov8" title="1">{
res = append(res, tmp)
}</span>
}
}
<span class="cov8" title="1">return res, nil</span>
case reflect.Map:<span class="cov8" title="1">
if op == "=~" </span><span class="cov8" title="1">{
// regexp
pat, err := regFilterCompile(rp)
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov8" title="1">for _, kv := range reflect.ValueOf(obj).MapKeys() </span><span class="cov8" title="1">{
tmp := reflect.ValueOf(obj).MapIndex(kv).Interface()
ok, err := eval_reg_filter(tmp, root, lp, pat)
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov8" title="1">if ok == true </span><span class="cov0" title="0">{
res = append(res, tmp)
}</span>
}
} else<span class="cov8" title="1"> {
for _, kv := range reflect.ValueOf(obj).MapKeys() </span><span class="cov8" title="1">{
tmp := reflect.ValueOf(obj).MapIndex(kv).Interface()
ok, err := eval_filter(tmp, root, lp, op, rp)
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov8" title="1">if ok == true </span><span class="cov8" title="1">{
res = append(res, tmp)
}</span>
}
}
default:<span class="cov8" title="1">
return nil, fmt.Errorf("don't support filter on this type: %v", reflect.TypeOf(obj).Kind())</span>
}
<span class="cov8" title="1">return res, nil</span>
}
func get_scan(obj interface{}) (interface{}, error) <span class="cov8" title="1">{
if reflect.TypeOf(obj) == nil </span><span class="cov8" title="1">{
return nil, nil
}</span>
<span class="cov8" title="1">switch reflect.TypeOf(obj).Kind() </span>{
case reflect.Map:<span class="cov8" title="1">
// iterate over keys in sorted by length, then alphabetically
var res []interface{}
if jsonMap, ok := obj.(map[string]interface{}); ok </span><span class="cov8" title="1">{
var sortedKeys []string
for k := range jsonMap </span><span class="cov8" title="1">{
sortedKeys = append(sortedKeys, k)
}</span>
<span class="cov8" title="1">sort.Slice(sortedKeys, func(i, j int) bool </span><span class="cov8" title="1">{
if len(sortedKeys[i]) != len(sortedKeys[j]) </span><span class="cov0" title="0">{
return len(sortedKeys[i]) < len(sortedKeys[j])
}</span>
<span class="cov8" title="1">return sortedKeys[i] < sortedKeys[j]</span>
})
<span class="cov8" title="1">for _, k := range sortedKeys </span><span class="cov8" title="1">{
res = append(res, jsonMap[k])
}</span>
<span class="cov8" title="1">return res, nil</span>
}
<span class="cov8" title="1">keys := reflect.ValueOf(obj).MapKeys()
sort.Slice(keys, func(i, j int) bool </span><span class="cov8" title="1">{
ki, kj := keys[i].String(), keys[j].String()
if len(ki) != len(kj) </span><span class="cov0" title="0">{
return len(ki) < len(kj)
}</span>
<span class="cov8" title="1">return ki < kj</span>
})
<span class="cov8" title="1">for _, k := range keys </span><span class="cov8" title="1">{
res = append(res, reflect.ValueOf(obj).MapIndex(k).Interface())
}</span>
<span class="cov8" title="1">return res, nil</span>
case reflect.Slice:<span class="cov8" title="1">
// slice we should get from all objects in it.
var res []interface{}
for i := 0; i < reflect.ValueOf(obj).Len(); i++ </span><span class="cov8" title="1">{
tmp := reflect.ValueOf(obj).Index(i).Interface()
newObj, err := get_scan(tmp)
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov8" title="1">res = append(res, newObj.([]interface{})...)</span>
}
<span class="cov8" title="1">return res, nil</span>
default:<span class="cov8" title="1">
return nil, fmt.Errorf("object is not scannable: %v", reflect.TypeOf(obj).Kind())</span>
}
}
// @.isbn => @.isbn, exists, nil
// @.price < 10 => @.price, <, 10
// @.price <= $.expensive => @.price, <=, $.expensive
// @.author =~ /.*REES/i => @.author, match, /.*REES/i
// count(@.book) > 0 => count(@.book), >, 0
// match(@.author, 'REES') => match(@.author, 'REES'), exists, nil
func parse_filter(filter string) (lp string, op string, rp string, err error) <span class="cov8" title="1">{
tmp := ""
stage := 0
quoteChar := rune(0)
parenDepth := 0
for idx, c := range filter </span><span class="cov8" title="1">{
switch c </span>{
case '\'':<span class="cov8" title="1">
if quoteChar == 0 </span><span class="cov8" title="1">{
quoteChar = c
}</span> else<span class="cov8" title="1"> if c == quoteChar </span><span class="cov8" title="1">{
quoteChar = 0
}</span> else<span class="cov8" title="1"> {
tmp += string(c)
}</span>
<span class="cov8" title="1">continue</span>
case '"':<span class="cov8" title="1">
if quoteChar == 0 </span><span class="cov8" title="1">{
quoteChar = c
}</span> else<span class="cov8" title="1"> if c == quoteChar </span><span class="cov8" title="1">{
quoteChar = 0
}</span> else<span class="cov8" title="1"> {
tmp += string(c)
}</span>
<span class="cov8" title="1">continue</span>
case '(':<span class="cov8" title="1">
if quoteChar == 0 </span><span class="cov8" title="1">{
parenDepth++
}</span>
<span class="cov8" title="1">tmp += string(c)
continue</span>
case ')':<span class="cov8" title="1">
if quoteChar == 0 </span><span class="cov8" title="1">{
parenDepth--
}</span>
<span class="cov8" title="1">tmp += string(c)
continue</span>
case ' ':<span class="cov8" title="1">
if quoteChar != 0 || parenDepth > 0 </span><span class="cov8" title="1">{
// Inside quotes or parentheses, keep the space
tmp += string(c)
continue</span>
}
<span class="cov8" title="1">if tmp != "" </span><span class="cov8" title="1">{
switch stage </span>{
case 0:<span class="cov8" title="1">
lp = tmp</span>
case 1:<span class="cov8" title="1">
op = tmp</span>
case 2:<span class="cov8" title="1">
rp = tmp</span>
}
<span class="cov8" title="1">tmp = ""
stage++
if stage > 2 </span><span class="cov8" title="1">{
return "", "", "", errors.New(fmt.Sprintf("invalid char at %d: `%c`", idx, c))
}</span>
}
default:<span class="cov8" title="1">
tmp += string(c)</span>
}
}
<span class="cov8" title="1">if tmp != "" </span><span class="cov8" title="1">{
switch stage </span>{
case 0:<span class="cov8" title="1">
lp = tmp
if strings.HasSuffix(lp, ")") || stage == 0 </span><span class="cov8" title="1">{
// Function call without operator, or simple expression without operator
// set exists operator
op = "exists"
}</span>
case 1:<span class="cov8" title="1">
op = tmp</span>
case 2:<span class="cov8" title="1">
rp = tmp</span>
}
}
<span class="cov8" title="1">return lp, op, rp, err</span>
}
// func parse_filter_v1(filter string) (lp string, op string, rp string, err error) {
// tmp := ""
// istoken := false
// for _, c := range filter {
// if istoken == false && c != ' ' {
// istoken = true
// }
// if istoken == true && c == ' ' {
// istoken = false
// }
// if istoken == true {
// tmp += string(c)
// }
// if istoken == false && tmp != "" {
// if lp == "" {
// lp = tmp[:]
// tmp = ""
// } else if op == "" {
// op = tmp[:]
// tmp = ""
// } else if rp == "" {
// rp = tmp[:]
// tmp = ""
// }
// }
// }
// if tmp != "" && lp == "" && op == "" && rp == "" {
// lp = tmp[:]
// op = "exists"
// rp = ""
// err = nil
// return
// } else if tmp != "" && rp == "" {
// rp = tmp[:]
// tmp = ""
// }
// return lp, op, rp, err
// }
func eval_reg_filter(obj, root interface{}, lp string, pat *regexp.Regexp) (res bool, err error) <span class="cov8" title="1">{
if pat == nil </span><span class="cov8" title="1">{
return false, errors.New("nil pat")
}</span>
<span class="cov8" title="1">lp_v, err := get_lp_v(obj, root, lp)
if err != nil </span><span class="cov8" title="1">{
return false, err
}</span>
<span class="cov8" title="1">switch v := lp_v.(type) </span>{
case string:<span class="cov8" title="1">
return pat.MatchString(v), nil</span>
default:<span class="cov8" title="1">
return false, errors.New("only string can match with regular expression")</span>
}
}
func get_lp_v(obj, root interface{}, lp string) (interface{}, error) <span class="cov8" title="1">{
// Check if lp is a function call like count(@.xxx) or match(@.xxx, pattern)
if strings.HasSuffix(lp, ")") </span><span class="cov8" title="1">{
return eval_filter_func(obj, root, lp)
}</span>
<span class="cov8" title="1">var lp_v interface{}
if strings.HasPrefix(lp, "@.") </span><span class="cov8" title="1">{
return filter_get_from_explicit_path(obj, lp)
}</span> else<span class="cov8" title="1"> if strings.HasPrefix(lp, "$.") </span><span class="cov8" title="1">{
return filter_get_from_explicit_path(root, lp)
}</span> else<span class="cov8" title="1"> {
lp_v = lp
}</span>
<span class="cov8" title="1">return lp_v, nil</span>
}
// eval_filter_func evaluates function calls in filter expressions
func eval_filter_func(obj, root interface{}, expr string) (interface{}, error) <span class="cov8" title="1">{
// Find the first ( that starts the function arguments
parenIdx := -1
for i, c := range expr </span><span class="cov8" title="1">{
if c == '(' </span><span class="cov8" title="1">{
parenIdx = i
break</span>
}
}
<span class="cov8" title="1">if parenIdx < 0 </span><span class="cov8" title="1">{
return nil, fmt.Errorf("invalid function call: %s", expr)
}</span>
<span class="cov8" title="1">funcName := strings.TrimSpace(expr[:parenIdx])
// Find the matching closing parenthesis
argsStart := parenIdx + 1