summaryrefslogtreecommitdiffstats
path: root/tests/modules
diff options
context:
space:
mode:
authorNicolas Williams <nico@cryptonector.com>2015-06-26 20:15:04 -0500
committerNicolas Williams <nico@cryptonector.com>2015-06-26 20:17:02 -0500
commit5108a451caf0054f4ff6c1e900072e0eb41d894f (patch)
tree1b492fd079249206001e83722f8a9b0420a2162a /tests/modules
parentec7647c4ab1c50d7f45c8ff9fd73917af867e0be (diff)
Alternative implementation of tovalues
Diffstat (limited to 'tests/modules')
-rw-r--r--tests/modules/streaming.jq46
1 files changed, 27 insertions, 19 deletions
diff --git a/tests/modules/streaming.jq b/tests/modules/streaming.jq
index 0dafc0cd..4e2909e0 100644
--- a/tests/modules/streaming.jq
+++ b/tests/modules/streaming.jq
@@ -1,41 +1,49 @@
# Filter and adjust streamed values so that only values from the .th
# level are output.
-def trunc(inputs):
- . as $n | inputs | . as $input | if (.[0]|length) > 1 then setpath([0];$input[0][1:]) else empty end;
+def trunc(stream):
+ . as $n | stream | . as $input | if (.[0]|length) > $n then setpath([0];$input[0][$n:]) else empty end;
# Reduce streamed values back to normal
def tovalues(i):
+ def debug(msg): . as $dot | [msg, .] | debug | $dot;
foreach i as $item (
- # [<current value being built>,
- # <is current value valid?>,
- # <previous, complete value>,
- # <is previous value valid?>]
- [null,false,null,false];
+ [null,false,null];
# Updator
#
# If the new $item is a top-level value,
# then clear out the current value
- if ($item[0]|length) == 0 then [null,false,.[2],.[3]]
- # else if the new $item terminates the current value,
- # then rotate the current value into the previous value slot.
- elif ($item|length) == 1 and ($item[0]|length) < 2 then [null,false,.[0],.[1]]
- else . end |
- . as $state |
+ . as [$cur, $cur_isvalid, $prev] |
+ $item as [$path, $leaf] |
+ ($item|length > 1) as $has_leaf |
+ ($item|length == 1) as $closing |
+ ($path|length) as $plen |
+ # if the new $item terminates the current value, then cur is ready
+ # for extraction and we'll start building a new value with the next
+ # inputs
+ if ($plen == 0) or # top-level scalar
+ ($closing and $plen < 2) then [null,false,$cur]
+ # else continue building up cur
+ else . end |
+ . as [$cur, $cur_isvalid, $prev] |
# If the new $item has a leaf, upate the current value
- if ($item|length) > 1 and ($item[0]|length) > 0 then
- [.[0]|setpath(($item|.[0]); ($item|.[1])), # update current value
+ if $has_leaf and $plen > 0 then
+ [$cur|setpath(($path); $leaf), # update current value
true, # current value is now valid (if, perhaps, incomplete)
- $state[2], # previous value is unchanged
- $state[3]] # previous value is unchanged
+ $prev] # previous value is unchanged
else .
end;
# Extractor
#
+ . as [$cur, $cur_isvalid, $prev] |
+ $item as [$path, $leaf] |
+ ($item|length > 1) as $has_leaf |
+ ($item|length == 1) as $closing |
+ ($path|length) as $plen |
# If previous value is valid, output it
- if ($item[0]|length) == 1 and ($item|length == 1) and .[3] then .[2] else empty end,
+ if $plen == 1 and $closing then $prev else empty end,
# and/or if the new $item is a top-level scalar, output it
- if ($item[0]|length) == 0 then $item[1] else empty end
+ if $plen == 0 then $leaf else empty end
);