Fix toYamlPretty integer output

Signed-off-by: Puneet Dixit <236133619+puneetdixit200@users.noreply.github.com>
(cherry picked from commit fc77c0e61a)
pull/32140/head
Puneet Dixit 3 weeks ago
parent b8a5d51d16
commit 51104a8d00

@ -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

@ -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",

Loading…
Cancel
Save