From d8540d78f1a32aafbf7b909aa902fdd58341d61b Mon Sep 17 00:00:00 2001 From: Matt Butcher Date: Wed, 14 Dec 2016 16:47:25 -0700 Subject: [PATCH] feat(tiller): add fromYaml to template functions This adds a fromYaml template function. Closes #1604 --- pkg/chartutil/files.go | 15 +++++++++++++++ pkg/chartutil/files_test.go | 31 +++++++++++++++++++++++++++++++ pkg/engine/engine.go | 3 ++- pkg/engine/engine_test.go | 2 +- 4 files changed, 49 insertions(+), 2 deletions(-) diff --git a/pkg/chartutil/files.go b/pkg/chartutil/files.go index b10842bec..51df52f3c 100644 --- a/pkg/chartutil/files.go +++ b/pkg/chartutil/files.go @@ -174,3 +174,18 @@ func ToYaml(v interface{}) string { } return string(data) } + +// FromYaml converts a YAML document into a map[string]interface{}. +// +// This is not a general-purpose YAML parser, and will not parse all valid +// YAML 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 FromYaml(str string) map[string]interface{} { + m := map[string]interface{}{} + + if err := yaml.Unmarshal([]byte(str), &m); err != nil { + m["Error"] = err.Error() + } + return m +} diff --git a/pkg/chartutil/files_test.go b/pkg/chartutil/files_test.go index 6e4dd3a57..ae101201d 100644 --- a/pkg/chartutil/files_test.go +++ b/pkg/chartutil/files_test.go @@ -110,3 +110,34 @@ func TestToYaml(t *testing.T) { t.Errorf("Expected %q, got %q", expect, got) } } + +func TestFromYaml(t *testing.T) { + doc := `hello: world +one: + two: three +` + dict := FromYaml(doc) + if err, ok := dict["Error"]; ok { + t.Fatalf("Parse error: %s", err) + } + + if len(dict) != 2 { + t.Fatal("expected two elements.") + } + + world := dict["hello"] + if world.(string) != "world" { + t.Fatal("Expected the world. Is that too much to ask?") + } + + // This should fail because we don't currently support lists: + doc2 := ` +- one +- two +- three +` + dict = FromYaml(doc2) + if _, ok := dict["Error"]; !ok { + t.Fatal("Expected parser error") + } +} diff --git a/pkg/engine/engine.go b/pkg/engine/engine.go index 42db58c63..93b0b0b60 100644 --- a/pkg/engine/engine.go +++ b/pkg/engine/engine.go @@ -70,7 +70,8 @@ func FuncMap() template.FuncMap { // Add some extra functionality extra := template.FuncMap{ - "toYaml": chartutil.ToYaml, + "toYaml": chartutil.ToYaml, + "fromYaml": chartutil.FromYaml, // This is a placeholder for the "include" function, which is // late-bound to a template. By declaring it here, we preserve the diff --git a/pkg/engine/engine_test.go b/pkg/engine/engine_test.go index 3ee94cac1..e202804da 100644 --- a/pkg/engine/engine_test.go +++ b/pkg/engine/engine_test.go @@ -49,7 +49,7 @@ func TestFuncMap(t *testing.T) { } // Test for Engine-specific template functions. - expect := []string{"include", "toYaml"} + expect := []string{"include", "toYaml", "fromYaml"} for _, f := range expect { if _, ok := fns[f]; !ok { t.Errorf("Expected add-on function %q", f)