From 0a67cf2fef538528a2307bce3021d24a406f4972 Mon Sep 17 00:00:00 2001 From: yxxhero Date: Tue, 13 May 2025 13:24:27 +0800 Subject: [PATCH] feat(yaml): add JSON style encoding option to NewEncoder Signed-off-by: yxxhero --- pkg/yaml/yaml.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/pkg/yaml/yaml.go b/pkg/yaml/yaml.go index 1b30da25..588bbc4f 100644 --- a/pkg/yaml/yaml.go +++ b/pkg/yaml/yaml.go @@ -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)