Skip to content

Commit f550eda

Browse files
committedApr 25, 2023
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>
1 parent e630834 commit f550eda

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
 

‎pkg/engine/funcs.go

+16
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ func funcMap() template.FuncMap {
4848
// Add some extra functionality
4949
extra := template.FuncMap{
5050
"toToml": toTOML,
51+
"fromToml": fromTOML,
5152
"toYaml": toYAML,
5253
"fromYaml": fromYAML,
5354
"fromYamlArray": fromYAMLArray,
@@ -132,6 +133,21 @@ func toTOML(v interface{}) string {
132133
return b.String()
133134
}
134135

136+
// fromTOML converts a TOML document into a map[string]interface{}.
137+
//
138+
// This is not a general-purpose TOML parser, and will not parse all valid
139+
// TOML documents. Additionally, because its intended use is within templates
140+
// it tolerates errors. It will insert the returned error message string into
141+
// m["Error"] in the returned map.
142+
func fromTOML(str string) map[string]interface{} {
143+
m := make(map[string]interface{})
144+
145+
if err := toml.Unmarshal([]byte(str), &m); err != nil {
146+
m["Error"] = err.Error()
147+
}
148+
return m
149+
}
150+
135151
// toJSON takes an interface, marshals it to json, and returns a string. It will
136152
// always return a string, even on marshal error (empty string).
137153
//

0 commit comments

Comments
 (0)
Please sign in to comment.