Documentation
¶
Index ¶
- Variables
- func Eval[T any](ctx context.Context, eng Engine, exp string, vars map[string]interface{}) (retval T, reterr error)
- func EvalAny(ctx context.Context, eng Engine, exp string, vars map[string]interface{}) (retval interface{}, reterr error)
- type Engine
- type ExprEngine
- func (e *ExprEngine) AssignableTo(ctx context.Context, expression string, dataType string, isArray bool, ...) (string, error)
- func (e *ExprEngine) Eval(_ context.Context, exp string, vars map[string]interface{}) (interface{}, error)
- func (e *ExprEngine) GetVariables(ctx context.Context, exp string) ([]Variable, error)
- func (e *ExprEngine) Validate(_ context.Context, exp string, fn ...ValidationOptionFn) error
- func (e *ExprEngine) WithAllowLiterals(allowLiterals bool) ValidationOptionFn
- func (e *ExprEngine) WithEnv(env map[string]interface{}) ValidationOptionFn
- type ValidationOptionFn
- type Variable
Constants ¶
This section is empty.
Variables ¶
var ErrExpressionFailure = errors.New("expression failure")
ErrExpressionFailure signifies that an expression evaluation or compilation failed.
var ErrUnassignableTo = errors.New("cannot assign expression result to datatype")
ErrUnassignableTo is returned when an expression is not assignable to a datatype
Functions ¶
Types ¶
type Engine ¶ added in v1.1.1200
type Engine interface {
// Eval evaluates an expression given a set of variables and returns a generic type.
Eval(ctx context.Context, expr string, vars map[string]interface{}) (interface{}, error)
// GetVariables returns a list of variables mentioned in an expression
GetVariables(ctx context.Context, expr string) ([]Variable, error)
Validate(_ context.Context, exp string, fn ...ValidationOptionFn) error
AssignableTo(ctx context.Context, expression string, dataType string, isArray bool, fn ...ValidationOptionFn) (string, error)
WithAllowLiterals(allowLiterals bool) ValidationOptionFn
WithEnv(env map[string]interface{}) ValidationOptionFn
}
Engine represents an expression engine implementation.
type ExprEngine ¶ added in v1.1.1200
type ExprEngine struct {
}
ExprEngine is an implementation of the expr-lang an expression engine.
func (*ExprEngine) AssignableTo ¶ added in v1.1.2165
func (e *ExprEngine) AssignableTo(ctx context.Context, expression string, dataType string, isArray bool, fn ...ValidationOptionFn) (string, error)
AssignableTo tests to see if an expression is assignable to a datatype
func (*ExprEngine) Eval ¶ added in v1.1.1200
func (e *ExprEngine) Eval(_ context.Context, exp string, vars map[string]interface{}) (interface{}, error)
Eval takes a context, an expression string, and a map of variables. It returns the result of the expression evaluation as an interface{} and any error encountered during the evaluation process. If the expression string is empty, it returns nil, nil. If the expression string starts with "=", the "=" character is removed from the expression. It then compiles the expression using expr.Compile() and returns any compilation error wrapped with an ErrWorkflowFatal error. It runs the compiled expression using expr.Run(), passing in the variables map, and returns the result and any evaluation error. If there is an evaluation error, it returns the error wrapped with an additional message. If the evaluation is successful, it returns the result and nil as the error.
func (*ExprEngine) GetVariables ¶ added in v1.1.1200
GetVariables takes a context and an expression string. It trims any leading and trailing white spaces from the expression string. If the expression string is empty, it returns nil, nil. If the expression string starts with "=", the "=" character is removed from the expression. It then parses the expression using parser.Parse() and returns any parse error. It walks through the AST of the parsed expression and collects all IdentifierNode types into a slice of Variable structs. Finally, it returns the collected variables and nil as the error.
func (*ExprEngine) Validate ¶ added in v1.1.2055
func (e *ExprEngine) Validate(_ context.Context, exp string, fn ...ValidationOptionFn) error
Validate an expression for syntax errors.
func (*ExprEngine) WithAllowLiterals ¶ added in v1.1.2165
func (e *ExprEngine) WithAllowLiterals(allowLiterals bool) ValidationOptionFn
WithAllowLiterals allows expressions as literals. for example, "1" is a valid expression.
func (*ExprEngine) WithEnv ¶ added in v1.1.2165
func (e *ExprEngine) WithEnv(env map[string]interface{}) ValidationOptionFn
WithEnv sets the variables for the expression evaluation.
type ValidationOptionFn ¶ added in v1.1.2165
type ValidationOptionFn func(options *validateOptions)
ValidationOptionFn is a function that modifies the validation options.