Data Structures

Run-Time

An external client depends on the celpy.Environment.

The celpy.Environment builds the initial AST and the final runnable “program.” The celpy.Environment may also contain a type provider and type adapters.

The celpy.Environment also builds an celpy.evaluation.Activation with the variable and function bindings and the default package.

The celpy.evaluation.Activation create a kind of chainmap for name resolution. The chain has the following structure:

  • The end of the chain is the built-in defaults.

  • A layer on top of this can be provided as part of integration into some other app or framework.

  • The next layer is the “current” activation when evaluating a given expression. This often has command-line variables.

  • A transient top-most layer is used to create a local variable binding for the macro evaluations.

The AST is created by Lark from the CEL expression.

There are two celpy.Runner implementations.

  • The celpy.InterpretedRunner walks the AST, creating the final result or exception.

  • The celpy.CompiledRunner transforms the AST to remove empty rules. Then emits the result as a Python expression. It uses the Python internal compile() and eval() functions to evaluate the expression.

CEL Types

There are ten extension types that wrap Python built-in types to provide the unique CEL semantics.

  • celtypes.BoolType wraps int and creates additional type overload exceptions.

  • celtypes.BytesType wraps bytes it handles conversion from celtypes.StringType.

  • celtypes.DoubleType wraps float and creates additional type overload exceptions.

  • celtypes.IntType wraps int and adds a 64-bit signed range constraint.

  • celtypes.UintType wraps int and adds a 64-bit unsigned range constraint.

  • celtypes.ListType wraps list and includes some type overload exceptions.

  • celtypes.MapType wraps dict and includes some type overload exceptions. Additionally, the MapKeyTypes type hint is the subset of types permitted as keys.

  • celtypes.StringType wraps str and includes some type overload exceptions.

  • celtypes.TimestampType wraps datetime.datetime and includes a number of conversions from datetime.datetime, int, and str values.

  • celtypes.DurationType wraps datetime.timedelta and includes a number of conversions from datetime.timedelta, int, and str values.

Additionally, a celtypes.NullType is defined, but does not seem to be needed. It hasn’t been deleted, yet. but should be considered deprecated.