Skip to content

JonasHedEng/cel-gleam

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

58 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Common Expression Language (Gleam)

Package Version Hex Docs

The Common Expression Language (CEL) is a non-Turing complete language designed for simplicity, speed, safety, and portability. CEL's C-like syntax looks nearly identical to equivalent expressions in C++, Go, Java, and TypeScript. CEL is ideal for lightweight expression evaluation when a fully sandboxed scripting language is too resource intensive.

// Check whether a resource name starts with a group name.
resource.name.startsWith("/groups/" + auth.claims.group)
// Determine whether the request is in the permitted time window.
request.time - resource.age < duration("24h")
// Check whether all resource names in a list match a given filter.
auth.claims.email_verified && resources.all(r, r.startsWith(auth.claims.email))

Usage

gleam add cel@0.3
import gleam/io

import cel/interpreter

pub fn main() {
  let source =
    "[a, b, c].map(x, x + 2).filter(x, x > 4)[0] == 5 ? 'wibble' : 'wobble'"

  let ctx =
    interpreter.default_context()
    |> interpreter.insert_variable("a", interpreter.Int(1))
    |> interpreter.insert_variable("b", interpreter.Int(3))
    |> interpreter.insert_variable("c", interpreter.Int(5))

  let assert Ok(program) = interpreter.new(source)
  let assert Ok(interpreter.String(answer)) = interpreter.execute(program, ctx)

  io.println("execution result: " <> answer)
  // execution result: wibble
}

Further documentation can be found at https://hexdocs.pm/cel.

Development

gleam test  # Run the tests

State

This library is still very early in its development. The current state and planned future work:

  • Lexing
  • Parsing
  • Expression evaluation
  • Variable context resolution
  • Functions/macros
  • Context provision through Dynamic
  • Type checking
  • Field inits (ident{"a": 5})
  • Duration + Timestamp
  • Value → JSON (separate package)
  • Value → MsgPack (separate package)
  • (Maybe) Serializable AST

About

No description, website, or topics provided.

Resources

License

Stars

5 stars

Watchers

1 watching

Forks

Packages

 
 
 

Contributors

Languages