Check if context is string or dict

1 {{ printf "%T" "string input" }}
<br>
2 {{ printf "%T" (dict "key1" "dict" "key2" "input") }}
<br>
3 {{ printf "%T" (slice "this" "is" "an" "array") }}
<br>
4 {{ printf "%T" (slice "this" "is" 1 "array") }}
<br>
5 {{ printf "%T" (slice 1 2 3 4 ) }}
<br>
6 {{ printf "%T" 4 }}
<br>
7 {{ printf "%T" 4.4 }}
<br>
8 {{ printf "%T" nil }}
1 string
2 map[string]interface {}
3 []string
4 []interface {}
5 []int
6 int
7 float64
8 <nil>

So if the question is: type map or type NotAMap:

{{ $type :=  printf "%T" .  }}
{{ if in $type "map" }}is map
{{ else }}not map
{{ end }}

If you want it more granular, then you if-else through all the possibilities.

2 Likes