Celixir.API (Celixir v0.3.0)

Copy Markdown View Source

A macro-based way to define CEL function libraries.

Usage

defmodule MyApp.CelMath do
  use Celixir.API, scope: "mymath"

  defcel abs(x) do
    Kernel.abs(x)
  end

  defcel clamp(val, lo, hi) do
    val |> max(lo) |> min(hi)
  end
end

env = Celixir.Environment.new() |> MyApp.CelMath.register()
Celixir.eval!("mymath.clamp(150, 0, 100)", env)
# => 100

Functions defined with defcel receive plain Elixir values (already unwrapped from CEL internal types) and should return plain Elixir values.

Without a scope

defmodule MyApp.CelHelpers do
  use Celixir.API

  defcel greet(name) do
    "Hello, #{name}!"
  end
end

env = Celixir.Environment.new() |> MyApp.CelHelpers.register()
Celixir.eval!("greet('world')", env)
# => "Hello, world!"

Summary

Functions

Defines a CEL-callable function.

Functions

defcel(call, list)

(macro)

Defines a CEL-callable function.

The function receives plain Elixir values and should return a plain Elixir value.