Compare commits

..

No commits in common. "1ff915dd81b03cb807d30dff5ac085bd12d433be" and "8d2c36ffa41368759c3bcb25b8790c8ce1e5562d" have entirely different histories.

4 changed files with 20 additions and 126 deletions

View File

@ -211,7 +211,6 @@ There are currently the following built-in modifiers:
- `@tostr`: Converts json to a string. Wraps a json string. - `@tostr`: Converts json to a string. Wraps a json string.
- `@fromstr`: Converts a string from json. Unwraps a json string. - `@fromstr`: Converts a string from json. Unwraps a json string.
- `@group`: Groups arrays of objects. See [e4fc67c](https://github.com/tidwall/gjson/commit/e4fc67c92aeebf2089fabc7872f010e340d105db). - `@group`: Groups arrays of objects. See [e4fc67c](https://github.com/tidwall/gjson/commit/e4fc67c92aeebf2089fabc7872f010e340d105db).
- `@dig`: Search for a value without providing its entire path. See [e8e87f2](https://github.com/tidwall/gjson/commit/e8e87f2a00dc41f3aba5631094e21f59a8cf8cbf).
### Modifier arguments ### Modifier arguments

View File

@ -258,7 +258,6 @@ There are currently the following built-in modifiers:
- `@tostr`: Converts json to a string. Wraps a json string. - `@tostr`: Converts json to a string. Wraps a json string.
- `@fromstr`: Converts a string from json. Unwraps a json string. - `@fromstr`: Converts a string from json. Unwraps a json string.
- `@group`: Groups arrays of objects. See [e4fc67c](https://github.com/tidwall/gjson/commit/e4fc67c92aeebf2089fabc7872f010e340d105db). - `@group`: Groups arrays of objects. See [e4fc67c](https://github.com/tidwall/gjson/commit/e4fc67c92aeebf2089fabc7872f010e340d105db).
- `@dig`: Search for a value without providing its entire path. See [e8e87f2](https://github.com/tidwall/gjson/commit/e8e87f2a00dc41f3aba5631094e21f59a8cf8cbf).
#### Modifier arguments #### Modifier arguments

View File

@ -2754,7 +2754,6 @@ func execModifier(json, path string) (pathOut, res string, ok bool) {
var parsedArgs bool var parsedArgs bool
switch pathOut[0] { switch pathOut[0] {
case '{', '[', '"': case '{', '[', '"':
// json arg
res := Parse(pathOut) res := Parse(pathOut)
if res.Exists() { if res.Exists() {
args = squash(pathOut) args = squash(pathOut)
@ -2763,20 +2762,14 @@ func execModifier(json, path string) (pathOut, res string, ok bool) {
} }
} }
if !parsedArgs { if !parsedArgs {
// simple arg idx := strings.IndexByte(pathOut, '|')
i := 0 if idx == -1 {
for ; i < len(pathOut); i++ { args = pathOut
if pathOut[i] == '|' { pathOut = ""
break } else {
} args = pathOut[:idx]
switch pathOut[i] { pathOut = pathOut[idx:]
case '{', '[', '"', '(':
s := squash(pathOut[i:])
i += len(s) - 1
}
} }
args = pathOut[:i]
pathOut = pathOut[i:]
} }
} }
return pathOut, fn(json, args), true return pathOut, fn(json, args), true
@ -2796,24 +2789,19 @@ func unwrap(json string) string {
// DisableModifiers will disable the modifier syntax // DisableModifiers will disable the modifier syntax
var DisableModifiers = false var DisableModifiers = false
var modifiers map[string]func(json, arg string) string var modifiers = map[string]func(json, arg string) string{
"pretty": modPretty,
func init() { "ugly": modUgly,
modifiers = map[string]func(json, arg string) string{ "reverse": modReverse,
"pretty": modPretty, "this": modThis,
"ugly": modUgly, "flatten": modFlatten,
"reverse": modReverse, "join": modJoin,
"this": modThis, "valid": modValid,
"flatten": modFlatten, "keys": modKeys,
"join": modJoin, "values": modValues,
"valid": modValid, "tostr": modToStr,
"keys": modKeys, "fromstr": modFromStr,
"values": modValues, "group": modGroup,
"tostr": modToStr,
"fromstr": modFromStr,
"group": modGroup,
"dig": modDig,
}
} }
// AddModifier binds a custom modifier command to the GJSON syntax. // AddModifier binds a custom modifier command to the GJSON syntax.
@ -3447,30 +3435,3 @@ func escapeComp(comp string) string {
} }
return comp return comp
} }
func parseRecursiveDescent(all []Result, parent Result, path string) []Result {
if res := parent.Get(path); res.Exists() {
all = append(all, res)
}
if parent.IsArray() || parent.IsObject() {
parent.ForEach(func(_, val Result) bool {
all = parseRecursiveDescent(all, val, path)
return true
})
}
return all
}
func modDig(json, arg string) string {
all := parseRecursiveDescent(nil, Parse(json), arg)
var out []byte
out = append(out, '[')
for i, res := range all {
if i > 0 {
out = append(out, ',')
}
out = append(out, res.Raw...)
}
out = append(out, ']')
return string(out)
}

View File

@ -2636,68 +2636,3 @@ func TestIssue301(t *testing.T) {
assert(t, Get(json, `fav\.movie.[1]`).String() == "[]") assert(t, Get(json, `fav\.movie.[1]`).String() == "[]")
} }
func TestModDig(t *testing.T) {
json := `
{
"group": {
"issues": [
{
"fields": {
"labels": [
"milestone_1",
"group:foo",
"plan:a",
"plan:b"
]
},
"refid": "123"
},{
"fields": {
"labels": [
"milestone_2",
"group:foo",
"plan:a",
"plan"
]
},
"refid": "456"
},[
{"extra_deep":[{
"fields": {
"labels": [
"milestone_3",
"group:foo",
"plan:a",
"plan"
]
},
"refid": "789"
}]
}]
]
}
}
`
assert(t, Get(json, "group.@dig:#(refid=123)|0.fields.labels.0").String() == "milestone_1")
assert(t, Get(json, "group.@dig:#(refid=456)|0.fields.labels.0").String() == "milestone_2")
assert(t, Get(json, "group.@dig:#(refid=789)|0.fields.labels.0").String() == "milestone_3")
json = `
{ "something": {
"anything": {
"abcdefg": {
"finally": {
"important": {
"secret": "password",
"name": "jake"
}
},
"name": "melinda"
}
}
}
}`
assert(t, Get(json, "@dig:name").String() == `["melinda","jake"]`)
assert(t, Get(json, "@dig:secret").String() == `["password"]`)
}