fix more issue
Signed-off-by: yxxhero <aiopsclub@163.com>
This commit is contained in:
parent
c0deedb93e
commit
20ee7330ac
|
|
@ -15,4 +15,5 @@ const (
|
||||||
GoccyGoYaml = "HELMFILE_GOCCY_GOYAML"
|
GoccyGoYaml = "HELMFILE_GOCCY_GOYAML"
|
||||||
CacheHome = "HELMFILE_CACHE_HOME"
|
CacheHome = "HELMFILE_CACHE_HOME"
|
||||||
Interactive = "HELMFILE_INTERACTIVE"
|
Interactive = "HELMFILE_INTERACTIVE"
|
||||||
|
EanbleGoccyGoYamlJsonStyle = "HELMFILE_ENABLE_GOCCY_GOYAML_JSON_STYLE"
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -4,12 +4,14 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
goruntime "runtime"
|
goruntime "runtime"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
"github.com/helmfile/helmfile/pkg/envvar"
|
||||||
"github.com/helmfile/helmfile/pkg/filesystem"
|
"github.com/helmfile/helmfile/pkg/filesystem"
|
||||||
"github.com/helmfile/helmfile/pkg/runtime"
|
"github.com/helmfile/helmfile/pkg/runtime"
|
||||||
)
|
)
|
||||||
|
|
@ -191,12 +193,20 @@ func TestToYaml(t *testing.T) {
|
||||||
input any
|
input any
|
||||||
expected string
|
expected string
|
||||||
wantErr bool
|
wantErr bool
|
||||||
|
enableJsonStyle bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
// https://github.com/helmfile/helmfile/issues/2024
|
// https://github.com/helmfile/helmfile/issues/2024
|
||||||
name: "test unmarshalling issue 2024",
|
name: "test unmarshalling issue 2024",
|
||||||
|
enableJsonStyle: true,
|
||||||
input: map[string]any{"thisShouldBeString": "01234567890123456789"},
|
input: map[string]any{"thisShouldBeString": "01234567890123456789"},
|
||||||
expected: `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 {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
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)
|
actual, err := ToYaml(tt.input)
|
||||||
if tt.wantErr {
|
if tt.wantErr {
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
|
|
|
||||||
|
|
@ -3,12 +3,12 @@ package yaml
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"io"
|
"io"
|
||||||
"strconv"
|
"os"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/goccy/go-yaml"
|
"github.com/goccy/go-yaml"
|
||||||
v2 "gopkg.in/yaml.v2"
|
v2 "gopkg.in/yaml.v2"
|
||||||
|
|
||||||
|
"github.com/helmfile/helmfile/pkg/envvar"
|
||||||
"github.com/helmfile/helmfile/pkg/runtime"
|
"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) {
|
func Marshal(v any) ([]byte, error) {
|
||||||
if runtime.GoccyGoYaml {
|
if runtime.GoccyGoYaml {
|
||||||
var b bytes.Buffer
|
var b bytes.Buffer
|
||||||
yamlEncoder := yaml.NewEncoder(
|
yamlEncoderOpts := []yaml.EncodeOption{
|
||||||
&b,
|
|
||||||
yaml.Indent(2),
|
yaml.Indent(2),
|
||||||
yaml.UseSingleQuote(true),
|
yaml.UseSingleQuote(true),
|
||||||
yaml.UseLiteralStyleIfMultiline(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)
|
err := yamlEncoder.Encode(v)
|
||||||
defer func() {
|
defer func() {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue