Closed ju6ge closed 3 months ago
Hi @ju6ge, that is some quite cool work!
However, I do not want to put new filters into std.jq
that are not part of main jq
.
I have even refused the inclusion of some jq
filters (SQL-style operators, see #33) in std.jq
.
Still, I think that filters like yours should be somehow findable and includable easily. The better way to do this than to put it into std.jq
, I think, would be to have some kind of jq module repository where people could submit their filters to, and which could be then include
d in jq programs.
I guess that it would be best to open an issue in the jq
repository.
If you put the module in a repo you could add a link to it at https://github.com/fiatjaf/awesome-jq?tab=readme-ov-file#libraries-and-tools-for-jq-itself. There are some jq module repositories but don't know how much they are used
Hi guys, I experimented doing the same thing with the original jq. But I have found that the regex implementation of the original is not working correctly. There are already open issues for this, and they have been open for a while. So that is why i turned to jaq
So doing this is just not possible with the original 'jq' until they fix their regex parsing.
I get your policy of not adding thing not present in the original, sadly this will mean that I have to maintain my own fork for my purposes, to have this working easily in CI environments. And that people who would like to use this, have to do extra work to benefit from the work I put into this.
@ju6ge i see, could you point me to the regex issue?
@wader You can test it for yourself if you take the following definition and try to parse a semver
string using jq
:
def parse_semver: capture("^v?(?P<major>0|[1-9]\\d*)\\.(?P<minor>0|[1-9]\\d*)\\.(?P<patch>0|[1-9]\\d*)(?:-(?P<prerelease>(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?$");
I get the following jq
error:
jq: error (at <stdin>:0): Regex failure: undefined group option
This lead me to the following Github Issue: https://github.com/jqlang/jq/issues/2663. Just searching for group option
in the Issues. I have not dug further …
Ok, the error seem to be about (?P<name>)
named capture groups or? but i don't see how they differ from just (?<name>)
? if i change parse_semver
to use (?<name>)
it seems to work better:
$ jq -n -L . 'include "semver"; "1.2.3-4+meta" | parse_semver'
{
"major": "1",
"minor": "2",
"patch": "3",
"prerelease": "4",
"buildmetadata": "meta"
}
@wader thanks for the pointer … so with some adjustment I could make it work for jq
\o/
@ju6ge i think so unless there was some special reasons for using (?P<name>)
but i can't find that there is any difference between them. found this is https://www.pcre.org/current/doc/html/pcre2pattern.html
In PCRE2, a capture group can be named in one of three ways: (?
...) or (?'name'...) as in Perl, or (?P ...) as in Python
Oniguruma the regex library used by jq seems to support python syntax (ONIG_SYNTAX_PYTHON
) but i don't know if it's possible to mix hmm. I would say easier is to just change to (?<name>)
@wader I think I will try adding this to jq
directly with the adjusted regex. And post a link to the PR here ;)
Regarding the PYTHON
syntax, at least regex101 seems to think that the full regex is a valid python regex. So dunno …
Yeap it's probably valid python regex just that jq doesn't configure Oniguruma to support it and i suspect it won't be changed any time soon.
As one of the current jq maintainers i would say that we're quite reluctant to add new standard library functions. But please add it to awsome-jq, the jq wiki or both
This PR adds builtin support for parsing and ordering semver strings:
parse_semver
function:parses semver strings according to semver regex
Taking the example valid semver strings this would look something like this:
cat /tmp/test.json | jaq
cat /tmp/test.json | jaq 'map(. | parse_semver)' | [.[14]]
semver_ord
function:use parse_semver to create a nested array sortable by
sort_by
function, this is done by transforming the version into the following structure:{}
.[major, minor, patch]
first.prereleases
are ordered lexicographically first and then according to their subversionapplying the ordering to the above list of semver strings would look like this:
cat /tmp/test.json | ./target/debug/jaq 'sort_by(. | semver_ord) | reverse'