Skip to content

Commit

Permalink
KEP-25: Implementation of toYaml function. (#1375)
Browse files Browse the repository at this point in the history
Signed-off-by: Marcin Owsiany <mowsiany@D2iQ.com>
  • Loading branch information
porridge committed Mar 3, 2020
1 parent 825c3ca commit ddd65ed
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/engine/renderer/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ type Engine struct {
func New() *Engine {
f := sprig.TxtFuncMap()

f["toYaml"] = ToYaml

// Prevent environment access inside the running KUDO Controller
funcs := []string{"env", "expandenv", "base", "dir", "clean", "ext", "isAbs"}

Expand Down
8 changes: 8 additions & 0 deletions pkg/engine/renderer/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ func TestRender(t *testing.T) {
},
expected: "Name: Bob User"},
{name: "function", template: "Name: {{ .Params.Name | upper }}", params: map[string]interface{}{"Name": "hello"}, expected: "Name: HELLO"},
{
name: "toYaml",
template: "{{ toYaml .Params.Dict | trim | indent 2 }}",
params: map[string]interface{}{
"Dict": map[string]string{"Foo": "Bar", "Baz": "Quux"},
},
expected: " Baz: Quux\n Foo: Bar",
},
}

engine := New()
Expand Down
9 changes: 9 additions & 0 deletions pkg/engine/renderer/functions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package renderer

import "gopkg.in/yaml.v2"

// ToYaml takes any value, and returns its YAML representation as a string.
func ToYaml(v interface{}) (string, error) {
out, err := yaml.Marshal(v)
return string(out), err
}

0 comments on commit ddd65ed

Please sign in to comment.