Documentation ¶
Overview ¶
Package evaluator evaluates an expression in the form of s-expression
Package evaluator treats the element within expression with three types, each type has its own array form:
- number For convenience, we treat float64, int64 and so on as type of number. For example, float 100.0 is equal to int 100, but not euqal to string "100"
- string character string quoted with `, ', or " are treated as type of string. You can convert type string to any other defined type you like by type convert functions which are mentioned later
- function or variable character string without quotes are regarded as type of function or variable which depends on whether this function exists. For example in expression (age birthdate), both age and birthdate is unquoted. age is type of function because we have registered a function named age, while birthdate is type of variable for not found. The program will come to errors if there is neither parameter nor function named birthdate when evaluating
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNotFound means the unknow string within the expression cannot be Get from neither functions or params ErrNotFound = errors.New("neither function not variable found") // ErrInvalidResult means the invalid result type expected with the real output ErrInvalidResult = errors.New("invalid result type") )
var ( // ErrUnexpectedEnd occurs most time with the missing parenthesis ErrUnexpectedEnd = errors.New("unexpected end") // ErrNilInput means the input is nil ErrNilInput = errors.New("nil input") // ErrLeftOverText indicates there are some of the expression are left pared ErrLeftOverText = errors.New("left over text") // ErrUnmatchedParenthesis indicated the mismatching parenthesis ErrUnmatchedParenthesis = errors.New("unmatched parenthesis") )
Functions ¶
Types ¶
type Expression ¶
type Expression struct {
// contains filtered or unexported fields
}
Expression stands for an expression which can be evaluated by passing required params
Example ¶
exp, err := New(`(eq gender 'male')`) if err != nil { log.Fatal(err) } params := MapParams{"gender": "male"} res, err := exp.Eval(params) if err != nil { log.Fatal(err) } fmt.Println(res) res, err = exp.EvalBool(params) if err != nil { log.Fatal(err) } fmt.Println(res)
Output: true true
Example (Ignore) ¶
s := `(!(a b )())` exp, err := parse(s) if err != nil { return } exp.dump(0)
Output: evaluator.list: 3 elements: [! [a b] []] evaluator.varString: ! evaluator.list: 2 elements: [a b] evaluator.varString: a evaluator.varString: b evaluator.list: 0 elements: []
func New ¶
func New(expr string) (Expression, error)
New will return a Expression by parsing the given expression string
func (Expression) Eval ¶
func (e Expression) Eval(params Params) (interface{}, error)
Eval evaluates the Expression with params and return the real value in the type of interface
func (Expression) EvalBool ¶
func (e Expression) EvalBool(params Params) (bool, error)
EvalBool invokes method Eval and does boolean type assertion, return ErrInvalidResult if the type of result is not boolean
func (Expression) Properties ¶
func (e Expression) Properties() []string
Properties returns the field names in an Expression. e.g. Expression constructed by `(or (and (between age 18 80) (eq gender "male") )` returns "age", "gender" by calling Properties.