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

yaml.Marshal(...): adds an (for me) unwanted \n #355

Closed
consideRatio opened this issue Apr 5, 2018 · 6 comments
Closed

yaml.Marshal(...): adds an (for me) unwanted \n #355

consideRatio opened this issue Apr 5, 2018 · 6 comments

Comments

@consideRatio
Copy link

For me, it is an unwanted behavior that a new line is popping up in the string after using the yaml.Marshal(...) function. Does it make sense to fix this? I arrive here through Helm issue #3470 and have traced the origin of that issue to this behavior of the yaml.Marshal function. The Helm repo could circumvent it by trimming the new line as well.

Reproduction

package main

import (
	"fmt"

	"gopkg.in/yaml.v2"
)

type Person struct {
	Name string `json:"name"`
	Age  int    `json:"age"`
}

func main() {
	p := Person{"Erik", 31}
	y, _ := yaml.Marshal(p)
	fmt.Println(string(y))
	fmt.Println(string(y))
	/* Output:
	age: 31
	name: Erik

	age: 31
	name: Erik

	*/
}
@rogpeppe
Copy link
Contributor

rogpeppe commented Apr 5, 2018

Thanks for the report, but AFAICS the extra newline is being printed because you're using fmt.Println not fmt.Print. If you use fmt.Print, there is no extra newline printed. Therefore I'm going to close this issue as invalid.

@consideRatio
Copy link
Author

consideRatio commented Apr 5, 2018

@rogpeppe thank you for the reply!

There is a new line added in the end of the string by the yaml.Marshal function, and an extra one by the println. Those two together produce the output of a fully blank line. So one too much I'd say.

If this is wanted or unwanted behavior can boil down to the question if it yaml.Marshal should output a string like A or B.

# output string A (current behavior)
"age: 31\nname: Erik\n"

# output string B (expected behavior)
"age: 31\nname: Erik"

@rogpeppe
Copy link
Contributor

rogpeppe commented Apr 5, 2018

Newlines are a significant part of the YAML format. Why do you think it's a good idea that a formatted YAML file should not contain a final newline?

@consideRatio
Copy link
Author

I'm not confident that it would make sense for this repo to add a new line. I'm thinking that the benefit of not including it would allow a user to choose to end with a new line or not by the usage of "println" / "print" or similar function. No matter if that would make sense, it might become an issue switching to not include a \n now for current users being accustomed to it.

The yaml.Marshal function is in my case not used to write a full YAML file, but a part of it. I'm using Kubernetes/Helm that has declared a toYaml function that outputs yaml snippets based on github.com/ghodss/yaml which is wrapping for this repo.

I'll close this as it seems like the current behavior is the generally expected behavior.

@niemeyer
Copy link
Contributor

niemeyer commented Apr 5, 2018

The yaml format is inherently multi-line, and indented, so having the new line there seems appropriate, and as you hint stripping it is trivial.

If someone is dealing with yaml snippets, they are already into byte-fiddling territory anyway because they will have to reindent the output to match the context.

@consideRatio
Copy link
Author

@rogpeppe and @niemeyer thanks for taking the time to help me think this through!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants