Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patterns in variable bindings. #218

Merged
merged 16 commits into from
Oct 16, 2024
Merged

Patterns in variable bindings. #218

merged 16 commits into from
Oct 16, 2024

Conversation

01mf02
Copy link
Owner

@01mf02 01mf02 commented Oct 15, 2024

This PR adds support for patterns in variable bindings, e.g. . as {a: [$x, {("b", "c"): $y, $z}]} | $x, $y, $z.
This also works in foreach and reduce.

Compared to jq, this also allows indexing with integers, e.g. {(0): $x, (1): $y}, which is equivalent to [$x, $y]. This can be useful if you wish to destructure arrays, but not necessarily the starting items.

However, jaq does not support the (undocumented) syntax {$x: ...}; for example, the filter {x: [1]} as {$x: [$y]} | $x, $y this yields [1] and 1 in jq.

@01mf02 01mf02 merged commit c3e7e46 into main Oct 16, 2024
1 check passed
@01mf02 01mf02 deleted the patterns branch October 16, 2024 08:07
@wader
Copy link
Contributor

wader commented Oct 16, 2024

Impressive work! and i had no idea jq supported combining key/value pattern like{$x: [$y]}. Index based patter for arrays seems nice addition, wonder how much work it would be to get into jq. I've also wished sometimes for some kind of rest-syntax like [$x, ...$xs] and {$k, ...$obj_with_all_other_keys}

@01mf02
Copy link
Owner Author

01mf02 commented Oct 16, 2024

Regarding {$x: pattern}, I have not implemented this because of a few reasons:

  • {$x: pattern} is not the same as {($x): pattern}. I can't think of any other place in jq where ($x) cannot be substituted by $x, so this is some really surprising behaviour. If you only know the {(f): pattern} syntax and are confronted with {$x: pattern}, you may be up for a pretty big surprise.
  • {$x: pattern} discriminates keys that are not "identifier-like"; that is, this only works for fields like "x", but not for "here-be-dragons". (The same could be said of course about .x and {$x}, but these can be transformed quite simply into equivalent constructs that work with non-identifier-like keys, such as .["x"] and {"x": $x}. There is no easy transformation for {$x: pattern}, however. This can be really cumbersome if you have used {$x: pattern}, perhaps somewhere inside another pattern, then your input format changes from {"x": ...} to {"here-be-dragons: ...}. Have fun rewriting your pattern, especially if you use it inside of reduce or foreach.)

For these reasons, I would also strongly recommend against using this syntax.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants