Familliar syntax and all of <nixpkgs/lib>
and builtins
in your hands.
Now going through jq
tutorial:
Identity filter
$ # jq
$ curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' | jq '.'
$ # nixq
$ curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' | nixq '_'
First element
$ # jq
$ ... | jq '.[0]'
$ # nixq, multiple versions
$ ... | nixq '_0'
$ ... | nixq 'elemAt _ 0'
$ ... | nixq 'head _'
Select several fields from first element, including some nested
$ # jq
$ jq '.[0] | {message: .commit.message, name: .commit.committer.name}'
$ # nixq
$ nixq 'with _0; { message = commit.message; name = commit.committer.name; }'
Same, but for all elements
$ # jq
$ jq '.[] | {message: .commit.message, name: .commit.committer.name}'
$ # nixq, unfortunately, can't do that in same fashion. But see next example
Same, but wrap result into a list, so it will be a valid JSON
$ # jq
$ jq '[.[] | {message: .commit.message, name: .commit.committer.name}]'
$ # nixq
$ nixq 'map (_: with _.commit; { message = message; name = committer.name; }) _'
Now the most complex expression from tutorial, don't know how to describe it in short
$ # jq
$ jq '[.[] | {message: .commit.message, name: .commit.committer.name, parents: [.parents[].html_url]}]'
$ # nixq
$ nixq 'map (_: {
message = _.commit.message;
name = _.commit.committer.name;
parents = map (_: _.html_url) _.parents;
}) _'
in $@
doesn't make much sense inside a string-assignment context in shell. It effectively gets coerced to$*
, but makes the reader scratch their heads trying to figure out if it's in a context where it could be expanded into multiple words first. :)...to be a little more clear about what I mean by multiple words...
runs the command:
...retaining the original element separators. In assigning a value to a single string, you can't have separators at all.
I'd strongly suggest requiring a query to be passed as exactly one string, and making any other usage an explicit error. That way there's no magic going on behind the user's back in terms of how their arguments are or aren't reassembled (which is often particularly surprising if a user is trying to run a query involving quotes, which
$@
or$*
will discard when used as here).