Safe Access Notes

1 min read

Type-safe field access with defaults. Use when navigating paths where intermediate keys may be absent or the value may arrive in an unexpected type.

Reference

NoteSignatureReturns
getOrval, def interface{}val if non-empty, else def
getStringOrval interface{}, def stringstring or def
getIntOrval interface{}, def intint or def
getBoolOrval interface{}, def boolbool or def

Examples

# Replicas with default — handles absent key and non-int types
replicas: "{{ getIntOr (get . \"spec\" \"replicas\") 1 }}"

# Image with fallback
image: "{{ getStringOr (get . \"spec\" \"image\") \"nginx:latest\" }}"

# Feature flag with safe default
enabled: "{{ getBoolOr (get . \"spec\" \"monitoring\") false }}"

# Any value with fallback
value: "{{ getOr .spec.config \"{}\" }}"

Difference from default

default val def returns def when val is empty (nil, "", 0, false). It does not do type coercion.

getStringOr, getIntOr, getBoolOr additionally coerce the value to the target type — useful when a field may arrive as a string "3" but you need an int, or when navigating deep paths with get that may return nil.