diff --git a/pkg/envvar/const.go b/pkg/envvar/const.go index 0235af17..eb4de609 100644 --- a/pkg/envvar/const.go +++ b/pkg/envvar/const.go @@ -6,13 +6,14 @@ const ( // use helm status to check if a release exists before installing it UseHelmStatusToCheckReleaseExistence = "HELMFILE_USE_HELM_STATUS_TO_CHECK_RELEASE_EXISTENCE" - DisableRunnerUniqueID = "HELMFILE_DISABLE_RUNNER_UNIQUE_ID" - Experimental = "HELMFILE_EXPERIMENTAL" // environment variable for experimental features, expecting "true" lower case - Environment = "HELMFILE_ENVIRONMENT" - FilePath = "HELMFILE_FILE_PATH" - TempDir = "HELMFILE_TEMPDIR" - UpgradeNoticeDisabled = "HELMFILE_UPGRADE_NOTICE_DISABLED" - GoccyGoYaml = "HELMFILE_GOCCY_GOYAML" - CacheHome = "HELMFILE_CACHE_HOME" - Interactive = "HELMFILE_INTERACTIVE" + DisableRunnerUniqueID = "HELMFILE_DISABLE_RUNNER_UNIQUE_ID" + Experimental = "HELMFILE_EXPERIMENTAL" // environment variable for experimental features, expecting "true" lower case + Environment = "HELMFILE_ENVIRONMENT" + FilePath = "HELMFILE_FILE_PATH" + TempDir = "HELMFILE_TEMPDIR" + UpgradeNoticeDisabled = "HELMFILE_UPGRADE_NOTICE_DISABLED" + GoccyGoYaml = "HELMFILE_GOCCY_GOYAML" + CacheHome = "HELMFILE_CACHE_HOME" + Interactive = "HELMFILE_INTERACTIVE" + EanbleGoccyGoYamlJsonStyle = "HELMFILE_ENABLE_GOCCY_GOYAML_JSON_STYLE" ) diff --git a/pkg/tmpl/context_funcs_test.go b/pkg/tmpl/context_funcs_test.go index 7af7db91..60e181a4 100644 --- a/pkg/tmpl/context_funcs_test.go +++ b/pkg/tmpl/context_funcs_test.go @@ -4,12 +4,14 @@ import ( "encoding/json" "fmt" "io/fs" + "os" "path/filepath" goruntime "runtime" "testing" "github.com/stretchr/testify/require" + "github.com/helmfile/helmfile/pkg/envvar" "github.com/helmfile/helmfile/pkg/filesystem" "github.com/helmfile/helmfile/pkg/runtime" ) @@ -187,16 +189,24 @@ func TestToYaml_NestedMapInterfaceKey(t *testing.T) { func TestToYaml(t *testing.T) { tests := []struct { - name string - input any - expected string - wantErr bool + name string + input any + expected string + wantErr bool + enableJsonStyle bool }{ { // https://github.com/helmfile/helmfile/issues/2024 - name: "test unmarshalling issue 2024", - input: map[string]any{"thisShouldBeString": "01234567890123456789"}, - expected: `thisShouldBeString: "01234567890123456789" + name: "test unmarshalling issue 2024", + enableJsonStyle: true, + input: map[string]any{"thisShouldBeString": "01234567890123456789"}, + expected: `'thisShouldBeString': '01234567890123456789' +`, + }, + { + name: "test unmarshalling issue 2024 with int64", + input: map[string]any{"thisShouldBeString": 1234567890123456789}, + expected: `thisShouldBeString: 1234567890123456789 `, }, { @@ -237,6 +247,12 @@ func TestToYaml(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { + if tt.enableJsonStyle { + _ = os.Setenv(envvar.EanbleGoccyGoYamlJsonStyle, "true") + } + defer func() { + _ = os.Unsetenv(envvar.EanbleGoccyGoYamlJsonStyle) + }() actual, err := ToYaml(tt.input) if tt.wantErr { require.Error(t, err) diff --git a/pkg/yaml/yaml.go b/pkg/yaml/yaml.go index 08217717..e10f916f 100644 --- a/pkg/yaml/yaml.go +++ b/pkg/yaml/yaml.go @@ -3,12 +3,12 @@ package yaml import ( "bytes" "io" - "strconv" - "strings" + "os" "github.com/goccy/go-yaml" v2 "gopkg.in/yaml.v2" + "github.com/helmfile/helmfile/pkg/envvar" "github.com/helmfile/helmfile/pkg/runtime" ) @@ -68,20 +68,19 @@ func NewDecoder(data []byte, strict bool) func(any) error { func Marshal(v any) ([]byte, error) { if runtime.GoccyGoYaml { var b bytes.Buffer - yamlEncoder := yaml.NewEncoder( - &b, + yamlEncoderOpts := []yaml.EncodeOption{ yaml.Indent(2), yaml.UseSingleQuote(true), yaml.UseLiteralStyleIfMultiline(true), - yaml.CustomMarshaler( - func(v string) ([]byte, error) { - if strings.HasPrefix(v, "0") { - // check v is 0xxxx style number. - return []byte(strconv.Quote(v)), nil - } - return []byte(v), nil - }, - ), + } + // enable JSON style if the envvar is set + if os.Getenv(envvar.EanbleGoccyGoYamlJsonStyle) == "true" { + yamlEncoderOpts = append(yamlEncoderOpts, yaml.JSON(), yaml.Flow(false)) + } + + yamlEncoder := yaml.NewEncoder( + &b, + yamlEncoderOpts..., ) err := yamlEncoder.Encode(v) defer func() {