Type Notes

3 min read

Type notes inspect and convert the runtime Go types of values in the CR. Because Kubernetes stores everything as JSON, field types depend on how the API server serialized them — these notes let you branch on that safely.

Reference

NoteDescription
typeOfReturn the runtime type name of any value.
typeMapShorthand predicates — return true when the value matches that type.
typeListShorthand predicates — return true when the value matches that type.
typeStringShorthand predicates — return true when the value matches that type.
typeNumberShorthand predicates — return true when the value matches that type.
typeBoolShorthand predicates — return true when the value matches that type.
typeNullShorthand predicates — return true when the value matches that type.
isEmptyReturn true when the value is nil, an empty string, an empty slice, or an empty map.
lenReturn the length of a string, slice, or map.
toIntConvert any value to int64.
toFloatConvert any value to float64.
toBoolConvert a value to bool.
toStringConvert any value to its string representation.
toJsonConvert any value to its JSON representation.

Examples

# typeOf
# value: "{{ typeOf .spec.schedule }}"

# typeMap
normalize:
  spec:
    schedule: >
      {{ if typeMap .spec.schedule }}
        {{ cronFromMap .spec.schedule }}
      {{ else }}
        {{ cronNormalize .spec.schedule }}
      {{ end }}
# value: "{{ typeList .spec.regions }}"
# spec.regions: [us-east-1, eu-west-1] → true
# spec.regions: "us-east-1"           → false

# typeList
normalize:
  spec:
    schedule: >
      {{ if typeMap .spec.schedule }}
        {{ cronFromMap .spec.schedule }}
      {{ else }}
        {{ cronNormalize .spec.schedule }}
      {{ end }}
# value: "{{ typeList .spec.regions }}"
# spec.regions: [us-east-1, eu-west-1] → true
# spec.regions: "us-east-1"           → false

# typeString
normalize:
  spec:
    schedule: >
      {{ if typeMap .spec.schedule }}
        {{ cronFromMap .spec.schedule }}
      {{ else }}
        {{ cronNormalize .spec.schedule }}
      {{ end }}
# value: "{{ typeList .spec.regions }}"
# spec.regions: [us-east-1, eu-west-1] → true
# spec.regions: "us-east-1"           → false

# typeNumber
normalize:
  spec:
    schedule: >
      {{ if typeMap .spec.schedule }}
        {{ cronFromMap .spec.schedule }}
      {{ else }}
        {{ cronNormalize .spec.schedule }}
      {{ end }}
# value: "{{ typeList .spec.regions }}"
# spec.regions: [us-east-1, eu-west-1] → true
# spec.regions: "us-east-1"           → false

# typeBool
normalize:
  spec:
    schedule: >
      {{ if typeMap .spec.schedule }}
        {{ cronFromMap .spec.schedule }}
      {{ else }}
        {{ cronNormalize .spec.schedule }}
      {{ end }}
# value: "{{ typeList .spec.regions }}"
# spec.regions: [us-east-1, eu-west-1] → true
# spec.regions: "us-east-1"           → false

# typeNull
normalize:
  spec:
    schedule: >
      {{ if typeMap .spec.schedule }}
        {{ cronFromMap .spec.schedule }}
      {{ else }}
        {{ cronNormalize .spec.schedule }}
      {{ end }}
# value: "{{ typeList .spec.regions }}"
# spec.regions: [us-east-1, eu-west-1] → true
# spec.regions: "us-east-1"           → false

# isEmpty
# value: "{{ isEmpty .spec.annotations }}"
# {}          → true
# nil         → true
# {app: foo}  → false

# len
# value: "{{ len .spec.regions }}"
# ["us-east-1", "eu-west-1"] → 2

# value: "{{ len .spec.schedule }}"
# {minute: "*/5", hour: "*", ...} → 5  (map with 5 fields)

# value: "{{ len .metadata.name }}"
# "my-app" → 6  (string length)

# toInt
# value: "{{ toInt .spec.replicas }}"
# "3"   → 3
# 3.7   → 3
# true  → 1
# false → 0

# toFloat
# value: "{{ toFloat .spec.threshold }}"
# "0.75" → 0.75
# 3      → 3.0

# toBool
# value: "{{ toBool .spec.enabled }}"
# "yes"  → true
# "no"   → false
# "true" → true

# toString
# value: "{{ toString .spec.replicas }}"
# 3     → "3"
# true  → "true"
# 3.14  → "3.14"

# toJson
# value: "{{ toJson .spec }}"    →  `{"replicas":3,"enabled":true}`
when:
  - field: spec.schedule
    operator: typeOf
    value: map       # same string that typeOf returns