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

Unexpected order when adding multiple elements to array #2143

Open
ryenus opened this issue Sep 6, 2024 · 4 comments
Open

Unexpected order when adding multiple elements to array #2143

ryenus opened this issue Sep 6, 2024 · 4 comments
Labels

Comments

@ryenus
Copy link
Contributor

ryenus commented Sep 6, 2024

Describe the bug

When trying to sequentially append two objects to an array, say array + o1 + o2, instead of appending o1 then appending o2, yq combined the objects o1 and o2, then appended the new object to the array.

Version of yq: 4.44.3
Operating system: mac
Installed via: homebrew

Input Yaml
N/A

Command

The command you ran:

yq -n '[] + {"a":1} + {"b":2}'

Actual behavior

- a: 1
  b: 2

Expected behavior

- a: 1
- b: 2

Additional context
Add any other context about the problem here.

@jandubois
Copy link

jandubois commented Dec 3, 2024

It looks like operators have right associativity, i.e. a + b + c means a + (b + c).

Normally + should be non-associative (so the order should not matter), but the type coercion properties of the left hand side breaks this.

I'm not sure if making all the operators right-associative was intentional; I suspect it is a side-effect of transforming expressions into a postfix representation internally.

This is even more surprising for operators like subtraction that are normally left-associative:

$ yq -n '6 - 5 - 4'
5

Here the expression is processed as 6 - (5 - 4).

I don't see how this could be fixed without breaking backwards compatibility, but it would help a lot if this was documented (see #1648).

@ryenus
Copy link
Contributor Author

ryenus commented Dec 3, 2024

Thank you @jandubois ! Verifying with subtraction indeed make it more obvious:

Command/Expression Result
jq -n '3 - 2 - 1' 0
yq -n '3 - 2 - 1' 2

@jandubois
Copy link

@ryenus Just in case it wasn't obvious, you can avoid these issues by always adding parenthesis explicitly:

$ yq -n '([] + {"a":1}) + {"b":2}'
- a: 1
- b: 2

@ryenus
Copy link
Contributor Author

ryenus commented Dec 3, 2024

Just in case it wasn't obvious, you can avoid these issues by always adding parenthesis explicitly:

Agreed, for array concatenation, another workaround is to wrap the 2nd operand also in an array:

$ yq -n '[] + [{"a":1}] + {"b":2}'
- a: 1
- b: 2

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

No branches or pull requests

2 participants