celx

package
v1.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 3, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package celx provides utilities for working with CEL (Common Expression Language) expressions, including environment creation, expression compilation, evaluation, and conversion between CEL values and JSON-compatible types.

Index

Constants

This section is empty.

Variables

View Source
var ErrJSONMapExpected = errors.New("expected JSON object")

ErrJSONMapExpected indicates a CEL value could not be converted into a JSON object map

Functions

func NewEnv

func NewEnv(cfg EnvConfig, vars ...cel.EnvOption) (*cel.Env, error)

NewEnv builds a CEL environment from the provided config and variable declarations

func ToJSON

func ToJSON(val ref.Val) (any, error)

ToJSON converts a CEL value to a native JSON-compatible value

func ToJSONMap

func ToJSONMap(val ref.Val) (map[string]any, error)

ToJSONMap converts a CEL value to a JSON object map

Types

type EnvConfig

type EnvConfig struct {
	// ParserRecursionLimit caps the parser recursion depth, 0 uses CEL defaults
	ParserRecursionLimit int
	// ParserExpressionSizeLimit caps expression size (code points), 0 uses CEL defaults
	ParserExpressionSizeLimit int
	// ComprehensionNestingLimit caps nested comprehensions, 0 disables the check
	ComprehensionNestingLimit int
	// ExtendedValidations enables extra AST validations (regex, duration, timestamps, homogeneous aggregates)
	ExtendedValidations bool
	// OptionalTypes enables CEL optional types and optional field syntax
	OptionalTypes bool
	// IdentifierEscapeSyntax enables backtick escaped identifiers
	IdentifierEscapeSyntax bool
	// CrossTypeNumericComparisons enables comparisons across numeric types
	CrossTypeNumericComparisons bool
	// MacroCallTracking records macro calls in AST source info for debugging
	MacroCallTracking bool
}

type EvalConfig

type EvalConfig struct {
	// Timeout is the maximum duration allowed for evaluating a CEL expression
	Timeout time.Duration
	// CostLimit caps the runtime cost of CEL evaluation, 0 disables the limit
	CostLimit uint64
	// InterruptCheckFrequency controls how often CEL checks for interrupts during comprehensions
	InterruptCheckFrequency uint
	// EvalOptimize enables evaluation-time optimizations for repeated program runs
	EvalOptimize bool
	// TrackState enables evaluation state tracking for debugging
	TrackState bool
}

EvalConfig configures CEL evaluation behavior

type Evaluator

type Evaluator struct {
	// contains filtered or unexported fields
}

Evaluator compiles and evaluates CEL expressions with caching

func NewEvaluator

func NewEvaluator(env *cel.Env, config EvalConfig) *Evaluator

NewEvaluator creates a new CEL evaluator with the provided environment and configuration

func (*Evaluator) Compile

func (e *Evaluator) Compile(expression string) (*cel.Ast, *cel.Issues)

Compile parses and type-checks a CEL expression using the evaluator environment

func (*Evaluator) Evaluate

func (e *Evaluator) Evaluate(ctx context.Context, expression string, vars map[string]any) (ref.Val, *cel.EvalDetails, error)

Evaluate runs a CEL expression against the provided variables

func (*Evaluator) EvaluateJSONMap

func (e *Evaluator) EvaluateJSONMap(ctx context.Context, expression string, vars map[string]any) (map[string]any, error)

EvaluateJSONMap evaluates a CEL expression and converts the result to a JSON object map

func (*Evaluator) Program

func (e *Evaluator) Program(ast *cel.Ast) (cel.Program, error)

Program builds a CEL program from an AST without evaluation options

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL