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 new lines are added to literal strings #406

Closed
nervo opened this issue Oct 11, 2023 · 5 comments · Fixed by #507
Closed

Trailing new lines are added to literal strings #406

nervo opened this issue Oct 11, 2023 · 5 comments · Fixed by #507
Labels
bug Something isn't working

Comments

@nervo
Copy link
Contributor

nervo commented Oct 11, 2023

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

  • Go version: 1.21.2
  • go-yaml's Version: v1.11.2
@nervo nervo added the bug Something isn't working label Oct 11, 2023
@nervo
Copy link
Contributor Author

nervo commented May 17, 2024

Partially fixed by #421 :)

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

@zoncoen may i ask you some help on this ?

@goccy goccy added invalid This doesn't seem right and removed bug Something isn't working labels Nov 5, 2024
@goccy
Copy link
Owner

goccy commented Nov 5, 2024

@nervo github.com/go-yaml/yaml also interprets this as a single newline, and I believe interpreting it as one is correct according to the spec. Please provide a source that justifies interpreting it as two.

@goccy goccy closed this as completed Nov 5, 2024
@nervo
Copy link
Contributor Author

nervo commented Nov 5, 2024

@goccy sorry, maybe there is a misunderstanding :)
In such a situation, according to the spec, there should be only one newline, exactly as you say.

The issue is that there is currently two new lines :)

@goccy goccy added bug Something isn't working and removed invalid This doesn't seem right labels Nov 6, 2024
@goccy goccy reopened this Nov 6, 2024
@goccy
Copy link
Owner

goccy commented Nov 6, 2024

@nervo I see, so unnecessary newline characters are being added. That does seem to be the case. Thank you.

@goccy
Copy link
Owner

goccy commented Nov 6, 2024

@nervo I've fixed this problem with #507 . This was already reflected to master branch. Please confirm it 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants