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 output when deleting maps #1941

Closed
AbrohamLincoln opened this issue Feb 8, 2024 · 4 comments
Closed

Unexpected output when deleting maps #1941

AbrohamLincoln opened this issue Feb 8, 2024 · 4 comments
Labels

Comments

@AbrohamLincoln
Copy link

Describe the bug
If you delete all maps from a yaml document, the output is improperly formatted if the original document contains a document start.

Version of yq: v4.40.5
Operating system: mac
Installed via: homebrew

Input Yaml
test.yml:

---
foo:
  bar: baz

Command

yq 'del(.foo)' test.yaml

Actual behavior

---
{}

Expected behavior

---

or

or

{}

Additional context
I need to handle a case where I might delete all of the maps in a doc, but not always. The doc may or may not have the document start line.
The workaround I've come up with is to output as json, which drops the document start and the comments, then convert it back to yaml.

yq 'del(.foo)' test.yaml -o json | yq -p json -o yaml

which results in the following:

{}

Related to: #493

@mikefarah
Copy link
Owner

yq purposefully keeps the document separator and any comments by default. To get rid of document separators you can use the "--no-doc" flag.

yq 'del(.foo)' --no-doc test.yaml

@AbrohamLincoln
Copy link
Author

Is there a different way to perform the delete that wont result in an empty map?
I wouldn't mind (and honestly expected) having just the document separator or an empty string as the output.

@mikefarah
Copy link
Owner

Hmm yeah - you could apply another update if the length of the map is 0. Some thing like:

yq 'del(.foo) | select(length == 0) |= ""' test.yaml
---

So after ".foo" is deleted, if the map length is 0 (has no kids), we can set it to the empty string

@AbrohamLincoln
Copy link
Author

Thanks!

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