CEL.Env (CEL v0.2.0)

Copy Markdown View Source

What a CEL expression may use: its variables and their types, custom functions, and the message and enum types of its CEL.TypeProvider. The standard CEL functions and macros, and CEL's own types, are always available.

Build one with build/1 and pass it to CEL.check/2, CEL.compile/2 or CEL.eval/3. A CEL.Program keeps the environment it was compiled with, so evaluation does not take one.

Summary

Types

t()

An environment; build one with build/1.

Functions

Builds an environment.

Types

t()

@type t() :: %CEL.Env{
  abbreviations: term(),
  container: term(),
  functions: term(),
  types: term(),
  validate_literals: term(),
  vars: term()
}

An environment; build one with build/1.

Functions

build(opts \\ [])

@spec build(keyword()) :: t()

Builds an environment.

env =
  CEL.Env.build(
    types: PB.CEL.types(MyApp.Schema),
    container: "my.pkg",
    vars: [user: {:message, "my.pkg.User"}, tags: {:list, :string}],
    functions: [my_function]
  )

Options

  • :vars — the variables expressions may read, as a keyword list or map from name to CEL.Type.t/0.
  • :functions — custom functions, as CEL.Function structs. See the "Custom CEL functions" guide.
  • :types — the message and enum types expressions may use, as a CEL.TypeProvider.t/0, such as PB.CEL.types/1 returns for a protobuf schema. Without it there are none. Message and enum names in :vars and :functions may be atoms or strings; each is resolved through the provider, and an unknown one raises, with or without a provider. The well-known types CEL builds in, such as google.protobuf.Timestamp, need no provider.
  • :container — the package names are resolved in, as with container in other CEL implementations: with "my.pkg", User resolves to my.pkg.User.
  • :abbreviations — fully qualified names expressions may refer to by their last segment, as with Abbrevs in cel-go: with ["my.pkg.Decision"], Decision.REMOVED means my.pkg.Decision.REMOVED whatever the container. An abbreviation is used instead of the container, even when the container would resolve the name to something else, and hides a variable of the same name; variables a macro binds still hide it. Abbreviations apply to variables, types and enum values, not to function names.
  • :validate_literals — a subset of [:duration, :timestamp, :regex]. Checking then rejects literal arguments evaluation would reject, such as duration('30d'), timestamp('nope') or s.matches('['). By default these fail when evaluated, as CEL specifies.

Raises CEL.OptionError for an unknown option or an invalid value.