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

Updating existing empty map results in single-line yaml syntax #1176

Closed
marcostvz opened this issue Apr 13, 2022 · 3 comments
Closed

Updating existing empty map results in single-line yaml syntax #1176

marcostvz opened this issue Apr 13, 2022 · 3 comments
Labels

Comments

@marcostvz
Copy link

Describe the bug
As described in https://mikefarah.gitbook.io/yq/operators/assign-update#update-empty-object-and-array, updating a file with an existing empty map results in a one-liner yaml. In our use case, this makes helm template to break.

Version of yq: 4.24.2
Operating system: tested on mac and linux
Installed via: homebrew for mac, binary release for linux

Input Yaml
fields.yaml:

global: {}

Command

yq eval '.global.test = "val"' fields.yaml

Actual behavior

global: {test: val}

Expected behavior

global:
  test: val

Additional context
If we work with an empty file, or a non-existing key, the format is correct:

$ yq eval '.global.test = "val"' empty-fields.yaml
global:
  test: val

$ yq eval '.other.test = "val"' fields.yaml
global: {}
other:
  test: val

Also, as a workaround, we can parse it to json and back to yaml and the format is correct:

$ yq eval '.global.test = "val"' fields.yaml -o json | yq -P '.' -
global:
  test: val
@mikefarah
Copy link
Owner

Yeah this is a side effect of yq trying to maintain the styling used in the document. I'm surprised helm doesn't like it as it's perfectly valid yaml.

I'll look into making it a bit smarter for scenarios like this, till then you can:

yq -P '.global.test = "val"' fields.yaml

@mikefarah
Copy link
Owner

Fixed in 4.24.5

@krokofant
Copy link

I want to have the empty map converted to a single line definition when updated. Seems there is no way to achieve this now.

I have branches with commits changing something like:

foo1:
  bar: true

and

foo2:
  bar: true

Now merging these automatically would result in (depending on which way)

foo1:
foo2:
  bar: true

If I could maintain the yaml in a single line, then I could work around this issue. The merge would be:

foo1: { bar: true }
foo2: { bar: true }

I expected combining these commands would preserve the inline format:

yq e -i ".foo1 = {}" myvalues.yaml
yq e -i ".foo1.bar = true" myvalues.yaml

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

3 participants