From 96ac6ebc6cc7eb59453bc60ce45fc8d65b9e39d7 Mon Sep 17 00:00:00 2001 From: Matt Butcher Date: Fri, 24 Jun 2016 16:25:43 -0600 Subject: [PATCH] fix(tiller): stop printing when var is missing Instead of printing "no value", this prints an empty string by default, but adds a Strict flag on the engine, which (if true) will cause a template render to error out if a value is not supplied. Strict is set to false so that developers can instead use `default` to set default values. --- pkg/engine/engine.go | 16 +++++++++++++++- pkg/engine/engine_test.go | 5 +++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/pkg/engine/engine.go b/pkg/engine/engine.go index f5cd9df21..b8be5a316 100644 --- a/pkg/engine/engine.go +++ b/pkg/engine/engine.go @@ -19,6 +19,7 @@ package engine import ( "bytes" "fmt" + "strings" "text/template" "github.com/Masterminds/sprig" @@ -31,6 +32,9 @@ type Engine struct { // FuncMap contains the template functions that will be passed to each // render call. This may only be modified before the first call to Render. FuncMap template.FuncMap + // If strict is enabled, template rendering will fail if a template references + // a value that was not passed in. + Strict bool } // New creates a new Go template Engine instance. @@ -93,6 +97,13 @@ func (e *Engine) render(tpls map[string]renderable) (map[string]string, error) { // to share common blocks, but to make the entire thing feel like a file-based // template engine. t := template.New("gotpl") + if e.Strict { + t.Option("missingkey=error") + } else { + // Not that zero will attempt to add default values for types it knows, + // but will still emit for others. We mitigate that later. + t.Option("missingkey=zero") + } files := []string{} for fname, r := range tpls { t = t.New(fname).Funcs(e.FuncMap) @@ -108,7 +119,10 @@ func (e *Engine) render(tpls map[string]renderable) (map[string]string, error) { if err := t.ExecuteTemplate(&buf, file, tpls[file].vals); err != nil { return map[string]string{}, fmt.Errorf("render error in %q: %s", file, err) } - rendered[file] = buf.String() + // Work around the issue where Go will emit "" even if Options(missing=zero) + // is set. Since missing=error will never get here, we do not need to handle + // the Strict case. + rendered[file] = strings.Replace(buf.String(), "", "", -1) buf.Reset() } diff --git a/pkg/engine/engine_test.go b/pkg/engine/engine_test.go index e8c8e54cc..28de0a779 100644 --- a/pkg/engine/engine_test.go +++ b/pkg/engine/engine_test.go @@ -46,6 +46,7 @@ func TestRender(t *testing.T) { Templates: []*chart.Template{ {Name: "test1", Data: []byte("{{.outer | title }} {{.inner | title}}")}, {Name: "test2", Data: []byte("{{.global.callme | lower }}")}, + {Name: "test3", Data: []byte("{{.noValue}}")}, }, Values: &chart.Config{ Raw: "outer: DEFAULT\ninner: DEFAULT", @@ -82,6 +83,10 @@ func TestRender(t *testing.T) { if out["test2"] != expect { t.Errorf("Expected %q, got %q", expect, out["test2"]) } + expect = "" + if out["test3"] != expect { + t.Errorf("Expected %q, got %q", expect, out["test3"]) + } if _, err := e.Render(c, v); err != nil { t.Errorf("Unexpected error: %s", err)