Skip to content

Commit

Permalink
Fix deletion using assigning empty against arrays (fix jqlang#2051)
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed Jul 6, 2023
1 parent a881532 commit 559b737
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/builtin.jq
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,24 @@ def add: reduce .[] as $x (null; . + $x);
def del(f): delpaths([path(f)]);
def _assign(paths; $value): reduce path(paths) as $p (.; setpath($p; $value));
def _modify(paths; update):
reduce path(paths) as $p (.;
reduce path(paths) as $p ([., []];
. as $dot
| null
| label $out
| ($dot | getpath($p)) as $v
| ($dot[0] | getpath($p)) as $v
| (
( $$$$v
| update
| (., break $out) as $v
| $$$$dot
| setpath($p; $v)
| setpath([0] + $p; $v)
),
(
$$$$dot
| delpaths([$p])
| setpath([1]; .[1] + [$p])
)
)
);
) | . as $dot | $dot[0] | delpaths($dot[1]);
def map_values(f): .[] |= f;

# recurse
Expand Down
5 changes: 5 additions & 0 deletions tests/jq.test
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,11 @@ def inc(x): x |= .+1; inc(.[].a)
{"a":[{"b":5}]}
{"a":[{"c":3,"b":5}]}

# #2051, deletion using assigning empty against arrays
(.[] | select(. >= 2)) |= empty
[1,5,3,0,7]
[1,0]

.[2][3] = 1
[4]
[4, null, [null, null, null, 1]]
Expand Down

0 comments on commit 559b737

Please sign in to comment.