Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
cases:
# verify fix for issue 7656
- note: walkbuiltin/array path not overwritten
query: data.generated.p = x
modules:
- |
package generated

x := {"a": [{"b": [{"c": [
{"aa": "AA"},
{"bb": "BB"},
{"cc": "CC"},
{"dd": "DDD"},
]}]}]}

p := {path: value |
[path, value] := walk(x)
count(value) == 2
}
data: {}
want_result:
- x:
"[\"a\",0,\"b\",0,\"c\",0,\"aa\"]": "AA"
"[\"a\",0,\"b\",0,\"c\",1,\"bb\"]": "BB"
"[\"a\",0,\"b\",0,\"c\",2,\"cc\"]": "CC"
15 changes: 12 additions & 3 deletions v1/topdown/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func walk(filter, path *ast.Array, input *ast.Term, iter func(*ast.Term) error)
pathCopy = ast.InternedEmptyArrayValue
} else {
// Shallow copy, as while the array is modified, the elements are not
pathCopy = path.Slice(0, path.Len())
pathCopy = copyShallow(path)
}

// TODO(ae): I'd *really* like these terms to be retrieved from a sync.Pool, and
Expand All @@ -48,8 +48,7 @@ func walk(filter, path *ast.Array, input *ast.Term, iter func(*ast.Term) error)
filter = filter.Slice(1, -1)
if key.IsGround() {
if term := input.Get(key); term != nil {
path = pathAppend(path, key)
return walk(filter, path, term, iter)
return walk(filter, pathAppend(path, key), term, iter)
}
return nil
}
Expand Down Expand Up @@ -149,6 +148,16 @@ func pathIsWildcard(operands []*ast.Term) bool {
return false
}

func copyShallow(arr *ast.Array) *ast.Array {
cpy := make([]*ast.Term, 0, arr.Len())

arr.Foreach(func(elem *ast.Term) {
cpy = append(cpy, elem)
})

return ast.NewArray(cpy...)
}

func init() {
RegisterBuiltinFunc(ast.WalkBuiltin.Name, evalWalk)
}