feat(yaml): add JSON style encoding option to NewEncoder (#2038)

This commit is contained in:
yxxhero 2025-05-13 18:45:22 +08:00 committed by GitHub
parent 84bc096576
commit 867bef0f03
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 6 additions and 1 deletions

View File

@ -20,7 +20,12 @@ type Encoder interface {
// NewEncoder creates and returns a function that is used to encode a Go object to a YAML document
func NewEncoder(w io.Writer) Encoder {
if runtime.GoccyGoYaml {
return yaml.NewEncoder(w)
yamlEncoderOpts := []yaml.EncodeOption{}
// enable JSON style if the envvar is set
if os.Getenv(envvar.EnableGoccyGoYamlJSONStyle) == "true" {
yamlEncoderOpts = append(yamlEncoderOpts, yaml.JSON(), yaml.Flow(false))
}
return yaml.NewEncoder(w, yamlEncoderOpts...)
}
return v2.NewEncoder(w)