goccy / go-yaml

YAML support for the Go language
MIT License
1.12k stars 129 forks source link

Trailing new lines are added to literal strings #406

Open nervo opened 11 months ago

nervo commented 11 months ago

Even if they are not part of a literal string, trailing newlines are systematically added to the string

foo: |
  bar

# ↑↑↑ Note the two newlines right after the literal string

In this siituation, the bar value will be "bar\n\n" instead of "bar\n".

Note that this also applies with strip sign |-

Here is a simple reproduction case:

package main

import (
    "fmt"
    goYaml "github.com/goccy/go-yaml"
)

func main() {
    yml := `
foo: |-
  bar

`
    var vmap[string]any
    goYaml.Unmarshal([]byte(yml), &v)

    fmt.Println(v["foo"])
    // -> "bar\n\n"
}

And a related playground: https://go.dev/play/p/XG6oCnbwwiz

nervo commented 4 months ago

Partially fixed by https://github.com/goccy/go-yaml/pull/421 :)

Now, additional trailing lines only occurs without the strip (-) indicator.

@zoncoen may i ask you some help on this ?