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

Trailing spaces before newlines prevent styling string as literal #2147

Open
CRThaze opened this issue Sep 10, 2024 · 1 comment
Open

Trailing spaces before newlines prevent styling string as literal #2147

CRThaze opened this issue Sep 10, 2024 · 1 comment
Labels

Comments

@CRThaze
Copy link

CRThaze commented Sep 10, 2024

Describe the bug
When styling strings as literals that have whitespace preceding newlines, the styling fails.

Version of yq: 4.44.3
Operating system: linux
Installed via: binary

Input Yaml

test.yml:

- foo: 2024-09-09T09:53:58+00:00
  bar: "hello"
  baz: "hello\nworld"
- foo: 2024-09-10T09:53:58+00:00
  bar: "hi"
  baz: "\"hi\nworld\""
- foo: 2024-09-11T09:53:58+00:00
  bar: "bye"
  baz: "good \"bye\n    cruel\" world!"
- foo: 2024-09-12T09:53:58+00:00
  bar: "bye"
  baz: "good \"bye \n    cruel\" world!"

Command

yq '.[] |= with(.[] | select(type == "!!str" and test("\n")); . style="literal" )' test.yml

Actual behavior

- foo: 2024-09-09T09:53:58+00:00
  bar: "hello"
  baz: |-
    hello
    world
- foo: 2024-09-10T09:53:58+00:00
  bar: "hi"
  baz: |-
    "hi
    world"
- foo: 2024-09-11T09:53:58+00:00
  bar: "bye"
  baz: |-
    good "bye
        cruel" world!
- foo: 2024-09-12T09:53:58+00:00
  bar: "bye"
  baz: "good \"bye \n    cruel\" world!"

Expected behavior

- foo: 2024-09-09T09:53:58+00:00
  bar: "hello"
  baz: |-
    hello
    world
- foo: 2024-09-10T09:53:58+00:00
  bar: "hi"
  baz: |-
    "hi
    world"
- foo: 2024-09-11T09:53:58+00:00
  bar: "bye"
  baz: |-
    good "bye
        cruel" world!
- foo: 2024-09-12T09:53:58+00:00
  bar: "bye"
  baz: |-
    good "bye 
        cruel" world!

Additional context

Work-around is to strip whitespace before newlines:

yq '.[] |= with(.[] | select(type == "!!str" and test("\n")); . |= sub("\s+\n", "\n") | . style="literal" )' test.yml

Result:

- foo: 2024-09-09T09:53:58+00:00
  bar: "hello"
  baz: |-
    hello
    world
- foo: 2024-09-10T09:53:58+00:00
  bar: "hi"
  baz: |-
    "hi
    world"
- foo: 2024-09-11T09:53:58+00:00
  bar: "bye"
  baz: |-
    good "bye
        cruel" world!
- foo: 2024-09-12T09:53:58+00:00
  bar: "bye"
  baz: |-
    good "bye
        cruel" world!
@vergenzt
Copy link

I wish something could at least be logged when this happens 🙁 I just was trying to convert a bunch of long double-quoted strings to literal format so they're easier to read as a human, and it was working on some but silently not changing others! I finally ended up searching "style literal" in this repo's Github issues to stumble upon this and I'm pretty sure this is the reason. About to test...

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