yq icon indicating copy to clipboard operation
yq copied to clipboard

folded multiline scalars should stay in their original format

Open Shanuson opened this issue 4 years ago • 3 comments

Folded scalars in YAML allow to have a nice human readable format for scalars with line breaks that will produce whitespace in the resulted scalar. Sadly writing into a YAML file with yq removes this formatting. When we write the YAML we add line breaks to these scalars on purpose. Even after some manipulation off the file with a tool like yq we look at/read the YAMLs again, so still having the line breaks in the new file helps us humans to read them.

The following examples are with folded scalars, but this should probably be done for all multiline scalars.

The problem:

If we have yq.yml like (I included the block part in the yaml to show that for these scalars it is already possible to keep the line breaks):

plain: 
  two
  lines
folded: >
  two
  lines
block: |
  two
  lines

And we run a command:

yq write yq.yaml version 0.0.1

we get output

plain: two lines
folded: >
  two lines
block: |
  two
  lines
version: 0.0.1

The result works fine technically but loses its readability when the line breaks get removed and the resulted line is really long. What would be nice would be:

plain: 
  two
  lines
folded: >
  two
  lines
block: |
  two
  lines
version: 0.0.1

And maybe it is also possible to turn this on/off via some new flag (--stay-folded), or doing it differently for folded scalars vs plain or single/double quoted?

Also not sure if this is something to be implemented here or in the go-yaml project?

Shanuson avatar May 19 '20 14:05 Shanuson

This is an issue of the go-yaml project. I noticed that when I made the 'folded' value longer, it does its own wrapping:

plain: 
  two
  lines
folded: >
  two really long lines about stuff and thigns
  lines two really long lines about stuff and thigns and what not
  lines two really long lines about stuff and thigns and what not
block: |
  two
  lines

resulted in:

plain: two lines
folded: >
  two really long lines about stuff and thigns lines two really long lines about stuff
  and thigns and what not lines two really long lines about stuff and thigns and what
  not

block: |-
  two
  lines
version: 1

mikefarah avatar May 20 '20 00:05 mikefarah

Related issue: https://github.com/go-yaml/yaml/issues/387

mikefarah avatar Jun 15 '20 02:06 mikefarah

If this is to be done, having a CLI flag would definitely be useful. yq is useful to see how one's YAML file is interpreted by a other programs. I'd want the current behavior to be the default (because that's what a program reading a YAML file sees) and have a flag like --debug that disables folding if you need it.

conradkleinespel avatar Aug 20 '20 13:08 conradkleinespel