Fix non-existent response from multires query on empty array

This commit fixes an issue where a multires query on an empty
array will result in a non-existent (empty string) result.

For example `Get("[]", "#(key=value)#").Raw` resulted in an
empty string. But, it should actually result in the empty
array `[]`.
This commit is contained in:
tidwall 2021-03-25 15:31:00 -07:00
parent c75c954102
commit d6cb589fc4
2 changed files with 16 additions and 5 deletions

View File

@ -1406,7 +1406,6 @@ func parseArray(c *parseContext, i int, path string) (int, bool) {
} }
return false return false
} }
for i < len(c.json)+1 { for i < len(c.json)+1 {
if !rp.arrch { if !rp.arrch {
pmatch = partidx == h pmatch = partidx == h
@ -1608,10 +1607,17 @@ func parseArray(c *parseContext, i int, path string) (int, bool) {
c.calcd = true c.calcd = true
return i + 1, true return i + 1, true
} }
if len(multires) > 0 && !c.value.Exists() { if !c.value.Exists() {
c.value = Result{ if len(multires) > 0 {
Raw: string(append(multires, ']')), c.value = Result{
Type: JSON, Raw: string(append(multires, ']')),
Type: JSON,
}
} else if rp.query.all {
c.value = Result{
Raw: "[]",
Type: JSON,
}
} }
} }
return i + 1, false return i + 1, false

View File

@ -2233,3 +2233,8 @@ func TestFlattenRemoveNonExist(t *testing.T) {
raw := Get("[[1],[2,[[],[3]],[4,[5],[],[[[6]]]]]]", `@flatten:{"deep":true}`).Raw raw := Get("[[1],[2,[[],[3]],[4,[5],[],[[[6]]]]]]", `@flatten:{"deep":true}`).Raw
assert(t, raw == "[1,2,3,4,5,6]") assert(t, raw == "[1,2,3,4,5,6]")
} }
func TestPipeEmptyArray(t *testing.T) {
raw := Get("[]", `#(hello)#`).Raw
assert(t, raw == "[]")
}