From b075f7a35d25ec0a4414b011142744f8f1821b47 Mon Sep 17 00:00:00 2001 From: Ilya Kiselev Date: Mon, 23 Mar 2026 18:20:54 +0300 Subject: [PATCH 1/2] feat(engine): add mustToToml template function Add `mustToToml` that panics on marshal error, consistent with `mustToYaml` and `mustToJson`. This makes it possible for chart authors to get a hard failure when TOML serialization fails, rather than having to inspect the output manually. `toToml` behavior is unchanged in this commit. Closes #31430 Signed-off-by: Ilya Kiselev --- pkg/engine/funcs.go | 16 ++++++++++++++++ pkg/engine/funcs_test.go | 7 +++++++ 2 files changed, 23 insertions(+) diff --git a/pkg/engine/funcs.go b/pkg/engine/funcs.go index ba842a51a..97db47dfd 100644 --- a/pkg/engine/funcs.go +++ b/pkg/engine/funcs.go @@ -50,6 +50,7 @@ func funcMap() template.FuncMap { // Add some extra functionality extra := template.FuncMap{ "toToml": toTOML, + "mustToToml": mustToTOML, "fromToml": fromTOML, "toYaml": toYAML, "mustToYaml": mustToYAML, @@ -162,6 +163,21 @@ func toTOML(v any) string { return b.String() } +// mustToTOML takes an interface, marshals it to toml, and returns a string. +// It will panic if there is an error. +// +// This is designed to be called from a template when you need to ensure that the +// output TOML is valid. +func mustToTOML(v any) string { + b := bytes.NewBuffer(nil) + e := toml.NewEncoder(b) + err := e.Encode(v) + if err != nil { + panic(err) + } + return b.String() +} + // fromTOML converts a TOML document into a map[string]interface{}. // // This is not a general-purpose TOML parser, and will not parse all valid diff --git a/pkg/engine/funcs_test.go b/pkg/engine/funcs_test.go index 48202454e..be9d0153f 100644 --- a/pkg/engine/funcs_test.go +++ b/pkg/engine/funcs_test.go @@ -159,6 +159,13 @@ keyInElement1 = "valueInElement1"`, tpl: `{{ toJson . }}`, expect: "", // should return empty string and swallow error vars: loopMap, + }, { + tpl: `{{ mustToToml . }}`, + vars: map[int]string{1: "one"}, // non-string key is invalid in TOML + }, { + tpl: `{{ mustToToml . }}`, + expect: "foo = \"bar\"\n", // should succeed and return TOML string + vars: map[string]string{"foo": "bar"}, }, } From c1a5a6e260bd070bce9a8299795400340e10c468 Mon Sep 17 00:00:00 2001 From: Ilya Kiselev Date: Mon, 23 Mar 2026 18:38:57 +0300 Subject: [PATCH 2/2] docs(engine): fix misleading toTOML doc comment The toTOML doc comment said "returns empty string on marshal error" but the implementation actually returns err.Error(). Fix the comment to match the real behavior. Also mention mustToToml as the strict alternative. Signed-off-by: Ilya Kiselev --- pkg/engine/funcs.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pkg/engine/funcs.go b/pkg/engine/funcs.go index 97db47dfd..e03c13b38 100644 --- a/pkg/engine/funcs.go +++ b/pkg/engine/funcs.go @@ -149,10 +149,11 @@ func fromYAMLArray(str string) []any { return a } -// toTOML takes an interface, marshals it to toml, and returns a string. It will -// always return a string, even on marshal error (empty string). +// toTOML takes an interface, marshals it to toml, and returns a string. +// On marshal error it returns the error string. // -// This is designed to be called from a template. +// This is designed to be called from a template. Use mustToToml if you need +// the template to fail hard on marshal errors. func toTOML(v any) string { b := bytes.NewBuffer(nil) e := toml.NewEncoder(b)