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

Unique does not work on arrays with objects #2068

Closed
reuzel opened this issue Jun 13, 2024 · 3 comments
Closed

Unique does not work on arrays with objects #2068

reuzel opened this issue Jun 13, 2024 · 3 comments
Labels

Comments

@reuzel
Copy link

reuzel commented Jun 13, 2024

Describe the bug
When provided with an array of objects, unique does not perform a 'deep' comparison to determine equality (like jq does).

Version of yq: 4.44.1
Operating system: linux
Installed via: binary release

Input Yaml
Concise yaml document(s) (as simple as possible to show the bug, please keep it to 10 lines or less)
data1.yml:

- a: 1
  b: 2
- a: 2
  b: 2
- a: 1
  b: 2
- a: 1
  b: 1

Command
The command you ran:

yq e 'unique' data1.yml

Actual behavior

- a: 1
  b: 2

Expected behavior

- a: 1
  b: 2
- a: 2
  b: 2
- a: 1
  b: 1

Additional comment
Using a roundtrip via jq provides the right output (though the order in the output was sorted):

cat data1.yml | yq -j - | jq 'unique' - | yq -P -

result:

- a: 1
  b: 1
- a: 1
  b: 2
- a: 2
  b: 2
@mikefarah
Copy link
Owner

Unique really only operates on scalars (strings/numbers/..) but what you can do, is unique_by(exp); and give it an expression that gives a scalar that it knows how to compare:

yq 'unique_by(to_yaml)' data1.yaml
- a: 1
  b: 2
- a: 2
  b: 2
- a: 1
  b: 1

@mikefarah
Copy link
Owner

mikefarah commented Jun 15, 2024

Guess I can use this same trick in the unique function under the hood to handle objects 🤔

@mikefarah
Copy link
Owner

Fixed in v4.44.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