Documentation
¶
Overview ¶
Package expr provides a simple expression DSL for cortex formulas.
Supported operations:
- Arithmetic: +, -, *, /, %
- Comparison: ==, !=, <, >, <=, >=
- Logical: &&, ||, !
- Functions: min, max, abs, floor, ceil, round, if, sqrt, pow
Example expressions:
"base_salary * tax_rate" "if(age >= 65, senior_discount, 0)" "round(total * 0.0825, 2)" "min(calculated, max_amount)"
Index ¶
- type BinaryExpr
- type BoolLit
- type CallExpr
- type Evaluator
- type Expression
- func (e *Expression) Eval(ctx context.Context, getter ValueGetter) (any, error)
- func (e *Expression) EvalBool(ctx context.Context, getter ValueGetter) (bool, error)
- func (e *Expression) EvalFloat64(ctx context.Context, getter ValueGetter) (float64, error)
- func (e *Expression) EvalWithMap(ctx context.Context, values map[string]any) (any, error)
- func (e *Expression) Raw() string
- func (e *Expression) RegisterFunc(name string, fn Func)
- type Func
- type Ident
- type Lexer
- type Node
- type NumberLit
- type Parser
- type StringLit
- type Token
- type TokenType
- type UnaryExpr
- type ValueGetter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BinaryExpr ¶
BinaryExpr represents a binary expression.
type Evaluator ¶
type Evaluator struct {
// contains filtered or unexported fields
}
Evaluator evaluates an AST against a value getter.
func NewEvaluator ¶
func NewEvaluator() *Evaluator
NewEvaluator creates a new evaluator with built-in functions.
func (*Evaluator) RegisterFunc ¶
RegisterFunc registers a custom function.
type Expression ¶
type Expression struct {
// contains filtered or unexported fields
}
Expression represents a compiled expression.
func Compile ¶
func Compile(input string) (*Expression, error)
Compile parses and compiles an expression string.
func MustCompile ¶
func MustCompile(input string) *Expression
MustCompile compiles an expression, panicking on error.
func (*Expression) Eval ¶
func (e *Expression) Eval(ctx context.Context, getter ValueGetter) (any, error)
Eval evaluates the expression against a value getter.
func (*Expression) EvalBool ¶
func (e *Expression) EvalBool(ctx context.Context, getter ValueGetter) (bool, error)
EvalBool evaluates the expression and returns a bool.
func (*Expression) EvalFloat64 ¶
func (e *Expression) EvalFloat64(ctx context.Context, getter ValueGetter) (float64, error)
EvalFloat64 evaluates the expression and returns a float64.
func (*Expression) EvalWithMap ¶
EvalWithMap evaluates the expression using a map as the value source.
func (*Expression) Raw ¶
func (e *Expression) Raw() string
Raw returns the original expression string.
func (*Expression) RegisterFunc ¶
func (e *Expression) RegisterFunc(name string, fn Func)
RegisterFunc registers a custom function for this expression.
type Lexer ¶
type Lexer struct {
// contains filtered or unexported fields
}
Lexer tokenizes an expression string.
type Node ¶
type Node interface {
// contains filtered or unexported methods
}
Node represents an AST node.
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
Parser parses an expression into an AST.
type TokenType ¶
type TokenType int
TokenType represents the type of a token.
const ( TokenEOF TokenType = iota TokenError // Literals TokenNumber TokenString TokenIdent TokenBool // Operators TokenPlus // + TokenMinus // - TokenStar // * TokenSlash // / TokenPercent // % TokenEq // == TokenNe // != TokenLt // < TokenLe // <= TokenGt // > TokenGe // >= TokenAnd // && TokenOr // || TokenNot // ! // Delimiters TokenLParen // ( TokenRParen // ) TokenComma // , )
type ValueGetter ¶
ValueGetter retrieves values by name (e.g., from EvalContext).