Types

4 min read

Inspect and convert types: what type a value has (map, slice, string, number, bool, null), whether it is empty, and conversion to strings, numbers, booleans, lists and JSON.

Reference

FunctionDescription
isCompositeReports whether v holds other values: a map or a slice.
isEmptyReturns true when the value is nil, an empty string, an empty slice, or an empty map.
isScalarReports whether v is a single value: a string, number or bool.
lenReturns the length of a string, slice, or map.
toBoolConverts a value to bool.
toFloatConverts any value to float64.
toIntConverts any value to int64.
toJSONMarshals any value to a JSON string.
toListConverts a comma-separated string to a list of trimmed strings.
toStringConverts any value to its string representation.
typeBoolReports whether v is a boolean; the same as eq (typeOf v) “bool”.
typeListReports whether v is a list (a YAML array); the same as eq (typeOf v) “slice”.
typeMapReports whether v is a map (a YAML object); the same as eq (typeOf v) “map”.
typeNullReports whether v is null or absent; the same as eq (typeOf v) “null”.
typeNumberReports whether v is a number; the same as eq (typeOf v) “number”.
typeOfReturns the runtime type name of any value.
typeStringReports whether v is a string; the same as eq (typeOf v) “string”.

Functions

isComposite

isComposite reports whether v holds other values: a map or a slice.

{{ isComposite .spec.selector }}  → true
{{ isComposite .spec.replicas }}  → false

isEmpty

isEmpty returns true when the value is nil, an empty string, an empty slice, or an empty map.

{{ isEmpty .spec.logLevel }}  → true
{{ isEmpty .spec.regions }}   → false

isScalar

isScalar reports whether v is a single value: a string, number or bool.

{{ isScalar .deployment.spec.replicas }}  → true
{{ isScalar .spec.selector }}  → false

len

len returns the length of a string, slice, or map. Overrides Go’s built-in len with one that handles all three types uniformly.

{{ len .spec.regions }}            → 2
{{ len .deployment.metadata.name }}  → 3

toBool

toBool converts a value to bool. Returns an error for unrecognized strings.

{{ toBool "yes" }}  → true

toFloat

toFloat converts any value to float64.

{{ toFloat "0.75" }}  → 0.75

toInt

toInt converts any value to int64. Truncates floats.

{{ toInt "3" }}  → 3
{{ toInt 3.7 }}  → 3

toJSON

toJSON marshals any value to a JSON string. Useful for embedding structured data in annotations, ConfigMap values, or status fields.

{{ toJSON .spec.selector }}  → {"app":"web","tier":"frontend"}

toList

toList converts a comma-separated string to a list of trimmed strings. Returns an empty list for empty strings. Use it where a comma-separated value, such as an annotation, needs to be treated as a list.

{{ toList "a, b, c" }}  → [a b c]

toString

toString converts any value to its string representation. Uses fmt.Sprintf("%v", v).

{{ toString .deployment.spec.replicas }}  → "3"

typeBool

typeBool reports whether v is a boolean; the same as eq (typeOf v) “bool”.

{{ if typeBool .spec.schedule }}…{{ end }}

typeList

typeList reports whether v is a list (a YAML array); the same as eq (typeOf v) “slice”.

{{ if typeList .spec.schedule }}…{{ end }}

typeMap

typeMap reports whether v is a map (a YAML object); the same as eq (typeOf v) “map”.

{{ if typeMap .spec.schedule }}…{{ end }}

typeNull

typeNull reports whether v is null or absent; the same as eq (typeOf v) “null”.

{{ if typeNull .spec.schedule }}…{{ end }}

typeNumber

typeNumber reports whether v is a number; the same as eq (typeOf v) “number”.

{{ if typeNumber .spec.schedule }}…{{ end }}

typeOf

typeOf returns the runtime type name of any value. The canonical way to branch on field type.

{{ typeOf .spec.schedule }}  → "map"
{{ typeOf .spec.regions }}   → "slice"

typeString

typeString reports whether v is a string; the same as eq (typeOf v) “string”.

{{ if typeString .spec.schedule }}…{{ end }}