Skip to content

Commit

Permalink
improve definition and tests of fromstream function
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed Sep 22, 2024
1 parent a09589e commit eeebe07
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 19 deletions.
2 changes: 1 addition & 1 deletion builtin.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions builtin.jq
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,17 @@ def truncate_stream(f):
. as $n | null | f |
if .[0] | length > $n then .[0] |= .[$n:] else empty end;
def fromstream(f):
{ x: null, e: false } as $init |
foreach f as $i (
$init;
if .e then $init end |
if $i | length == 2 then
setpath(["e"]; $i[0] | length == 0) |
setpath(["x"] + $i[0]; $i[1])
foreach f as $pv (
null;
if .e then null end |
$pv as [$p, $v] |
if $pv | length == 2 then
setpath(["v"] + $p; $v) |
setpath(["e"]; $p | length == 0)
else
setpath(["e"]; $i[0] | length == 1)
setpath(["e"]; $p | length == 1)
end;
if .e then .x else empty end
if .e then .v else empty end
);
def tostream:
path(def r: (.[]? | r), .; r) as $p |
Expand Down
32 changes: 23 additions & 9 deletions cli/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4479,13 +4479,16 @@
- name: fromstream function
args:
- -c
- 'fromstream(0,1,2|truncate_stream([[0],1],[[1,0],2],[[1,0]],[[1]]))'
- 'fromstream(range(5) | truncate_stream({x:[1,{y:[2]},3],z:[4]} | tostream))'
input: 'null'
expected: |
[1,[2]]
{"x":[1,{"y":[2]},3],"z":[4]}
[1,{"y":[2]},3]
[4]
{"y":[2]}
[2]
- name: stream function
- name: tostream function
args:
- -c
- 'tostream'
Expand Down Expand Up @@ -5975,17 +5978,28 @@
[["a",2]]
[["a"]]
- name: inputs function with stream option and truncate_stream function
- name: inputs function with stream option and fromstream function
args:
- -n
- -c
- --stream
- 'fromstream(1 | truncate_stream(inputs) | select(length > 1) | .[0] |= .[1:])'
input: '{"a":[1,true,null,"x"]}'
- 'fromstream(inputs)'
input: 'null [false] {"x":[null]}'
expected: |
1
true
null
"x"
[false]
{"x":[null]}
- name: inputs function with stream option and truncate_stream function
args:
- -n
- -c
- --stream
- 'fromstream(1 | truncate_stream(inputs))'
input: '{"x":[1,{"y":[2]},3],"z":[4]}'
expected: |
[1,{"y":[2]},3]
[4]
- name: inputs function with stream option
args:
Expand Down

0 comments on commit eeebe07

Please sign in to comment.