diff --git a/docs/jq.md b/docs/jq.md index 66a57d2..ba1d2d6 100644 --- a/docs/jq.md +++ b/docs/jq.md @@ -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 | | :------------- | :------------- | :------------- | | name | Name of the rule | none | -| srcs | List of input json files | none | +| srcs | List of input files | none | | filter | Filter expression (https://stedolan.github.io/jq/manual/#Basicfilters) | None | | filter_file | File containing filter expression (alternative to filter) | None | | args | Additional args to pass to jq | [] | diff --git a/lib/jq.bzl b/lib/jq.bzl index 326ea28..e274605 100644 --- a/lib/jq.bzl +++ b/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 diff --git a/lib/private/jq.bzl b/lib/private/jq.bzl index d52f73f..f40b975 100644 --- a/lib/private/jq.bzl +++ b/lib/private/jq.bzl @@ -2,7 +2,7 @@ _jq_attrs = { "srcs": attr.label_list( - allow_files = [".json"], + allow_files = True, mandatory = True, allow_empty = True, ), diff --git a/lib/tests/jq/BUILD.bazel b/lib/tests/jq/BUILD.bazel index 401990a..0262a35 100644 --- a/lib/tests/jq/BUILD.bazel +++ b/lib/tests/jq/BUILD.bazel @@ -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", +) diff --git a/lib/tests/jq/raw.txt b/lib/tests/jq/raw.txt new file mode 100644 index 0000000..dee551d --- /dev/null +++ b/lib/tests/jq/raw.txt @@ -0,0 +1,4 @@ +foo +bar +moo +cow \ No newline at end of file diff --git a/lib/tests/jq/raw_expected.json b/lib/tests/jq/raw_expected.json new file mode 100644 index 0000000..3798a64 --- /dev/null +++ b/lib/tests/jq/raw_expected.json @@ -0,0 +1,6 @@ +[ + "foo", + "bar", + "moo", + "cow" +]