pull/2047/merge
Jack Zampolin 9 years ago committed by GitHub
commit 017dc04314

@ -65,8 +65,4 @@ import:
version: ^0.2.1 version: ^0.2.1
- package: github.com/evanphx/json-patch - package: github.com/evanphx/json-patch
- package: github.com/facebookgo/symwalk - package: github.com/facebookgo/symwalk
testImports: - package: github.com/naoina/toml
- package: github.com/stretchr/testify
version: ^1.1.4
subpackages:
- assert

@ -25,6 +25,7 @@ import (
"github.com/gobwas/glob" "github.com/gobwas/glob"
"github.com/golang/protobuf/ptypes/any" "github.com/golang/protobuf/ptypes/any"
"github.com/naoina/toml"
) )
// Files is a map of files in a chart that can be accessed from a template. // Files is a map of files in a chart that can be accessed from a template.
@ -191,6 +192,19 @@ func FromYaml(str string) map[string]interface{} {
return m return m
} }
// ToToml takes an interface, marshals it to yaml, and returns a string. It will
// always return a string, even on marshal error (empty string).
//
// This is designed to be called from a template.
func ToToml(v interface{}) string {
data, err := toml.Marshall(v)
if err != nil {
// Swallow error inside of a template.
return ""
}
return string(data)
}
// 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).
// //

@ -111,6 +111,19 @@ func TestToYaml(t *testing.T) {
} }
} }
func TestToToml(t *testing) {
expect := "foo = bar\n"
v := struct {
Foo string `json:"foo"`
}{
Foo: "bar",
}
if got := ToToml(v); got != expect {
t.Errorf("Expected %q, got %q", expect, got)
}
}
func TestFromYaml(t *testing.T) { func TestFromYaml(t *testing.T) {
doc := `hello: world doc := `hello: world
one: one:

@ -70,6 +70,7 @@ func FuncMap() template.FuncMap {
// Add some extra functionality // Add some extra functionality
extra := template.FuncMap{ extra := template.FuncMap{
"toToml": chartutil.ToToml,
"toYaml": chartutil.ToYaml, "toYaml": chartutil.ToYaml,
"fromYaml": chartutil.FromYaml, "fromYaml": chartutil.FromYaml,
"toJson": chartutil.ToJson, "toJson": chartutil.ToJson,

@ -49,7 +49,7 @@ func TestFuncMap(t *testing.T) {
} }
// Test for Engine-specific template functions. // Test for Engine-specific template functions.
expect := []string{"include", "toYaml", "fromYaml"} expect := []string{"include", "toYaml", "fromYaml", "toJson", "fromJson", "toToml"}
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)

Loading…
Cancel
Save