Create omitted function

Signed-off-by: Bruno Almeida <bruno.almeida@nokia.com>
pull/12864/head
Bruno Almeida 4 months ago
parent 0d0f91d1ce
commit 847542c462

@ -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
}

@ -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" }}`,

Loading…
Cancel
Save