From f0551fab757d2d12b74d024dabea174f34fb2b9e Mon Sep 17 00:00:00 2001 From: itchyny Date: Fri, 29 May 2020 17:17:12 +0900 Subject: [PATCH] Fix deletion using assigning empty against arrays (fix #2051) --- src/builtin.jq | 5 ++++- tests/jq.test | 5 +++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/builtin.jq b/src/builtin.jq index ee78017609..ac677db8c6 100644 --- a/src/builtin.jq +++ b/src/builtin.jq @@ -11,7 +11,10 @@ def min_by(f): _min_by_impl(map([f])); 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 (.; label $out | (setpath($p; getpath($p) | update) | ., break $out), delpaths([$p])); +def _modify(ps; f): + reduce path(ps) as $p + ([., []]; label $out | (setpath([0] + $p; getpath([0] + $p) | f) | ., break $out), .[1] += [$p]) + | . as $x | $x[0] | delpaths($x[1]); def map_values(f): .[] |= f; # recurse diff --git a/tests/jq.test b/tests/jq.test index 2d5c36b887..7d525d8294 100644 --- a/tests/jq.test +++ b/tests/jq.test @@ -1037,6 +1037,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]]