Safe access

1 min read

Read a nested field and fall back to a default when it is absent or of the wrong type, typed as a string, an integer or a boolean.

Reference

FunctionDescription
getBoolOrReturns val as bool if it is a bool, otherwise returns def.
getIntOrReturns val as int if it is a numeric type (int, int64, float64), otherwise returns def.
getOrReturns val if non-empty, otherwise def.
getStringOrReturns val as a string if it is a non-empty string, otherwise returns def.

Functions

getBoolOr

getBoolOr returns val as bool if it is a bool, otherwise returns def.

{{ getBoolOr .spec.debug false }}    → true
{{ getBoolOr .spec.suspend false }}  → false

getIntOr

getIntOr returns val as int if it is a numeric type (int, int64, float64), otherwise returns def.

{{ getIntOr .deployment.spec.replicas 1 }}  → 3
{{ getIntOr .spec.workers 1 }}             → 1

getOr

getOr returns val if non-empty, otherwise def. The generic version — same semantics as default but reads more naturally when chaining with other functions.

{{ getOr .spec.workers 4 }}  → 4

getStringOr

getStringOr returns val as a string if it is a non-empty string, otherwise returns def.

{{ getStringOr .spec.sidecarImage "envoyproxy/envoy:v1.30" }}  → "envoyproxy/envoy:v1.30"