Skip to content

Commit

Permalink
repl: use map in _query_slurp_wrap
Browse files Browse the repository at this point in the history
  • Loading branch information
wader committed Sep 12, 2021
1 parent 538f4ff commit ccf6cab
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
22 changes: 17 additions & 5 deletions pkg/interp/query.jq
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ def _query_iter:
}
};

def _query_func($name; $args):
{
"term": {
"func": {
"args": $args,
"name": $name
},
"type": "TermTypeFunc"
}
};

def _query_func($name):
_query_func($name; null);

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

Expand Down Expand Up @@ -65,17 +79,15 @@ def _query_transform_last(f):
end;
_f;

# <filter...> | <slurp_func> -> [.[] | <filter...> | .] | (<slurp_func> | f)
# <filter...> | <slurp_func> -> map(<filter...> | .) | (<slurp_func> | f)
def _query_slurp_wrap(f):
# save and move directives to new root query
( . 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_func("map"; [$pipe])
| _query_pipe($lq | f)
| .meta = $meta
| .imports = $imports
Expand Down
24 changes: 12 additions & 12 deletions pkg/interp/query_test.jq
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ include "query";
)
,
([
["", "[.[] | .] | ."],
[".", "[.[] | .] | ."],
["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"],
["", "map(.) | ."],
[".", "map(.) | ."],
["a", "map(.) | a"],
["1, 2", "map(.) | 1, 2"],
["1 | 2", "map(1 | .) | 2"],
["1 | 2 | 3", "map(1 | 2 | .) | 3"],
["(1 | 2) | 3", "map((1 | 2) | .) | 3"],
["1 | (2 | 3)", "map(1 | .) | (2 | 3)"],
["1 as $_ | 2", "map(1 as $_ | .) | 2"],
["def f: 1; 1", "map(.) | def f: 1; 1"],
["def f: 1; 1 | 2", "map(def f: 1; 1 | .) | 2"],
["module {a:1};\ninclude \"a\";\n1", "module { a: 1 };\ninclude \"a\";\nmap(.) | 1"],
empty
][] | assert(
"\(.) | _query_slurp_wrap";
Expand Down

0 comments on commit ccf6cab

Please sign in to comment.