Documentation
¶
Index ¶
- type BinaryOpNode
- type Cache
- type CacheConfig
- type CacheStats
- type Config
- type Expression
- func (e *Expression) AddToCache(key string, value any)
- func (e *Expression) Evaluate(ctx context.Context, expr string, variables map[string]any) (any, error)
- func (e *Expression) EvaluateParallel(ctx context.Context, exprs []string, variables map[string]any) ([]any, error)
- func (e *Expression) EvaluateWithMetrics(ctx context.Context, expr string, variables map[string]any) (any, *Metrics, error)
- func (e *Expression) RegisterFunction(name string, handler any, validator Validator) error
- func (e *Expression) RegisterOperator(name string, precedence int, handler func(left, right any) (any, error)) error
- func (e *Expression) ValidateSyntax(expr string) error
- type ExpressionError
- type Function
- type FunctionCallNode
- type IdentifierNode
- type Metrics
- type Node
- type NumberNode
- type Operator
- type StringNode
- type Token
- type TokenType
- type Validator
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BinaryOpNode ¶
BinaryOpNode represents a binary operation node (e.g., +, -, *, /)
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache implements a thread-safe LRU cache with memory limits and TTL support
func NewCache ¶
func NewCache(config *CacheConfig) *Cache
NewCache creates a new cache instance with the given configuration
func (*Cache) PurgeExpired ¶
PurgeExpired manually removes all expired items from the cache
type CacheConfig ¶
type CacheConfig struct {
MaxSize int64 // Maximum memory size in bytes
TTL time.Duration // Time to live for cache entries
CleanupInterval time.Duration // Interval for cleanup routine
OnEvict func(key string, value any) // Callback when an item is evicted
}
CacheConfig defines configuration options for the cache
type CacheStats ¶
type CacheStats struct {
Hits int64 // Number of cache hits
Misses int64 // Number of cache misses
Evictions int64 // Number of cache evictions
Size int64 // Current size in bytes
}
CacheStats tracks cache statistics
type Config ¶
type Config struct {
MaxDepth int
Timeout int
AllowCustom bool
StrictMode bool
CacheEnabled bool
CacheSize int
CacheTTL time.Duration
MaxStringLength int
MaxArrayLength int
}
Config represents engine configuration
func DefaultConfig ¶
func DefaultConfig() *Config
DefaultConfig returns default engine configuration
type Expression ¶
type Expression struct {
// contains filtered or unexported fields
}
Expression represents an expression
func NewExpression ¶
func NewExpression(config *Config) *Expression
NewExpression creates a new expression engine with the given configuration.
Usage:
config := &Config{
MaxDepth: 10, // Maximum expression nesting depth
Timeout: 5000, // Evaluation timeout in milliseconds
AllowCustom: true, // Allow custom functions and operators
StrictMode: true, // Enable strict syntax validation
CacheEnabled: true, // Enable expression result caching
CacheSize: 1024 * 1024, // Cache size limit in bytes (1MB)
CacheTTL: time.Hour, // Cache entry time-to-live
MaxStringLength: 1024 * 1024, // Maximum string length in bytes
MaxArrayLength: 10000, // Maximum array length
}
// Create engine with config
expr := NewExpression(config)
// Or use default configuration
expr := NewExpression(nil)
// Evaluate expression
vars := map[string]any{
"x": 10,
"y": 20,
}
result, err := expr.Evaluate(context.Background(), "x + y", vars)
// Register custom function
expr.RegisterFunction("double", func(x float64) float64 {
return x * 2
}, validateOneNumber)
// Use built-in functions
result, err = expr.Evaluate(context.Background(), "abs(-10) + floor(3.7)", nil)
func (*Expression) AddToCache ¶
func (e *Expression) AddToCache(key string, value any)
AddToCache adds a value to the cache and enforces LRU policy
func (*Expression) Evaluate ¶
func (e *Expression) Evaluate(ctx context.Context, expr string, variables map[string]any) (any, error)
Evaluate evaluates an expression
func (*Expression) EvaluateParallel ¶
func (e *Expression) EvaluateParallel(ctx context.Context, exprs []string, variables map[string]any) ([]any, error)
EvaluateParallel evaluates multiple expressions in parallel
func (*Expression) EvaluateWithMetrics ¶
func (e *Expression) EvaluateWithMetrics(ctx context.Context, expr string, variables map[string]any) (any, *Metrics, error)
EvaluateWithMetrics evaluates an expression with metrics
func (*Expression) RegisterFunction ¶
func (e *Expression) RegisterFunction(name string, handler any, validator Validator) error
RegisterFunction registers a new function with validation
func (*Expression) RegisterOperator ¶
func (e *Expression) RegisterOperator(name string, precedence int, handler func(left, right any) (any, error)) error
RegisterOperator registers a new operator with validation
func (*Expression) ValidateSyntax ¶
func (e *Expression) ValidateSyntax(expr string) error
ValidateSyntax validates expression syntax without evaluating it
type ExpressionError ¶
type ExpressionError struct {
Type string // syntax, runtime, etc.
Message string
Line int
Col int
}
ExpressionError represents an expression error
func (*ExpressionError) Error ¶
func (e *ExpressionError) Error() string
Error returns the error message
type FunctionCallNode ¶
FunctionCallNode represents a function call node
type IdentifierNode ¶
type IdentifierNode struct {
Name string
}
IdentifierNode represents an identifier node