Skip to content

Commit

Permalink
cli: Fix non-string variables and var(; f) variant to delete etc
Browse files Browse the repository at this point in the history
  • Loading branch information
wader committed Sep 12, 2021
1 parent 21bef18 commit 2d4eb9c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pkg/interp/funcs.jq
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def help:
) | println;

# valid jq identifer, start with alpha or underscore then zero or more alpha, num or underscore
def _is_ident: test("^[a-zA-Z_][a-zA-Z_0-9]*$");
def _is_ident: type == "string" and test("^[a-zA-Z_][a-zA-Z_0-9]*$");
# escape " and \
def _escape_ident: gsub("(?<g>[\\\\\"])"; "\\\(.g)");

Expand Down
10 changes: 5 additions & 5 deletions pkg/interp/interp.jq
Original file line number Diff line number Diff line change
Expand Up @@ -257,14 +257,14 @@ def inputs:
def input_filename: _input_filename;

def var: _variables;
def var($k):
def var($k; f):
( . as $c
| if ($k | _is_ident | not) then error("invalid variable name \"\($k)\"") end
| _variables(
.[$k] |= ($c | if . == null then empty end)
)
| if ($k | _is_ident | not) then error("invalid variable name: \($k)") end
| _variables(.[$k] |= f)
| empty
);
def var($k): . as $c | var($k; $c);


# . will have additional array of options taking priority
# NOTE: is used by go Options()
Expand Down
8 changes: 5 additions & 3 deletions pkg/interp/testdata/var.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ null> var
}
null> $a
"aa"
null> null | var("a")
null> var("a"; empty)
null> $a
error: src:1:0: variable not defined: $a
null> var
Expand All @@ -28,11 +28,13 @@ null> . | repl
> null> $bb
"bb"
> null> ^D
null> null | var("bb")
null> var("bb"; empty)
null> var
{}
null> 123 | var("a b")
error: invalid variable name "a b"
error: invalid variable name: a b
null> 123 | var(null)
error: invalid variable name: null
null> ^D
$ fq -d mp3 -i . /test.mp3
mp3> .frames[0] | var("f")
Expand Down

0 comments on commit 2d4eb9c

Please sign in to comment.