@@ -2754,6 +2754,7 @@ func execModifier(json, path string) (pathOut, res string, ok bool) {
2754
2754
var parsedArgs bool
2755
2755
switch pathOut [0 ] {
2756
2756
case '{' , '[' , '"' :
2757
+ // json arg
2757
2758
res := Parse (pathOut )
2758
2759
if res .Exists () {
2759
2760
args = squash (pathOut )
@@ -2762,14 +2763,20 @@ func execModifier(json, path string) (pathOut, res string, ok bool) {
2762
2763
}
2763
2764
}
2764
2765
if ! parsedArgs {
2765
- idx := strings .IndexByte (pathOut , '|' )
2766
- if idx == - 1 {
2767
- args = pathOut
2768
- pathOut = ""
2769
- } else {
2770
- args = pathOut [:idx ]
2771
- pathOut = pathOut [idx :]
2766
+ // simple arg
2767
+ i := 0
2768
+ for ; i < len (pathOut ); i ++ {
2769
+ if pathOut [i ] == '|' {
2770
+ break
2771
+ }
2772
+ switch pathOut [i ] {
2773
+ case '{' , '[' , '"' , '(' :
2774
+ s := squash (pathOut [i :])
2775
+ i += len (s ) - 1
2776
+ }
2772
2777
}
2778
+ args = pathOut [:i ]
2779
+ pathOut = pathOut [i :]
2773
2780
}
2774
2781
}
2775
2782
return pathOut , fn (json , args ), true
@@ -2789,19 +2796,24 @@ func unwrap(json string) string {
2789
2796
// DisableModifiers will disable the modifier syntax
2790
2797
var DisableModifiers = false
2791
2798
2792
- var modifiers = map [string ]func (json , arg string ) string {
2793
- "pretty" : modPretty ,
2794
- "ugly" : modUgly ,
2795
- "reverse" : modReverse ,
2796
- "this" : modThis ,
2797
- "flatten" : modFlatten ,
2798
- "join" : modJoin ,
2799
- "valid" : modValid ,
2800
- "keys" : modKeys ,
2801
- "values" : modValues ,
2802
- "tostr" : modToStr ,
2803
- "fromstr" : modFromStr ,
2804
- "group" : modGroup ,
2799
+ var modifiers map [string ]func (json , arg string ) string
2800
+
2801
+ func init () {
2802
+ modifiers = map [string ]func (json , arg string ) string {
2803
+ "pretty" : modPretty ,
2804
+ "ugly" : modUgly ,
2805
+ "reverse" : modReverse ,
2806
+ "this" : modThis ,
2807
+ "flatten" : modFlatten ,
2808
+ "join" : modJoin ,
2809
+ "valid" : modValid ,
2810
+ "keys" : modKeys ,
2811
+ "values" : modValues ,
2812
+ "tostr" : modToStr ,
2813
+ "fromstr" : modFromStr ,
2814
+ "group" : modGroup ,
2815
+ "dig" : modDig ,
2816
+ }
2805
2817
}
2806
2818
2807
2819
// AddModifier binds a custom modifier command to the GJSON syntax.
@@ -3435,3 +3447,30 @@ func escapeComp(comp string) string {
3435
3447
}
3436
3448
return comp
3437
3449
}
3450
+
3451
+ func parseRecursiveDescent (all []Result , parent Result , path string ) []Result {
3452
+ if res := parent .Get (path ); res .Exists () {
3453
+ all = append (all , res )
3454
+ }
3455
+ if parent .IsArray () || parent .IsObject () {
3456
+ parent .ForEach (func (_ , val Result ) bool {
3457
+ all = parseRecursiveDescent (all , val , path )
3458
+ return true
3459
+ })
3460
+ }
3461
+ return all
3462
+ }
3463
+
3464
+ func modDig (json , arg string ) string {
3465
+ all := parseRecursiveDescent (nil , Parse (json ), arg )
3466
+ var out []byte
3467
+ out = append (out , '[' )
3468
+ for i , res := range all {
3469
+ if i > 0 {
3470
+ out = append (out , ',' )
3471
+ }
3472
+ out = append (out , res .Raw ... )
3473
+ }
3474
+ out = append (out , ']' )
3475
+ return string (out )
3476
+ }
0 commit comments