Merge pull request #12026 from dominikmueller/feat/toml-parsing

Feature / toml parsing
pull/13328/head
Joe Julian 3 months ago committed by GitHub
commit 4cc747150d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -78,7 +78,7 @@ func TestFuncMap(t *testing.T) {
} }
// Test for Engine-specific template functions. // Test for Engine-specific template functions.
expect := []string{"include", "required", "tpl", "toYaml", "fromYaml", "toToml", "toJson", "fromJson", "lookup"} expect := []string{"include", "required", "tpl", "toYaml", "fromYaml", "toToml", "fromToml", "toJson", "fromJson", "lookup"}
for _, f := range expect { for _, f := range expect {
if _, ok := fns[f]; !ok { if _, ok := fns[f]; !ok {
t.Errorf("Expected add-on function %q", f) t.Errorf("Expected add-on function %q", f)

@ -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).
// //

@ -37,6 +37,30 @@ func TestFuncs(t *testing.T) {
tpl: `{{ toToml . }}`, tpl: `{{ toToml . }}`,
expect: "foo = \"bar\"\n", expect: "foo = \"bar\"\n",
vars: map[string]interface{}{"foo": "bar"}, vars: map[string]interface{}{"foo": "bar"},
}, {
tpl: `{{ fromToml . }}`,
expect: "map[hello:world]",
vars: `hello = "world"`,
}, {
tpl: `{{ fromToml . }}`,
expect: "map[table:map[keyInTable:valueInTable subtable:map[keyInSubtable:valueInSubTable]]]",
vars: `
[table]
keyInTable = "valueInTable"
[table.subtable]
keyInSubtable = "valueInSubTable"`,
}, {
tpl: `{{ fromToml . }}`,
expect: "map[tableArray:[map[keyInElement0:valueInElement0] map[keyInElement1:valueInElement1]]]",
vars: `
[[tableArray]]
keyInElement0 = "valueInElement0"
[[tableArray]]
keyInElement1 = "valueInElement1"`,
}, {
tpl: `{{ fromToml . }}`,
expect: "map[Error:toml: line 0: unexpected EOF; expected key separator '=']",
vars: "one",
}, { }, {
tpl: `{{ toJson . }}`, tpl: `{{ toJson . }}`,
expect: `{"foo":"bar"}`, expect: `{"foo":"bar"}`,

Loading…
Cancel
Save