cel_expr
The Common Expression Language for Erlang, Elixir and Gleam: a parser, a type checker that follows cel-go's, an evaluator, the standard library, cel-go's extension libraries, and protobuf messages read from their encoding. It is pure Erlang, needs Erlang/OTP 29 or later, and passes cel-spec's conformance tests, type-checked.
Warning
early work in progress
| build tool | dependency |
|---|---|
| rebar3 | {deps, [cel_expr]}. |
| Gleam | gleam add cel_expr |
| Mix | {:cel_expr, "~> 0.1"} |
An expression compiles once, in an environment, and evaluates against activations, the values of its variables. An environment that declares its variables' types type-checks the expressions it compiles, so that one that refers to anything undeclared, or calls a function with the wrong types, fails to compile rather than to evaluate.
Erlang
Env = #{variables => #{<<"name">> => string}, extensions => [strings]},
{ok, Program} = cel_expr:compile(<<"name.upperAscii().startsWith('A')">>, Env),
{ok, true} = cel_expr:eval(Program, #{<<"name">> => <<"ada">>}),
{error, [#{message := <<"undeclared reference to 'nme' (in container '')">>}]} =
cel_expr:compile(<<"nme">>, Env).
CEL's values are plain Erlang terms: ints are integers, strings binaries, lists lists and maps maps, with tuples for the rest, such as {uint, 1} and {bytes, <<"a">>} (see cel_expr_value). cel_expr has the details of environments, custom functions and extensions, and cel_expr_pb of protobuf messages.
Elixir
Elixir calls the Erlang modules directly, with maps for environments:
env = %{variables: %{"name" => :string}, extensions: [:strings]}
{:ok, program} = :cel_expr.compile("name.upperAscii().startsWith('A')", env)
{:ok, true} = :cel_expr.eval(program, %{"name" => "ada"})
# A custom function, with its overloads for the type checker.
env = %{
variables: %{"name" => :string},
function_types: %{{"shout", :receiver} => [{[:string], :string}]},
functions: %{{"shout", :receiver} => fn [name] -> name <> "!" end}
}
Gleam
The Gleam binding is typed: cel_expr/env builds environments, cel_expr/program compiles and evaluates, cel_expr/value and cel_expr/types are CEL's values and types, and cel_expr/pb reads protobuf messages.
import cel_expr/env
import cel_expr/program
import cel_expr/types
import cel_expr/value
import gleam/dict
pub fn main() {
let env =
env.new()
|> env.variable("name", types.String)
|> env.extensions([env.Strings])
let assert Ok(program) =
program.compile("name.upperAscii().startsWith('A')", env)
let assert Ok(value.Bool(True)) =
program.eval(program, dict.from_list([#("name", value.String("ada"))]))
}
Protobuf messages
Messages are read from their encoding with the descriptors of an encoded FileDescriptorSet (protoc --include_imports --descriptor_set_out, or buf build -o), so any protobuf library's messages work: a message's fields are indexed in one pass and decoded only when an expression reads them. The descriptors are read once per descriptor set and kept in persistent_term, which the provider names, so programs that hold it stay small to send to other processes or to store.
{ok, Provider} = cel_expr_pb:provider(DescriptorSet),
{ok, User} = cel_expr_pb:message(Provider, <<"example.v1.User">>, Encoded),
{ok, Program} = cel_expr:compile(<<"user.name != ''">>, #{
provider => Provider,
variables => #{<<"user">> => {message, <<"example.v1.User">>}}
}),
{ok, true} = cel_expr:eval(Program, #{<<"user">> => User}).
Regular expressions
matches() takes RE2's syntax. By default it runs on OTP's re, with patterns rewritten where PCRE would read them otherwise, such as \s (which has no vertical tab to RE2) and {,2} (which is literal), and refused where re cannot run them with RE2's meaning, such as lookarounds, backreferences and possessive quantifiers. It fails matches that backtrack too far. For RE2's exact meaning, in time linear in the subject, depend on the re2 package too, a NIF on RE2 (its build fetches RE2 with git and compiles it, so it needs both and a C++ compiler), and choose it in the environment:
| language | dependency | environment |
|---|---|---|
| Erlang | {deps, [cel_expr, re2]}. |
#{regex => re2} |
| Elixir | {:re2, "~> 1.9"} |
%{regex: :re2} |
| Gleam | gleam add re2 |
env.regex(env, env.Re2) |
cel_expr does not depend on re2: without it, an environment that asks for it fails to compile anything, with an error that says so.
License
MIT. test/cel_go_parser_cases.eterm holds cel-go's parser tests under cel-go's Apache-2.0 license.