feat(pkg/engine): add TOML parsing functionality

Allows to use `fromToml` in templates similar to `fromJson` or `fromYaml`.
Closes #12024

Signed-off-by: Dominik Müller <mail@dominikm.de>
pull/12026/head
Dominik Müller 2 years ago
parent e63083492b
commit f550eda6e9

@ -48,6 +48,7 @@ func funcMap() template.FuncMap {
// Add some extra functionality // Add some extra functionality
extra := template.FuncMap{ extra := template.FuncMap{
"toToml": toTOML, "toToml": toTOML,
"fromToml": fromTOML,
"toYaml": toYAML, "toYaml": toYAML,
"fromYaml": fromYAML, "fromYaml": fromYAML,
"fromYamlArray": fromYAMLArray, "fromYamlArray": fromYAMLArray,
@ -132,6 +133,21 @@ func toTOML(v interface{}) string {
return b.String() 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
// TOML documents. Additionally, because its intended use is within templates
// it tolerates errors. It will insert the returned error message string into
// m["Error"] in the returned map.
func fromTOML(str string) map[string]interface{} {
m := make(map[string]interface{})
if err := toml.Unmarshal([]byte(str), &m); err != nil {
m["Error"] = err.Error()
}
return m
}
// toJSON takes an interface, marshals it to json, and returns a string. It will // toJSON takes an interface, marshals it to json, and returns a string. It will
// always return a string, even on marshal error (empty string). // always return a string, even on marshal error (empty string).
// //

Loading…
Cancel
Save