From 416074901892e46dd53b4084dca2d5f2df1c9993 Mon Sep 17 00:00:00 2001 From: Anastas Dancha Date: Sun, 31 May 2020 14:34:56 +0300 Subject: [PATCH] adding toJsonPretty function variant of toJson toJsonPretty is mostly a copy of toJson, but instead of `json.Marshal` is using `json.MarshalIndent` to pretty print JSON Signed-off-by: Anastas Dancha --- pkg/engine/engine_test.go | 3 ++- pkg/engine/funcs.go | 15 +++++++++++++++ pkg/engine/funcs_test.go | 4 ++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/pkg/engine/engine_test.go b/pkg/engine/engine_test.go index 87e84c48b..a3b560f3f 100644 --- a/pkg/engine/engine_test.go +++ b/pkg/engine/engine_test.go @@ -70,7 +70,7 @@ func TestFuncMap(t *testing.T) { } // 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", "toJson", "toJsonPretty", "fromJson", "lookup"} for _, f := range expect { if _, ok := fns[f]; !ok { t.Errorf("Expected add-on function %q", f) @@ -89,6 +89,7 @@ func TestRender(t *testing.T) { {Name: "templates/test2", Data: []byte("{{.Values.global.callme | lower }}")}, {Name: "templates/test3", Data: []byte("{{.noValue}}")}, {Name: "templates/test4", Data: []byte("{{toJson .Values}}")}, + {Name: "templates/test5", Data: []byte("{{toJsonPretty .Values}}")}, }, Values: map[string]interface{}{"outer": "DEFAULT", "inner": "DEFAULT"}, } diff --git a/pkg/engine/funcs.go b/pkg/engine/funcs.go index 92b4c3383..572081250 100644 --- a/pkg/engine/funcs.go +++ b/pkg/engine/funcs.go @@ -53,6 +53,7 @@ func funcMap() template.FuncMap { "fromYaml": fromYAML, "fromYamlArray": fromYAMLArray, "toJson": toJSON, + "toJsonPretty": toJSONPretty, "fromJson": fromJSON, "fromJsonArray": fromJSONArray, @@ -146,6 +147,20 @@ func toJSON(v interface{}) string { return string(data) } +// toJSONPretty takes an interface, marshals it to json (with indent of two +// spaces, 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 toJSONPretty(v interface{}) string { + data, err := json.MarshalIndent(v, "", " ") + if err != nil { + // Swallow errors inside of a template. + return "" + } + return string(data) +} + // fromJSON converts a JSON document into a map[string]interface{}. // // This is not a general-purpose JSON parser, and will not parse all valid diff --git a/pkg/engine/funcs_test.go b/pkg/engine/funcs_test.go index 62c63ec2b..77b698ce1 100644 --- a/pkg/engine/funcs_test.go +++ b/pkg/engine/funcs_test.go @@ -41,6 +41,10 @@ func TestFuncs(t *testing.T) { tpl: `{{ toJson . }}`, expect: `{"foo":"bar"}`, vars: map[string]interface{}{"foo": "bar"}, + }, {}, { + tpl: `{{ toJsonPretty . }}`, + expect: "{\n \"foo\": \"bar\"\n}", + vars: map[string]interface{}{"foo": "bar"}, }, { tpl: `{{ fromYaml . }}`, expect: "map[hello:world]",