diff --git a/pkg/engine/funcs.go b/pkg/engine/funcs.go index d03a818c2..5eef2a2b8 100644 --- a/pkg/engine/funcs.go +++ b/pkg/engine/funcs.go @@ -95,7 +95,7 @@ func toYAMLPretty(v interface{}) string { var data bytes.Buffer encoder := goYaml.NewEncoder(&data) encoder.SetIndent(2) - err := encoder.Encode(v) + err := encoder.Encode(normalizeYAMLScalars(v)) if err != nil { // Swallow errors inside of a template. @@ -104,6 +104,37 @@ func toYAMLPretty(v interface{}) string { return strings.TrimSuffix(data.String(), "\n") } +func normalizeYAMLScalars(v any) any { + switch typedValue := v.(type) { + case map[string]any: + normalized := make(map[string]any, len(typedValue)) + for key, value := range typedValue { + normalized[key] = normalizeYAMLScalars(value) + } + return normalized + case map[any]any: + normalized := make(map[any]any, len(typedValue)) + for key, value := range typedValue { + normalized[key] = normalizeYAMLScalars(value) + } + return normalized + case []any: + normalized := make([]any, len(typedValue)) + for index, value := range typedValue { + normalized[index] = normalizeYAMLScalars(value) + } + return normalized + case float64: + // sigs.k8s.io/yaml may unmarshal integer YAML values as float64. + if typedValue == math.Trunc(typedValue) && + typedValue > float64(math.MinInt64) && + typedValue < float64(math.MaxInt64) { + return int64(typedValue) + } + } + return v +} + // fromYAML converts a YAML document into a map[string]interface{}. // // This is not a general-purpose YAML parser, and will not parse all valid diff --git a/pkg/engine/funcs_test.go b/pkg/engine/funcs_test.go index a7e2506a3..c876dd318 100644 --- a/pkg/engine/funcs_test.go +++ b/pkg/engine/funcs_test.go @@ -37,6 +37,10 @@ func TestFuncs(t *testing.T) { tpl: `{{ toYamlPretty . }}`, expect: "baz:\n - 1\n - 2\n - 3", vars: map[string]interface{}{"baz": []int{1, 2, 3}}, + }, { + tpl: `{{ toYamlPretty (fromYaml .) }}`, + expect: "foo: 1000000", + vars: "foo: !!int 1000000", }, { tpl: `{{ toToml . }}`, expect: "foo = \"bar\"\n",