Allow chaining multipaths using the dot

This commit is contained in:
tidwall 2021-03-22 04:19:47 -07:00
parent 7419d01876
commit d18e16d152
2 changed files with 32 additions and 11 deletions

View File

@ -731,8 +731,13 @@ func parseArrayPath(path string) (r arrayPathResult) {
} }
if path[i] == '.' { if path[i] == '.' {
r.part = path[:i] r.part = path[:i]
r.path = path[i+1:] if !r.arrch && i < len(path)-1 && isDotPiperChar(path[i+1]) {
r.more = true r.pipe = path[i+1:]
r.piped = true
} else {
r.path = path[i+1:]
r.more = true
}
return return
} }
if path[i] == '#' { if path[i] == '#' {
@ -978,6 +983,11 @@ right:
return s return s
} }
// peek at the next byte and see if it's a '@', '[', or '{'.
func isDotPiperChar(c byte) bool {
return !DisableModifiers && (c == '@' || c == '[' || c == '{')
}
type objectPathResult struct { type objectPathResult struct {
part string part string
path string path string
@ -996,12 +1006,8 @@ func parseObjectPath(path string) (r objectPathResult) {
return return
} }
if path[i] == '.' { if path[i] == '.' {
// peek at the next byte and see if it's a '@', '[', or '{'.
r.part = path[:i] r.part = path[:i]
if !DisableModifiers && if i < len(path)-1 && isDotPiperChar(path[i+1]) {
i < len(path)-1 &&
(path[i+1] == '@' ||
path[i+1] == '[' || path[i+1] == '{') {
r.pipe = path[i+1:] r.pipe = path[i+1:]
r.piped = true r.piped = true
} else { } else {
@ -1031,14 +1037,11 @@ func parseObjectPath(path string) (r objectPathResult) {
continue continue
} else if path[i] == '.' { } else if path[i] == '.' {
r.part = string(epart) r.part = string(epart)
// peek at the next byte and see if it's a '@' modifier if i < len(path)-1 && isDotPiperChar(path[i+1]) {
if !DisableModifiers &&
i < len(path)-1 && path[i+1] == '@' {
r.pipe = path[i+1:] r.pipe = path[i+1:]
r.piped = true r.piped = true
} else { } else {
r.path = path[i+1:] r.path = path[i+1:]
r.more = true
} }
r.more = true r.more = true
return return

View File

@ -2204,3 +2204,21 @@ func TestVariousFuzz(t *testing.T) {
Get(testJSON, testJSON) Get(testJSON, testJSON)
} }
func TestSubpathsWithMultipaths(t *testing.T) {
const json = `
[
{"a": 1},
{"a": 2, "values": ["a", "b", "c", "d", "e"]},
true,
["a", "b", "c", "d", "e"],
4
]
`
assert(t, Get(json, `1.values.@ugly`).Raw == `["a","b","c","d","e"]`)
assert(t, Get(json, `1.values.[0,3]`).Raw == `["a","d"]`)
assert(t, Get(json, `3.@ugly`).Raw == `["a","b","c","d","e"]`)
assert(t, Get(json, `3.[0,3]`).Raw == `["a","d"]`)
assert(t, Get(json, `#.@ugly`).Raw == `[{"a":1},{"a":2,"values":["a","b","c","d","e"]},true,["a","b","c","d","e"],4]`)
assert(t, Get(json, `#.[0,3]`).Raw == `[[],[],[],["a","d"],[]]`)
}