Skip to content

Commit eeebe07

Browse files
committed
improve definition and tests of fromstream function
1 parent a09589e commit eeebe07

File tree

3 files changed

+33
-19
lines changed

3 files changed

+33
-19
lines changed

builtin.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

builtin.jq

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,17 @@ def truncate_stream(f):
9696
. as $n | null | f |
9797
if .[0] | length > $n then .[0] |= .[$n:] else empty end;
9898
def fromstream(f):
99-
{ x: null, e: false } as $init |
100-
foreach f as $i (
101-
$init;
102-
if .e then $init end |
103-
if $i | length == 2 then
104-
setpath(["e"]; $i[0] | length == 0) |
105-
setpath(["x"] + $i[0]; $i[1])
99+
foreach f as $pv (
100+
null;
101+
if .e then null end |
102+
$pv as [$p, $v] |
103+
if $pv | length == 2 then
104+
setpath(["v"] + $p; $v) |
105+
setpath(["e"]; $p | length == 0)
106106
else
107-
setpath(["e"]; $i[0] | length == 1)
107+
setpath(["e"]; $p | length == 1)
108108
end;
109-
if .e then .x else empty end
109+
if .e then .v else empty end
110110
);
111111
def tostream:
112112
path(def r: (.[]? | r), .; r) as $p |

cli/test.yaml

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4479,13 +4479,16 @@
44794479
- name: fromstream function
44804480
args:
44814481
- -c
4482-
- 'fromstream(0,1,2|truncate_stream([[0],1],[[1,0],2],[[1,0]],[[1]]))'
4482+
- 'fromstream(range(5) | truncate_stream({x:[1,{y:[2]},3],z:[4]} | tostream))'
44834483
input: 'null'
44844484
expected: |
4485-
[1,[2]]
4485+
{"x":[1,{"y":[2]},3],"z":[4]}
4486+
[1,{"y":[2]},3]
4487+
[4]
4488+
{"y":[2]}
44864489
[2]
44874490
4488-
- name: stream function
4491+
- name: tostream function
44894492
args:
44904493
- -c
44914494
- 'tostream'
@@ -5975,17 +5978,28 @@
59755978
[["a",2]]
59765979
[["a"]]
59775980
5978-
- name: inputs function with stream option and truncate_stream function
5981+
- name: inputs function with stream option and fromstream function
59795982
args:
59805983
- -n
5984+
- -c
59815985
- --stream
5982-
- 'fromstream(1 | truncate_stream(inputs) | select(length > 1) | .[0] |= .[1:])'
5983-
input: '{"a":[1,true,null,"x"]}'
5986+
- 'fromstream(inputs)'
5987+
input: 'null [false] {"x":[null]}'
59845988
expected: |
5985-
1
5986-
true
59875989
null
5988-
"x"
5990+
[false]
5991+
{"x":[null]}
5992+
5993+
- name: inputs function with stream option and truncate_stream function
5994+
args:
5995+
- -n
5996+
- -c
5997+
- --stream
5998+
- 'fromstream(1 | truncate_stream(inputs))'
5999+
input: '{"x":[1,{"y":[2]},3],"z":[4]}'
6000+
expected: |
6001+
[1,{"y":[2]},3]
6002+
[4]
59896003
59906004
- name: inputs function with stream option
59916005
args:

0 commit comments

Comments
 (0)