pull/31958/merge
Ilya Kiselev 2 days ago committed by GitHub
commit c1cbc8ce9a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -168,7 +168,8 @@ func fromYAMLArray(str string) []any {
} }
// toTOML takes an interface, marshals it to toml, and returns a string. // toTOML takes an interface, marshals it to toml, and returns a string.
// On marshal error it returns the error string. // On marshal error it returns an empty string (errors are swallowed),
// consistent with toYAML and toJSON.
// //
// This is designed to be called from a template. Use mustToToml if you need // This is designed to be called from a template. Use mustToToml if you need
// the template to fail hard on marshal errors. // the template to fail hard on marshal errors.
@ -177,7 +178,8 @@ func toTOML(v any) string {
e := toml.NewEncoder(b) e := toml.NewEncoder(b)
err := e.Encode(v) err := e.Encode(v)
if err != nil { if err != nil {
return err.Error() // Swallow errors inside of a template.
return ""
} }
return b.String() return b.String()
} }

@ -180,6 +180,10 @@ keyInElement1 = "valueInElement1"`,
tpl: `{{ mustToToml . }}`, tpl: `{{ mustToToml . }}`,
expect: "foo = \"bar\"\n", // should succeed and return TOML string expect: "foo = \"bar\"\n", // should succeed and return TOML string
vars: map[string]string{"foo": "bar"}, vars: map[string]string{"foo": "bar"},
}, {
tpl: `{{ toToml . }}`,
expect: "", // should return empty string and swallow error (not err.Error())
vars: map[int]string{1: "one"}, // non-string key is invalid in TOML
}, },
} }

Loading…
Cancel
Save