Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tiller): add fromYaml to template functions #1686

Merged
merged 1 commit into from
Dec 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions pkg/chartutil/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
31 changes: 31 additions & 0 deletions pkg/chartutil/files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
3 changes: 2 additions & 1 deletion pkg/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down