From 6af7b56cd2eba25a891f142cd8883f5a7383b4ab Mon Sep 17 00:00:00 2001 From: Emmanuel Oppong Date: Sun, 1 Mar 2026 02:37:37 -0600 Subject: [PATCH] feat: add mustToToml template function to mirror mustToYaml and mustToJson --- pkg/engine/funcs.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/pkg/engine/funcs.go b/pkg/engine/funcs.go index ba842a51a..bf0ee6aed 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,20 @@ 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 need to ensure that the +// output TOML is valid. +func mustToTOML(v any) string { + b := bytes.NewBuffer(nil) + e := toml.NewEncoder(b) + if err := e.Encode(v); 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