Skip to content
Myr

Make |= delete LHS when RHS is empty (Fix #1314)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Now that #1313 is fixed, |= no longer outputs null when the RHS update expression outputs empty. When a user wants to keep the current value of the LHS they would have the RHS update expression output `.`, so having `empty` achieve the same thing would be redundant. The obvious thing to do is to delete the LHS when the RHS update outputs `empty` (i.e., doesn't output any values). It's reasonable to think that existing programs won't be broken by this change, because reduce and |= not handling empty well is clearly a bug. (Though it's possible that some programs were using empty to quickly terminate reduce or |=, it's not likely. They should use label/break instead.) Prior to this change |= would use the _last_ value output by the RHS update expression. With this change |= will use the _first_ value instead. This change _is_ a minor backwards-incompatible change. It may or may not be acceptable; we'll see. It is a useful change in that it makes |= faster when the update expression produces multiple values.

authored by Nicolas Williams · committed by Nicolas Williams · parent aac8132 · Browse files · 3 files+11 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+11 added−4 removed

  • other:+11 added−4 removed

3 files+11 added−4 removed

modified:docs/content/3.manual/manual.yml+6 added-2 removed
2816 unmodified lines
2817 Thus `$var.foo |= . + 1` won't work as expected (`$var.foo` is2818 not a valid or useful path expression in `.`); use `$var |2819 .foo |= . + 1` instead.28202821 If the right-hand side outputs multiple values, only the last2822 one will be used.2821 If the right-hand side outputs no values (i.e., `empty`), then2822 the left-hand side path will be deleted, as with `del(path)`.28232824 If the right-hand side outputs multiple values, only the first2825 one will be used (NOTE: it used to be that only the last one was2826 used).28272828 examples:28292830 - program: '(..|select(type=="boolean")) |= if . then 1 else 0 end'2831 input: '[true,false,[5,true,[true,[false]],false]]'
174 unmodified lines
modified:src/builtin.jq+1 added-1 removed
8 unmodified lines
9def min_by(f): _min_by_impl(map([f]));10def add: reduce .[] as $x (null; . + $x);11def del(f): delpaths([path(f)]);12def _assign(paths; value): value as $v | reduce path(paths) as $p (.; setpath($p; $v));13def _modify(paths; update): reduce path(paths) as $p (.; setpath($p; getpath($p) | update));13def _modify(paths; update): reduce path(paths) as $p (.; label $out | (setpath($p; getpath($p) | update) | ., break $out), delpaths([$p]));14def map_values(f): .[] |= f;1516# recurse17def recurse(f): def r: ., (f | r); r;
289 unmodified lines
modified:jq.1.prebuilt+4 added-1 removed
3031 unmodified lines
3032.P3033Note that the left\-hand side of \'|=\' refers to a value in \fB\.\fR\. Thus \fB$var\.foo |= \. + 1\fR won\'t work as expected (\fB$var\.foo\fR is not a valid or useful path expression in \fB\.\fR); use \fB$var | \.foo |= \. + 1\fR instead\.3034.3035.P3036If the right\-hand side outputs multiple values, only the last one will be used\.3036If the right\-hand side outputs no values (i\.e\., \fBempty\fR), then the left\-hand side path will be deleted, as with \fBdel(path)\fR\.3037.3038.P3039If the right\-hand side outputs multiple values, only the first one will be used (NOTE: it used to be that only the last one was used)\.3040.3041.IP "" 43042.3043.nf3044
172 unmodified lines