Skip to content

Multiline strings with a line ending with a white space are represented inline #1681

Open
@denysvitali

Description

@denysvitali

Describe the bug

Using the "literal style" YAML spec page as an example and the following string:

literal\n\ttext\n

there are two ways of representing it:

  1. An inline representation:
content: "literal\n\ttext\n"
  1. A block representation:
content: |


   literal


   text

Depending on the context, one might be easier to read than the other.

Assuming the following two cases (white space represented by for clarity):

a)

/tmp/a.txt

TEST
1234
00000000: 5445 5354 0a31 3233 340a                 TEST.1234.

b)

/tmp/b.txt

TEST•
1234
00000000: 5445 5354 200a 3132 3334 0a              TEST .1234.

This is the output of the following command:

yq -n '.content = load_str("/tmp/{a,b}.txt")'

a)

content: |
  TEST
  1234

b)

content: "TEST \n1234\n"

Version of yq: 4.34.1
Operating system: MacOS
Installed via: Homebrew

Expected behavior

For clarity, it would be best if the result of encoding a multi-line string ending with •\n (0x20, 0x0a) would not mess up the whole representation - depending on the use.
It would be interesting to allow the user to define this behaviour - some times you really want to make clear that there is a space before a new line - whereas some times you don't really care (e.g: XML).

Additional context

This might be nit-picking, but the output really looks ugly when you have a multi-line document that out of nowhere becomes encoded in a single-line string just because one of the N lines contains a (0x20, 0x0a).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions