Skip to content

Commit

Permalink
repl: Handle directives, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wader committed Sep 12, 2021
1 parent 49f541c commit 1d0ebb5
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 37 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ fq:
# figure out all go pakges with test files
test: PKGS=$(shell find . -name "*_test.go" | xargs -n 1 dirname | sort | uniq)
test: jqtest
go test ${COVER} ${PKGS}
go test ${VERBOSE} ${COVER} ${PKGS}

testwrite: export WRITE_ACTUAL=1
testwrite: test

testv: export VERBOSE=-v
testv: test

cover: COVER=-cover -race -coverpkg=./... -coverprofile=cover.out
cover: test
go tool cover -html=cover.out -o cover.out.html
Expand Down
3 changes: 2 additions & 1 deletion pkg/interp/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,12 @@ func (i *Interp) queryFromString(c interface{}, a []interface{}) interface{} {
pos: p,
}
}

// TODO: use mapstruct?
b, err := json.Marshal(q)
if err != nil {
return err
}

var v interface{}
if err := json.Unmarshal(b, &v); err != nil {
return err
Expand Down
62 changes: 27 additions & 35 deletions pkg/interp/query.jq
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ def _query_array:
};

# a() -> b()
def _query_func_rename(name): .term.func.name = name;
def _query_func_rename(name):
.term.func.name = name;

# . | r
def _query_pipe(r):
Expand All @@ -31,60 +32,51 @@ def _query_iter:
}
};

def _query_is_func(name):
.term.func.name == name;

# last query in pipeline
def _query_pipe_last:
if .term then
( . as $t
| .term
| if .suffix_list then
( .suffix_list[-1]
| if .bind.body then (.bind.body | _query_pipe_last)
else .
end
if .term.suffix_list then
( .term.suffix_list[-1]
| if .bind.body then
( .bind.body
| _query_pipe_last
)
else $t
end
)
elif .op == "|" then (.right | _query_pipe_last)
else .
elif .op == "|" then
( .right
| _query_pipe_last
)
end;

def _query_is_func(name): .term.func.name == name;

def _query_replace_last(f):
# TODO: hack TCO bug
def _query_transform_last(f):
def _f:
if .term.suffix_list then
.term.suffix_list[-1] |=
if .bind.body then (.bind.body |= _f)
if .bind.body then
.bind.body |= _f
else f
end
elif .term then f
elif .op == "|" then (.right |= _f)
elif .op == "|" then
.right |= _f
else f
end;
_f;

def _query_find(f):
( if f then . else empty end
, if .op == "|" or .op == "," then
( (.left | _query_find(f))
, (.right | _query_find(f))
)
elif .term.suffix_list then
( .term.suffix_list
| map(.bind.body | _query_find(f))
)
else empty
end
);

# <filter...> | <slurp_func> -> [.[] | <filter...> | .] | (<slurp_func> | f)
def _query_slurp_wrap(f):
( _query_pipe_last as $lq
| _query_replace_last(_query_ident) as $pipe
( . as {$meta, $imports}
# move directives new root query
| del(.meta)
| del(.imports)
| _query_pipe_last as $lq
| _query_transform_last(_query_ident) as $pipe
| _query_iter
| _query_pipe($pipe)
| _query_array
| _query_pipe($lq | f)
| .meta = $meta
| .imports = $imports
);
45 changes: 45 additions & 0 deletions pkg/interp/query_test.jq
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
include "assert";
include "query";

(
([
["", "."],
[".", "."],
["a", "a"],
["1, 2", "1, 2"],
["1 | 2", "2"],
["1 | 2 | 3", "3"],
["(1 | 2) | 3", "3"],
["1 | (2 | 3)", "(2 | 3)"],
["1 as $_ | 2", "2"],
["def f: 1; 1", "def f: 1; 1"],
["def f: 1; 1 | 2", "2"],
empty
][] | assert(
"\(.) | _query_pipe_last";
.[1];
.[0] | _query_fromstring | _query_pipe_last | _query_tostring
)
)
,
([
["", "[.[] | .] | ."],
[".", "[.[] | .] | ."],
["a", "[.[] | .] | a"],
["1, 2", "[.[] | .] | 1, 2"],
["1 | 2", "[.[] | 1 | .] | 2"],
["1 | 2 | 3", "[.[] | 1 | 2 | .] | 3"],
["(1 | 2) | 3", "[.[] | (1 | 2) | .] | 3"],
["1 | (2 | 3)", "[.[] | 1 | .] | (2 | 3)"],
["1 as $_ | 2", "[.[] | 1 as $_ | .] | 2"],
["def f: 1; 1", "[.[] | .] | def f: 1; 1"],
["def f: 1; 1 | 2", "[.[] | def f: 1; 1 | .] | 2"],
["module {};\ninclude \"a\";\n1", "module {};\ninclude \"a\";\n[.[] | .] | 1"],
empty
][] | assert(
"\(.) | _query_slurp_wrap";
.[1];
.[0] | _query_fromstring | _query_slurp_wrap(.) | _query_tostring
)
)
)
5 changes: 5 additions & 0 deletions pkg/interp/testdata/repl.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ null> 1,2,3 | repl
2
3
> number, [3]> ^D
null> def f: 1; f,f | repl
> number, [2]> .
1
1
> number, [2]> ^D
null> [1,2,3] | repl
> [number, ...][3]> .
[
Expand Down

0 comments on commit 1d0ebb5

Please sign in to comment.