Skip to content

Commit

Permalink
fix: support non-json input files in jq
Browse files Browse the repository at this point in the history
  • Loading branch information
kormide committed Jun 17, 2022
1 parent b3c2884 commit ce015ca
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 3 deletions.
19 changes: 18 additions & 1 deletion docs/jq.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 18 additions & 1 deletion lib/jq.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,28 @@ def jq(name, srcs, filter = None, filter_file = None, args = [], out = None, **k
args = ["--slurp"],
out = "foobar.json",
)
# Convert genquery output to json
genquery(
name = "deps",
expression = "deps(//some:target)",
scope = ["//some:target"],
)
jq(
name = "deps_json",
srcs = [":deps"],
args = [
"--raw-input",
"--slurp",
],
filter = "{ deps: split(\"\\n\") | map(select(. | length > 0)) }",
)
```
Args:
name: Name of the rule
srcs: List of input json files
srcs: List of input files
filter: Filter expression (https://stedolan.github.io/jq/manual/#Basicfilters)
filter_file: File containing filter expression (alternative to `filter`)
args: Additional args to pass to jq
Expand Down
2 changes: 1 addition & 1 deletion lib/private/jq.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

_jq_attrs = {
"srcs": attr.label_list(
allow_files = [".json"],
allow_files = True,
mandatory = True,
allow_empty = True,
),
Expand Down
17 changes: 17 additions & 0 deletions lib/tests/jq/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,20 @@ diff_test(
file1 = "genrule_output.json",
file2 = "a_pretty.json",
)

# Raw input (--raw-input / -R)
jq(
name = "case_raw_input",
srcs = ["raw.txt"],
args = [
"--raw-input",
"--slurp",
],
filter = "split(\"\\n\")",
)

diff_test(
name = "case_raw_input_test",
file1 = ":case_raw_input",
file2 = "raw_expected.json",
)
4 changes: 4 additions & 0 deletions lib/tests/jq/raw.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
foo
bar
moo
cow
6 changes: 6 additions & 0 deletions lib/tests/jq/raw_expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
"foo",
"bar",
"moo",
"cow"
]

0 comments on commit ce015ca

Please sign in to comment.