diff --git a/pkg/engine/funcs.go b/pkg/engine/funcs.go index 8f05a3a1d..9cd4f404e 100644 --- a/pkg/engine/funcs.go +++ b/pkg/engine/funcs.go @@ -55,6 +55,8 @@ func funcMap() template.FuncMap { "fromJson": fromJSON, "fromJsonArray": fromJSONArray, + "omitted": omitted, + // This is a placeholder for the "include" function, which is // late-bound to a template. By declaring it here, we preserve the // integrity of the linter. @@ -174,3 +176,10 @@ func fromJSONArray(str string) []interface{} { } return a } + +func omitted(a, b interface{}) interface{} { + if b == nil { + return a + } + return b +} diff --git a/pkg/engine/funcs_test.go b/pkg/engine/funcs_test.go index 29bc121b5..59639ef18 100644 --- a/pkg/engine/funcs_test.go +++ b/pkg/engine/funcs_test.go @@ -94,6 +94,14 @@ func TestFuncs(t *testing.T) { tpl: `{{ fromYamlArray . }}`, expect: `[error unmarshaling JSON: while decoding JSON: json: cannot unmarshal object into Go value of type []interface {}]`, vars: `hello: world`, + }, { + tpl: `{{ .hello | omitted 'yes' }}`, + expect: `false`, + vars: map[string]bool{"hello": false}, + }, { + tpl: `{{ .value | omitted 'yes' }}`, + expect: `yes`, + vars: map[string]bool{"hello": false}, }, { // This should never result in a network lookup. Regression for #7955 tpl: `{{ lookup "v1" "Namespace" "" "unlikelynamespace99999999" }}`,