fix: support non-json input files in jq
This commit is contained in:
parent
b3c2884bdf
commit
ce015ca4f3
|
@ -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> |
|
||||
|
|
19
lib/jq.bzl
19
lib/jq.bzl
|
@ -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
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
_jq_attrs = {
|
||||
"srcs": attr.label_list(
|
||||
allow_files = [".json"],
|
||||
allow_files = True,
|
||||
mandatory = True,
|
||||
allow_empty = True,
|
||||
),
|
||||
|
|
|
@ -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",
|
||||
)
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
foo
|
||||
bar
|
||||
moo
|
||||
cow
|
|
@ -0,0 +1,6 @@
|
|||
[
|
||||
"foo",
|
||||
"bar",
|
||||
"moo",
|
||||
"cow"
|
||||
]
|
Loading…
Reference in New Issue