Technology
The expression module provides Common Expression Language (CEL) integration for Project Forge applications. It enables runtime evaluation of arbitrary expressions with high performance and caching capabilities.
Overview
This module provides:
- CEL Engine: Google’s Common Expression Language runtime for safe expression evaluation
- Expression Caching: Performance-optimized caching with compile-once-run-many pattern
- Type-Safe Evaluation: Compile-time checking and runtime type validation
- Boolean Result Checking: Utilities for evaluating conditional expressions
Key Features
Performance
- Compile-once-run-many pattern with automatic caching
- Thread-safe expression compilation and evaluation
- Sub-millisecond evaluation times for cached expressions
- Zero-allocation result checking where possible
Safety
- Sandboxed expression execution via CEL runtime
- Compile-time expression validation
- Type-safe parameter passing
- Error handling with detailed context
Flexibility
- Support for complex expressions with variables
- Custom CEL environment options
- Extensible expression parameter types
- Boolean and arbitrary result evaluation
Package Structure
Core Components
lib/expression/model.go- Expression struct and compilation/execution logic- Expression definition with key, description, and pattern
- Compile method for CEL AST generation and program creation
- Run method for parameterized expression evaluation
- Result type checking utilities
lib/expression/check.go- Engine with caching and evaluation services- Thread-safe expression cache management
- CEL environment configuration and initialization
- High-level Check method for boolean expression evaluation
- Compile method for pre-compilation and caching
Usage Examples
Basic Expression Evaluation
1 | |
Pre-compiled Expressions
1 | |
Custom CEL Environment
1 | |
CEL Expression Language
This module uses Google’s Common Expression Language (CEL), which provides:
Supported Operators
- Logical:
&&,||,! - Comparison:
==,!=,<,<=,>,>= - Arithmetic:
+,-,*,/,% - String:
+(concatenation),matches(),contains() - Collection:
in,size(), indexing with[]
Built-in Functions
- String:
size(),startsWith(),endsWith(),matches() - Collection:
size(),inoperator - Type:
type(), conversion functions - Math: Standard arithmetic operations
Data Types
- Primitives:
bool,int,uint,double,string,bytes - Collections:
list,map - Well-known types:
timestamp,duration
Configuration
The module supports CEL environment customization via engine creation options:
1 | |
Performance Characteristics
- Compilation: One-time cost per unique expression pattern
- Evaluation: Sub-millisecond for most expressions
- Memory: Expressions cached indefinitely (consider memory bounds for long-running applications)
- Concurrency: Thread-safe with minimal contention via cache-first pattern
Source Code
- Repository: https://github.com/kyleu/projectforge/tree/main/module/expression
- License: CC0 (Public Domain)
- Author: Kyle U (kyle@kyleu.com)
See Also
- CEL Specification - Complete CEL language reference
- CEL-Go Documentation - Go implementation details
- Project Forge Documentation - Complete documentation