Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple and efficient implementation of walk/1 #2795

Merged
merged 1 commit into from
Jul 31, 2023
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
15 changes: 8 additions & 7 deletions src/builtin.jq
Original file line number Diff line number Diff line change
Expand Up @@ -246,13 +246,14 @@ def bsearch($target):

# Apply f to composite entities recursively, and to atoms
def walk(f):
. as $in
| if type == "object" then
reduce keys_unsorted[] as $key
( {}; . + { ($key): ($in[$key] | walk(f)) } ) | f
elif type == "array" then map( walk(f) ) | f
else f
end;
def w:
if type == "object"
then map_values(w)
elif type == "array" then map(w)
else .
end
| f;
w;

# pathexps could be a stream of dot-paths
def pick(pathexps):
Expand Down
19 changes: 19 additions & 0 deletions tests/jq.test
Original file line number Diff line number Diff line change
Expand Up @@ -1968,3 +1968,22 @@ implode|explode
map(try implode catch .)
[123,["a"],[nan]]
["implode input must be an array","string (\"a\") can't be imploded, unicode codepoint needs to be numeric","number (null) can't be imploded, unicode codepoint needs to be numeric"]

# walk
walk(.)
{"x":0}
{"x":0}

walk(1)
{"x":0}
1

# The following is a regression test, not a requirement:
[walk(.,1)]
{"x":0}
[{"x":0},1]

# Issue #2584
walk(select(IN({}, []) | not))
{"a":1,"b":[]}
{"a":1}