cel_expr/program

Compiling CEL expressions in an environment (see cel_expr/env), once, and evaluating them against activations, the values of their variables:

let env = env.new() |> env.variable("name", types.String)
let assert Ok(program) =
  program.compile("size(name) > 0 && name.startsWith('a')", env)
let assert Ok(value.Bool(True)) =
  program.eval(program, dict.from_list([#("name", value.String("abc"))]))

Types

An error that stops an expression from compiling: a syntax error, or, in a checked environment, a type error. Its location is its line, from 1, and its column, from 0, in code points, or None for an error about the whole expression.

pub type CompileError {
  CompileError(
    message: String,
    location: option.Option(#(Int, Int)),
  )
}

Constructors

  • CompileError(
      message: String,
      location: option.Option(#(Int, Int)),
    )

A compiled expression.

pub type Program

Values

pub fn compile(
  source: String,
  env: env.Env,
) -> Result(Program, List(CompileError))

Compiles an expression, which is type-checked if the environment declares variables. The errors are in source order.

pub fn eval(
  program: Program,
  activation: dict.Dict(String, value.Value),
) -> Result(value.Value, String)

Evaluates a compiled expression with the values of its variables, or gives the message of the error it fails with.

pub fn format_error(
  source: String,
  error: CompileError,
) -> String

An error of the expression source as cel-go displays it: its location and message, and the line it is on with a caret below the column.

ERROR: <input>:1:4: Syntax error: mismatched input '<EOF>' expecting an expression
 | a +
 | ...^
✨ Search Document