feat: load jq filter from file

This commit is contained in:
Derek Cormier 2021-12-20 12:47:39 -08:00
parent 885fcb3f6e
commit e371656bc3
5 changed files with 60 additions and 14 deletions

View File

@ -7,7 +7,7 @@ Public API for jq
## jq
<pre>
jq(<a href="#jq-name">name</a>, <a href="#jq-srcs">srcs</a>, <a href="#jq-filter">filter</a>, <a href="#jq-args">args</a>, <a href="#jq-out">out</a>, <a href="#jq-kwargs">kwargs</a>)
jq(<a href="#jq-name">name</a>, <a href="#jq-srcs">srcs</a>, <a href="#jq-filter">filter</a>, <a href="#jq-filter_file">filter_file</a>, <a href="#jq-args">args</a>, <a href="#jq-out">out</a>, <a href="#jq-kwargs">kwargs</a>)
</pre>
Invoke jq with a filter on a set of json input files.
@ -50,6 +50,15 @@ jq(
""",
args = ["--slurp"],
)
# Load filter from a file
jq(
name = "merged",
srcs = ["foo.json", "bar.json"],
filter_file = "filter.txt",
args = ["--slurp"],
out = "foobar.json",
)
```
@ -60,9 +69,10 @@ jq(
| :------------- | :------------- | :------------- |
| <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-filter"></a>filter | mandatory jq filter specification (https://stedolan.github.io/jq/manual/#Basicfilters) | none |
| <a id="jq-args"></a>args | additional args to pass to jq | <code>[]</code> |
| <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> |
| <a id="jq-out"></a>out | Name of the output json file; defaults to the rule name plus ".json" | <code>None</code> |
| <a id="jq-kwargs"></a>kwargs | other common named parameters such as <code>tags</code> or <code>visibility</code> | none |
| <a id="jq-kwargs"></a>kwargs | Other common named parameters such as <code>tags</code> or <code>visibility</code> | none |

View File

@ -8,7 +8,7 @@ _jq_rule = rule(
toolchains = ["@aspect_bazel_lib//lib:jq_toolchain_type"],
)
def jq(name, srcs, filter, args = [], out = None, **kwargs):
def jq(name, srcs, filter = None, filter_file = None, args = [], out = None, **kwargs):
"""Invoke jq with a filter on a set of json input files.
For jq documentation, see https://stedolan.github.io/jq/.
@ -49,15 +49,25 @@ def jq(name, srcs, filter, args = [], out = None, **kwargs):
\"\"\",
args = ["--slurp"],
)
# Load filter from a file
jq(
name = "merged",
srcs = ["foo.json", "bar.json"],
filter_file = "filter.txt",
args = ["--slurp"],
out = "foobar.json",
)
```
Args:
name: Name of the rule
srcs: List of input json files
filter: mandatory jq filter specification (https://stedolan.github.io/jq/manual/#Basicfilters)
args: additional args to pass to jq
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
out: Name of the output json file; defaults to the rule name plus ".json"
**kwargs: other common named parameters such as `tags` or `visibility`
**kwargs: Other common named parameters such as `tags` or `visibility`
"""
if not out:
out = name + ".json"
@ -66,6 +76,7 @@ def jq(name, srcs, filter, args = [], out = None, **kwargs):
name = name,
srcs = srcs,
filter = filter,
filter_file = filter_file,
args = args,
out = out,
**kwargs

View File

@ -6,9 +6,8 @@ _jq_attrs = {
mandatory = True,
allow_empty = True,
),
"filter": attr.string(
mandatory = True,
),
"filter": attr.string(),
"filter_file": attr.label(allow_single_file = True),
"args": attr.string_list(),
"out": attr.output(mandatory = True),
}
@ -18,22 +17,32 @@ def _jq_impl(ctx):
out = ctx.outputs.out
args = ctx.attr.args
inputs = ctx.files.srcs[:]
if not ctx.attr.filter and not ctx.attr.filter_file:
fail("Must provide a filter or a filter_file")
if ctx.attr.filter and ctx.attr.filter_file:
fail("Cannot provide both a filter and a filter_file")
# jq hangs when there are no input sources unless --null-input flag is passed
if len(ctx.attr.srcs) == 0 and "-n" not in args and "--null-input" not in args:
args = args + ["--null-input"]
cmd = "{jq} {args} '{filter}' {sources} > {out}".format(
if ctx.attr.filter_file:
args = args + ["--from-file '%s'" % ctx.file.filter_file.path]
inputs.append(ctx.file.filter_file)
cmd = "{jq} {args} {filter} {sources} > {out}".format(
jq = jq_bin.path,
args = " ".join(args),
filter = ctx.attr.filter,
filter = "'%s'" % ctx.attr.filter if ctx.attr.filter else "",
sources = " ".join(["'%s'" % file.path for file in ctx.files.srcs]),
out = out.path,
)
ctx.actions.run_shell(
tools = [jq_bin],
inputs = ctx.files.srcs,
inputs = inputs,
outputs = [out],
command = cmd,
)

View File

@ -72,6 +72,20 @@ diff_test(
file2 = "null.json",
)
# Load filter from file
jq(
name = "case_filter_file",
srcs = ["a.json", "b.json"],
args = ["--slurp"],
filter_file = "merge_filter.txt",
)
diff_test(
name = "case_filter_file_test",
file1 = "a_b_merged.json",
file2 = ":case_filter_file",
)
# Call jq within a genrule
genrule(
name = "case_genrule",

View File

@ -0,0 +1,2 @@
# Merge the second object onto the first
.[0] * .[1]