MCPcopy
hub / github.com/tidwall/gjson / execModifier

Function execModifier

gjson.go:2850–2905  ·  view source on GitHub ↗

execModifier parses the path to find a matching modifier function. The input expects that the path already starts with a '@'

(json, path string)

Source from the content-addressed store, hash-verified

2848// execModifier parses the path to find a matching modifier function.
2849// The input expects that the path already starts with a '@'
2850func execModifier(json, path string) (pathOut, res string, ok bool) {
2851 name := path[1:]
2852 var hasArgs bool
2853 for i := 1; i < len(path); i++ {
2854 if path[i] == ':' {
2855 pathOut = path[i+1:]
2856 name = path[1:i]
2857 hasArgs = len(pathOut) > 0
2858 break
2859 }
2860 if path[i] == '|' {
2861 pathOut = path[i:]
2862 name = path[1:i]
2863 break
2864 }
2865 if path[i] == '.' {
2866 pathOut = path[i:]
2867 name = path[1:i]
2868 break
2869 }
2870 }
2871 if fn, ok := modifiers[name]; ok {
2872 var args string
2873 if hasArgs {
2874 var parsedArgs bool
2875 switch pathOut[0] {
2876 case '{', '[', '"':
2877 // json arg
2878 res := Parse(pathOut)
2879 if res.Exists() {
2880 args = squash(pathOut)
2881 pathOut = pathOut[len(args):]
2882 parsedArgs = true
2883 }
2884 }
2885 if !parsedArgs {
2886 // simple arg
2887 i := 0
2888 for ; i < len(pathOut); i++ {
2889 if pathOut[i] == '|' {
2890 break
2891 }
2892 switch pathOut[i] {
2893 case '{', '[', '"', '(':
2894 s := squash(pathOut[i:])
2895 i += len(s) - 1
2896 }
2897 }
2898 args = pathOut[:i]
2899 pathOut = pathOut[i:]
2900 }
2901 }
2902 return pathOut, fn(json, args), true
2903 }
2904 return pathOut, res, false
2905}
2906
2907// unwrap removes the '[]' or '{}' characters around json

Callers 1

GetFunction · 0.85

Calls 3

ParseFunction · 0.85
squashFunction · 0.85
ExistsMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…