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

Signed-off-by: yxxhero <aiopsclub@163.com>
This commit is contained in:
yxxhero 2025-05-13 13:24:27 +08:00
parent 84bc096576
commit 0a67cf2fef
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 // NewEncoder creates and returns a function that is used to encode a Go object to a YAML document
func NewEncoder(w io.Writer) Encoder { func NewEncoder(w io.Writer) Encoder {
if runtime.GoccyGoYaml { 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) return v2.NewEncoder(w)