expression

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BinaryOpNode

type BinaryOpNode struct {
	Left     Node
	Operator Operator
	Right    Node
}

BinaryOpNode represents a binary operation node (e.g., +, -, *, /)

func (*BinaryOpNode) Evaluate

func (n *BinaryOpNode) Evaluate(ctx context.Context, variables map[string]any) (any, error)

Evaluate evaluates the binary operation node

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) Clear

func (c *Cache) Clear()

Clear removes all items from the cache

func (*Cache) Get

func (c *Cache) Get(key string) (any, bool)

Get retrieves a value from the cache

func (*Cache) Len

func (c *Cache) Len() int

Len returns the number of items in the cache

func (*Cache) PurgeExpired

func (c *Cache) PurgeExpired() int

PurgeExpired manually removes all expired items from the cache

func (*Cache) Remove

func (c *Cache) Remove(key string) bool

Remove removes a key from the cache

func (*Cache) Set

func (c *Cache) Set(key string, value any) error

Set adds or updates a value in the cache

func (*Cache) Size

func (c *Cache) Size() int64

Size returns the current size of the cache in bytes

func (*Cache) Stats

func (c *Cache) Stats() CacheStats

Stats returns cache statistics

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 Function

type Function struct {
	Name      string
	Handler   any
	Validator Validator
}

Function represents an expression function

type FunctionCallNode

type FunctionCallNode struct {
	Name string
	Args []Node
}

FunctionCallNode represents a function call node

func (*FunctionCallNode) Evaluate

func (f *FunctionCallNode) Evaluate(ctx context.Context, variables map[string]any) (any, error)

Evaluate evaluates the function call node

type IdentifierNode

type IdentifierNode struct {
	Name string
}

IdentifierNode represents an identifier node

func (*IdentifierNode) Evaluate

func (n *IdentifierNode) Evaluate(ctx context.Context, variables map[string]any) (any, error)

Evaluate evaluates the identifier node

type Metrics

type Metrics struct {
	ParseTime   time.Duration
	EvalTime    time.Duration
	CacheHits   int64
	CacheMisses int64
}

type Node

type Node interface {
	Evaluate(ctx context.Context, variables map[string]any) (any, error)
}

Node represents an AST node

type NumberNode

type NumberNode struct {
	Value float64
}

NumberNode represents a number node

func (*NumberNode) Evaluate

func (n *NumberNode) Evaluate(ctx context.Context, variables map[string]any) (any, error)

Evaluate evaluates the number node

type Operator

type Operator struct {
	Name       string
	Precedence int
	Handler    func(left, right any) (any, error)
}

Operator represents an operator

type StringNode

type StringNode struct {
	Value string
}

StringNode represents a string node

func (*StringNode) Evaluate

func (n *StringNode) Evaluate(ctx context.Context, variables map[string]any) (any, error)

Evaluate evaluates the string node

type Token

type Token struct {
	Type  TokenType
	Value string
	Line  int
	Col   int
}

Token represents a lexical token

type TokenType

type TokenType int

TokenType represents expression token type

const (
	TokenEOF TokenType = iota
	TokenNumber
	TokenString
	TokenIdentifier
	TokenOperator
	TokenLParen
	TokenRParen
	TokenDot
	TokenComma
	TokenFunction
)

type Validator

type Validator func(args []any) error

Validator validates function arguments

Jump to

Keyboard shortcuts

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