expression

package
v1.1.1507 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Eval

func Eval[T any](ctx context.Context, eng Engine, exp string, vars map[string]interface{}) (retval T, reterr error)

Eval evaluates an expression given a set of variables and returns a generic type.

func EvalAny added in v0.1.175

func EvalAny(ctx context.Context, eng Engine, exp string, vars map[string]interface{}) (retval interface{}, reterr error)

EvalAny evaluates an expression given a set of variables and returns a 'boxed' interface type.

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

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) Eval added in v1.1.1200

func (e *ExprEngine) Eval(ctx 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

func (e *ExprEngine) GetVariables(ctx context.Context, exp string) ([]Variable, error)

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.

type Variable added in v1.1.1200

type Variable struct {
	Name string
}

Variable contains metadata about a variable.

func GetVariables added in v0.1.175

func GetVariables(ctx context.Context, eng Engine, exp string) ([]Variable, error)

GetVariables returns a list of variables mentioned in an expression

Jump to

Keyboard shortcuts

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