Skip to content

Standard library

The standard library is included by default. Call CelEnv.NewBuilder().WithoutStandardLibrary() if you want a stripped-down env (rare — usually for sandboxing).

Conversions

int(x) // x: int / uint / double / string / timestamp / duration / enum
uint(x) // x: int / uint / double / string
double(x) // x: int / uint / double / string
string(x) // x: any printable
bytes(x) // x: bytes / string
type(x) // any → type
duration(s) // s: string ('5m', '1h30m', ...)
timestamp(s) // s: string (RFC 3339)
dyn(x) // any → dyn (gradual typing escape hatch)

Conversions throw on out-of-range or malformed input.

size

size(s) // string → int (utf8 char count)
size(b) // bytes → int (byte count)
size(l) // list → int (element count)
size(m) // map → int (entry count)

has

has(obj.field) // proto presence / has-key / has-property

has is a macro, not a function — it only accepts attribute selects. See Errors & unknowns for the semantics across object types.

String predicates

s.startsWith(prefix)
s.endsWith(suffix)
s.contains(sub)
s.matches(regex) // regex follows RE2 syntax

For richer string ops (replace, split, format), use the strings extension.

Comprehensions (macros)

list.all(x, p) // ∀ x ∈ list, p(x)
list.exists(x, p) // ∃ x ∈ list, p(x)
list.exists_one(x, p) // exactly one
list.map(x, f) // [f(x) for x in list]
list.filter(x, p) // [x for x in list if p(x)]

map and filter work the same on map keys (m.map(k, f)).

Two-iter forms (cel-spec macros2):

list.map(i, v, expr) // i is index, v is value
list.filter(i, v, expr)
list.all(i, v, expr)
list.exists(i, v, expr)

For the parser-level macro framework, see Parser macros.

Time

ts.getFullYear()
ts.getMonth()
ts.getDayOfMonth()
ts.getDayOfWeek()
ts.getHours()
ts.getMinutes()
ts.getSeconds()
ts.getMilliseconds()
ts.getDate() // alias for getDayOfMonth
ts.getDayOfYear()
dur.getHours()
dur.getMinutes()
dur.getSeconds()
dur.getMilliseconds()

All getters take an optional second argument: a timezone string ('America/Los_Angeles', 'UTC', '+05:30'):

ts.getHours('UTC')

Arithmetic on time

timestamp - timestamp // → duration
timestamp - duration // → timestamp
timestamp + duration // → timestamp
duration + duration // → duration
duration - duration // → duration

Optionals (when extension is enabled)

optional.of(x)
optional.none()
opt.value()
opt.orValue(default)
opt.hasValue()
optional.ofNonZeroValue(x) // null/zero → none, else of(x)

Reflection

type(x) // returns a type value
type(x) == int
type(x) == list // unparametrised compare for parametric types

Error & dynamic

dyn(x) // type at runtime; bypasses static checks

See also

  • Operators — the operator-shaped built-ins.
  • Macros — comprehensions and has.
  • Extensions — the catalog of optional libraries.