Skip to content

Scalar Node with Literal style encodes as Double-quoted when value has a line with trailing spaces #78

@justfalter

Description

@justfalter

Note: this is a clone of go-yaml/yaml#880, with slight modification to make it more succinct.

data1.yaml

multi-line-string-with-trailing-space-chars: |
  this line does not have trailing whitespace
  this line has trailing space characters   
  this line does not have trailing whitespace
multi-line-string-with-trailing-tab-chars: |
  this line does not have trailing whitespace
  this line has trailing tab characters			
  this line does not have trailing whitespace
multi-line-string-without-trailing-whitespace: |
  this line does not have trailing whitespace
  this line does not have trailing whitespace
  this line does not have trailing whitespace

example code:

package main

import (
	"fmt"
	"os"

	"go.yaml.in/yaml/v4"
)

func main() {
	data, _ := os.ReadFile("data1.yaml")

	ymlData := make(map[string]any)

	yaml.Unmarshal(data, &ymlData)

	newData, _ := yaml.Marshal(ymlData)
	fmt.Println(string(newData))
}

output yaml:

multi-line-string-with-trailing-space-chars: "this line does not have trailing whitespace\nthis line has trailing space characters   \nthis line does not have trailing whitespace\n"
multi-line-string-with-trailing-tab-chars: |
    this line does not have trailing whitespace
    this line has trailing tab characters			
    this line does not have trailing whitespace
multi-line-string-without-trailing-whitespace: |
    this line does not have trailing whitespace
    this line does not have trailing whitespace
    this line does not have trailing whitespace

Observed behaviors:

  • string blocks that have lines with trailing space (0x20) characters will be converted into double-quoted strings. Trailing spaces are preserved.
  • string blocks that have lines with trailing tab (0x09) characters will remain as a string block. Trailing tabs are preserved.

Expectations:

  • string blocks that have lines with trailing space (0x20) characters would remain string blocks, and their trailing spaces would be preserved.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions