The below works
{{- if hasKey (index $envAll.Values.policy) "type" }}
{{- if has "two-wheeler" (index $envAll.Values.policy "type") }}
<code goes here>
{{- end }}
{{- end }}
while the below fails with "runtime error: invalid memory address or nil pointer dereference"
{{- if and (hasKey (index $envAll.Values.policy) "type") (has "two-wheeler" (index $envAll.Values.policy "type")) }}
<code goes here>
{{- end}}
There is no list by name "type" declared under $envAll.Values.policy.
In Go, if the right operand is evaluated conditionally, why does the last condition gets evaluated in the second code snippet? How do I solve it?
Edit (since it marked as duplicate): Unfortunately, I cannot use embedded {{ if }} like it is mentioned in the other post.
I simplified my problem above. I actually have to achieve this...
{{if or (and (condition A) (condition B)) (condition C)) }}
<code goes here>
{{ end }}
and
function in Go templates is not short-circuit evaluated (unlike the&&
operator in Go), all its arguments are evaluated always. So using 2{{if}}
is the right way to go here. – Blackfordor
andand
in templates evaluate all arguments, so the answer is still the same. – Blackford