From 371abc73ba97dfd49c3affa330236eb41150b8dd Mon Sep 17 00:00:00 2001 From: itchyny Date: Fri, 7 Jul 2023 13:54:27 +0900 Subject: [PATCH] Fix deletion using assigning empty against arrays (fix #2051) --- src/builtin.jq | 10 +++++----- tests/jq.test | 13 +++++++++++++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/builtin.jq b/src/builtin.jq index f9e5f1ea3c..bb7da93f42 100644 --- a/src/builtin.jq +++ b/src/builtin.jq @@ -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 diff --git a/tests/jq.test b/tests/jq.test index 308a4e6b02..a4b8d5fd74 100644 --- a/tests/jq.test +++ b/tests/jq.test @@ -1044,6 +1044,19 @@ 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] + +.[] |= select(. % 2 == 0) +[0,1,2,3,4,5] +[0,2,4] + +.foo[1,4,2,3] |= empty +{"foo":[0,1,2,3,4,5]} +{"foo":[0,5]} + .[2][3] = 1 [4] [4, null, [null, null, null, 1]]