builtin: add pick(stream)

pick(stream) works on both arrays and objects

Restrictions on the dot-paths are specified in manual.yml

See #2578
This commit is contained in:
pkoppstein authored and Nico Williams committed 2023-07-05 23:48:31 -05:00
1 parent a881532b29
commit 416e8ada7d
3 files changed
+32

No files matched your search

+18
View File
@@ -925,6 +925,24 @@ sections:
input: '{"a": 1, "b": 2, "c": 3}' input: '{"a": 1, "b": 2, "c": 3}'
output: ['{"a": 2, "b": 3, "c": 4}'] output: ['{"a": 2, "b": 3, "c": 4}']
- title: "`pick(stream)`"
body: |
Emit the projection of the input object or array defined by the specified stream of
dot-path specifications, such that if p is one of these specifications,
and `(. | p)` evaluates to $x, then `pick(stream) | p` will also be $x.
For arrays, negative indices and .[m:n] specifications should not be used.
examples:
- program: 'pick(.a, .b.c, .x)'
input: '{"a": 1, "b": {"c": 2, "d": 3}, "e": 4}'
output: ['{"a":1,"b":{"c":2},"x":null}']
- program: '[1,2,3,4] | pick(.[2], .[0], .[0])'
input: '[1,2,3,4]'
output: ['[1,null,3]']
- title: "`path(path_expression)`" - title: "`path(path_expression)`"
body: | body: |
+6
View File
@@ -260,6 +260,12 @@ def walk(f):
else f else f
end; end;
# stream should be a stream of dot-paths
def pick(stream):
. as $in
| reduce path(stream) as $a (null;
setpath($a; $in|getpath($a)) );
# SQL-ish operators here: # SQL-ish operators here:
def INDEX(stream; idx_expr): def INDEX(stream; idx_expr):
reduce stream as $row ({}; .[$row|idx_expr|tostring] = $row); reduce stream as $row ({}; .[$row|idx_expr|tostring] = $row);
+8
View File
@@ -219,6 +219,14 @@ map_values(.+1)
{"a": 1, "b": 2, "c": 3} {"a": 1, "b": 2, "c": 3}
{"a": 2, "b": 3, "c": 4} {"a": 2, "b": 3, "c": 4}
pick(.a, .b.c, .x)
{"a": 1, "b": {"c": 2, "d": 3}, "e": 4}
{"a":1,"b":{"c":2},"x":null}
[1,2,3,4] | pick(.[2], .[0], .[0])
[1,2,3,4]
[1,null,3]
path(.a[0].b) path(.a[0].b)
null null
["a",0,"b"] ["a",0,"b"]