List / Map Notes

1 min read

Inspect and transform slices and maps. All notes accept interface{} and return safe zero values for nil or wrong-type input.

Reference

NoteSignatureReturns
listHaslist, val interface{}bool
listGetlist interface{}, index intinterface{} — nil when out of bounds
listLenlist interface{}int
mapGetmap interface{}, key stringinterface{}
mapKeysmap interface{}[]string
mapValuesmap interface{}[]interface{}
mapPickmap interface{}, keys ...stringmap[string]interface{} — only named keys
mapOmitmap interface{}, keys ...stringmap[string]interface{} — keys removed

Examples

# Check region membership
- path: hasEastRegion
  value: "{{ listHas .spec.regions \"us-east-1\" }}"

# Use primary region
primaryRegion: "{{ listGet .spec.regions 0 }}"

# Region count
- path: regionCount
  value: "{{ listLen .spec.regions }}"

# Forward only public labels to a child resource
labels: "{{ mapOmit .metadata.labels \"internal-tag\" \"cost-center\" }}"

# Pass a focused subset of spec to a child
config: "{{ mapPick .spec \"image\" \"replicas\" \"port\" }}"

# Read a label value
- path: appLabel
  value: "{{ mapGet .metadata.labels \"app\" }}"

For parsing YAML/JSON strings into lists or maps, see type coercion notes.