kislyuk / yq

Command-line YAML, XML, TOML processor - jq wrapper for YAML/XML/TOML documents
https://kislyuk.github.io/yq/
Apache License 2.0
2.53k stars 81 forks source link

Array indexing diverges from jq syntax #156

Closed xxxserxxx closed 1 year ago

xxxserxxx commented 1 year ago

The jq range array function : isn't supported (or doesn't get passed to jq):

jq:

$ echo` '[1,2,3,4]' | jq -c '.[0:3]'
[1,2,3]

yq:

$ echo '<a><b x="1"/><b x="2"/><b x="3"/><b x="4"/></a>' | yq -p xml '[.a.b[] | .+x].[0:3]'
Error: cannot index array with '' (strconv.ParseInt: parsing "": invalid syntax)

The , function works as in jq, and while ; is illegal in jq, yq accepts it without complaint and (from what I can see) causes any other index to be ignored:

yq:

$ echo '<a><b x="1"/><b x="2"/><b x="3"/><b x="4"/></a>' | yq -p xml '[.a.b[] | .+x].[2;3]'
1
2
3
4
$ echo '<a><b x="1"/><b x="2"/><b x="3"/><b x="4"/></a>' | yq -p xml '[.a.b[] | .+x].[99;1111]'
1
2
3
4
kislyuk commented 1 year ago

This report doesn't make sense to me. I am unable to reproduce any of the examples you pasted.

I am going to close this issue for now. Please feel free to post minimal reproduction examples, and we can reopen as needed.

xxxserxxx commented 1 year ago

There are two issues here. The bigger one is that yq doesn't understand : ranges.

In jq, [1:3] gives array items, indexed at 0, up to but not including item 3.

$ echo '["a","b","c","d","e","f"]' | jq -c '.[1:4]'
["b","c","d"]

yq does not understand :. Construct an array with all of the children nodes of a:

$ echo '<a><b/><b/><b/><b/></a>' | yq -p xml '.a[] | length'
4
$ echo '<a><b/><b/><b/><b/></a>' | yq -p xml '.a[1:3] | length'
0
$ echo '{"a": [{"b":0},{"b":0},{"b":0},{"b":0}]}' | jq '.a[1:3] | length'
2

Or, if you think the XML isn't an equavalent for the JSON, how about:

echo '<a><b>0<b/><b>0<b/><b>0<b/><b>0<b/></a>' | yq -p xml '.a[1:3] | length'
0

I can't find any case where yq understands :. Can you?

The second issue, I can file a separate ticket for. The ; is not a valid array character -- use it in an array index, and JQ throws an error. YQ silently swallows it, and generates some output.

kislyuk commented 1 year ago

I think you are confused. I am unable to reproduce any of your examples that involve yq.