Skip to content

Commit

Permalink
interp: Add stderr again for jq compat
Browse files Browse the repository at this point in the history
Also add tests for print* and stderr
  • Loading branch information
wader committed Jan 7, 2022
1 parent 2c3e411 commit a3cfcd0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/interp/internal.jq
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@ def println: ., "\n" | print;
def printerr: tostring | _stderr;
def printerrln: ., "\n" | printerr;

# jq compat
def debug:
( ((["DEBUG", .] | tojson) | printerrln)
, .
);
def debug(f): . as $c | f | debug | $c;
# jq compat, output to compact json to stderr and let input thru
def stderr:
( (tojson | printerr)
, .
);

def _global_var($k): _global_state[$k];
def _global_var($k; f): _global_state(_global_state | .[$k] |= f) | .[$k];
Expand Down
29 changes: 29 additions & 0 deletions pkg/interp/testdata/debug.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,32 @@ null> ^D
stderr:
["DEBUG",{"a":123,"b":[]}]
["DEBUG",{"a":123}]
$ fq -n 'null, {a:1}, "abc" | ., "\n" | print'
null
{"a":1}
abc
$ fq -n 'null, {a:1}, "abc" | println'
null
{"a":1}
abc
$ fq -n 'null, {a:1}, "abc" | ., "\n" | printerr'
stderr:
null
{"a":1}
abc
$ fq -n 'null, {a:1}, "abc" | printerrln'
stderr:
null
{"a":1}
abc
$ fq -n 'null, {a:1}, "abc" | ., "\n" | stderr'
null
"\n"
{
"a": 1
}
"\n"
"abc"
"\n"
stderr:
null"\n"{"a":1}"\n""abc""\n"

0 comments on commit a3cfcd0

Please sign in to comment.