From 738af32057cd52763673ed1962f52118474fd09a Mon Sep 17 00:00:00 2001 From: Brian Brazil Date: Sat, 28 Nov 2015 13:45:32 +0000 Subject: [PATCH] template: Use zero-values for missing values. Currently missing values will get the value rather than the empty string. Using the empty string is more consistent, and should be easier for users to deal with too. --- template/template.go | 2 ++ template/template_test.go | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/template/template.go b/template/template.go index 3c66bbd2b..e06e26d82 100644 --- a/template/template.go +++ b/template/template.go @@ -273,6 +273,7 @@ func (te Expander) Expand() (result string, resultErr error) { }() tmpl, err := text_template.New(te.name).Funcs(te.funcMap).Parse(te.text) + tmpl.Option("missingkey=zero") if err != nil { return "", fmt.Errorf("error parsing template %v: %v", te.name, err) } @@ -297,6 +298,7 @@ func (te Expander) ExpandHTML(templateFiles []string) (result string, resultErr }() tmpl := html_template.New(te.name).Funcs(html_template.FuncMap(te.funcMap)) + tmpl.Option("missingkey=zero") tmpl.Funcs(html_template.FuncMap{ "tmpl": func(name string, data interface{}) (html_template.HTML, error) { var buffer bytes.Buffer diff --git a/template/template_test.go b/template/template_test.go index 16f634f5e..0dd46a4fd 100644 --- a/template/template_test.go +++ b/template/template_test.go @@ -84,6 +84,21 @@ func TestTemplateExpansion(t *testing.T) { text: "{{ query \"metric{instance='a'}\" | first | label \"instance\" }}", output: "a", }, + { + // Missing label is empty when using label function. + text: "{{ query \"metric{instance='a'}\" | first | label \"foo\" }}", + output: "", + }, + { + // Missing label is empty when not using label function. + text: "{{ $x := query \"metric\" | first }}{{ $x.Labels.foo }}", + output: "", + }, + { + text: "{{ $x := query \"metric\" | first }}{{ $x.Labels.foo }}", + output: "", + html: true, + }, { // Range over query and sort by label. text: "{{ range query \"metric\" | sortByLabel \"instance\" }}{{.Labels.instance}}:{{.Value}}: {{end}}",