fix: support non-json input files in jq

This commit is contained in:
Derek Cormier 2022-06-17 09:46:11 -07:00
parent b3c2884bdf
commit ce015ca4f3
6 changed files with 64 additions and 3 deletions

19
docs/jq.md generated
View File

@ -68,6 +68,23 @@ jq(
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)) }",
)
```
@ -77,7 +94,7 @@ jq(
| Name | Description | Default Value |
| :------------- | :------------- | :------------- |
| <a id="jq-name"></a>name | Name of the rule | none |
| <a id="jq-srcs"></a>srcs | List of input json files | none |
| <a id="jq-srcs"></a>srcs | List of input files | none |
| <a id="jq-filter"></a>filter | Filter expression (https://stedolan.github.io/jq/manual/#Basicfilters) | <code>None</code> |
| <a id="jq-filter_file"></a>filter_file | File containing filter expression (alternative to <code>filter</code>) | <code>None</code> |
| <a id="jq-args"></a>args | Additional args to pass to jq | <code>[]</code> |

View File

@ -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

View File

@ -2,7 +2,7 @@
_jq_attrs = {
"srcs": attr.label_list(
allow_files = [".json"],
allow_files = True,
mandatory = True,
allow_empty = True,
),

View File

@ -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
lib/tests/jq/raw.txt Normal file
View File

@ -0,0 +1,4 @@
foo
bar
moo
cow

View File

@ -0,0 +1,6 @@
[
"foo",
"bar",
"moo",
"cow"
]