From ce015ca4f373333c3effd2504036727c20de775f Mon Sep 17 00:00:00 2001 From: Derek Cormier Date: Fri, 17 Jun 2022 09:46:11 -0700 Subject: [PATCH] fix: support non-json input files in jq --- docs/jq.md | 19 ++++++++++++++++++- lib/jq.bzl | 19 ++++++++++++++++++- lib/private/jq.bzl | 2 +- lib/tests/jq/BUILD.bazel | 17 +++++++++++++++++ lib/tests/jq/raw.txt | 4 ++++ lib/tests/jq/raw_expected.json | 6 ++++++ 6 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 lib/tests/jq/raw.txt create mode 100644 lib/tests/jq/raw_expected.json diff --git a/docs/jq.md b/docs/jq.md index 66a57d229..ba1d2d6a7 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 326ea282c..e274605a3 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 d52f73f0a..f40b9755a 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 401990ab4..0262a3579 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 000000000..dee551de0 --- /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 000000000..3798a64b9 --- /dev/null +++ b/lib/tests/jq/raw_expected.json @@ -0,0 +1,6 @@ +[ + "foo", + "bar", + "moo", + "cow" +]