eval

package
v0.0.0-...-f987b8c Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2025 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package eval provides a simple lexical analyzer for processing logical expressions. It converts human-readable logical operators (and, or, not) to their Go equivalents (&&, ||, !).

Index

Constants

This section is empty.

Variables

View Source
var ErrIndexOutOfRange = errors.New("index out of range")

Functions

func CtxWithParam

func CtxWithParam(ctx context.Context, param Param) context.Context

CtxWithParam returns a new context with the parameter.

func DefaultParamKey

func DefaultParamKey() string

DefaultParamKey returns the default key of the parameter.

func MustRegisterEvalFunc

func MustRegisterEvalFunc(name string, v any)

MustRegisterEvalFunc registers a function for eval. If an error occurs, it will panic.

func RegisterEvalFunc

func RegisterEvalFunc(name string, v any) error

RegisterEvalFunc registers a function for eval. The function must be a function with one return value. And Allowed to overwrite the built-in function.

Types

type ExprCompiler

type ExprCompiler interface {
	// Compile compiles the expression and returns the expression.
	Compile(expr string) (Expression, error)
}

ExprCompiler is an evaluator of the expression.

type Expression

type Expression interface {
	// Execute evaluates the expression and returns the value.
	Execute(params Parameter) (Value, error)
}

Expression is an expression which can be evaluated to a value.

func Compile

func Compile(expr string) (Expression, error)

Compile compiles the expression and returns the expression.

type GenericParameter

type GenericParameter struct {
	// Value is the wrapped value
	Value reflect.Value
	// contains filtered or unexported fields
}

GenericParameter is a parameter that wraps a generic value.

func (*GenericParameter) Clear

func (g *GenericParameter) Clear()

Clear clears the cache of the parameter.

func (*GenericParameter) Get

func (g *GenericParameter) Get(name string) (value reflect.Value, exists bool)

Get implements Parameter. It will scopeCache the value of the parameter for better performance.

type H

type H map[string]any

H is a shortcut for map[string]any

func (H) AsParam

func (h H) AsParam() Parameter

AsParam converts the H to a Parameter.

type Lexer

type Lexer struct {
	// contains filtered or unexported fields
}

Lexer performs lexical analysis on input strings. It uses Go's standard scanner to tokenize the input and processes specific identifiers for logical operations.

func NewLexer

func NewLexer(input string) *Lexer

NewLexer creates a new Lexer instance with the given input string. It initializes the internal scanner with the input and configures it to scan comments as well.

func (*Lexer) Tokenize

func (l *Lexer) Tokenize() string

Tokenize processes the input and returns a string with converted operators. It scans through all tokens, replacing logical operators while preserving other tokens and maintaining proper spacing.

type NoOPParameter

type NoOPParameter struct{}

NoOPParameter is a no-op parameter. Its does nothing when calling the Get method.

func (NoOPParameter) Get

Get implements Parameter. always return false.

type Param

type Param = any

Param is an alias of any type. It is used to represent the parameter of the xmlSQLStatement and without type limitation.

func ParamFromContext

func ParamFromContext(ctx context.Context) Param

ParamFromContext returns the parameter from the context.

type ParamGroup

type ParamGroup []Parameter

ParamGroup is a group of parameters which implements the Parameter interface.

func (ParamGroup) Get

func (g ParamGroup) Get(name string) (reflect.Value, bool)

Get implements Parameter.

type Parameter

type Parameter interface {
	// Get returns the value of the named parameter with the type of reflect.Value.
	Get(name string) (reflect.Value, bool)
}

Parameter is the interface that wraps the Get method. Get returns the value of the named parameter.

func NewGenericParam

func NewGenericParam(v any, wrapKey string) Parameter

NewGenericParam creates a generic parameter. if the value is not a map, struct, slice or array, then wrap it as a map.

func NewParameter

func NewParameter(v Param) Parameter

NewParameter creates a new parameter with the given value.

type StaticExprOptimizer

type StaticExprOptimizer struct{}

StaticExprOptimizer is used to optimize static expressions at compile time

func (*StaticExprOptimizer) Optimize

func (s *StaticExprOptimizer) Optimize(exp ast.Expr, params Parameter) (ast.Expr, error)

Optimize optimizes static expressions by evaluating them at compile time

type SyntaxError

type SyntaxError struct {
	// contains filtered or unexported fields
}

SyntaxError represents a syntax error. The error occurs when parsing the expression.

func (*SyntaxError) Error

func (s *SyntaxError) Error() string

Error returns the error message.

func (*SyntaxError) Unwrap

func (s *SyntaxError) Unwrap() error

Unwrap returns the underlying error.

type Value

type Value = reflect.Value

Value is an alias of reflect.Value. for semantic.

func Eval

func Eval(expr string, params Parameter) (Value, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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