-
Notifications
You must be signed in to change notification settings - Fork 1
/
ExpressionEngineGovaluate.go
58 lines (51 loc) · 1.65 KB
/
ExpressionEngineGovaluate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package engines
import (
"github.com/zhuxiujia/GoMybatis/lib/github.com/Knetic/govaluate"
"reflect"
"strings"
)
type ExpressionEngineGovaluate struct {
}
func (it *ExpressionEngineGovaluate) Name() string {
return "ExpressionEngineGovaluate"
}
//编译一个表达式
//参数:lexerArg 表达式内容
//返回:interface{} 编译结果,error 错误
func (it *ExpressionEngineGovaluate) Lexer(expression string) (interface{}, error) {
expression = it.repleaceExpression(expression)
return govaluate.NewEvaluableExpression(expression)
}
//执行一个表达式
//参数:lexerResult=编译结果,arg=参数
//返回:执行结果,错误
func (it *ExpressionEngineGovaluate) Eval(compileResult interface{}, arg interface{}, operation int) (interface{}, error) {
var argMap = arg.(map[string]interface{})
for k, v := range argMap {
if v != nil {
var reflectV = reflect.ValueOf(v)
if reflectV.IsValid() == false || (reflectV.Kind() == reflect.Ptr && reflectV.IsNil()) {
argMap[k] = nil
}
}
}
argMap["nil"] = nil
argMap["null"] = nil
return compileResult.(*govaluate.EvaluableExpression).Evaluate(argMap)
}
func (it *ExpressionEngineGovaluate) LexerAndEval(lexerArg string, arg interface{}) (interface{}, error) {
var lex, err = it.Lexer(lexerArg)
if err != nil {
return nil, err
}
return it.Eval(lex, arg, 0)
}
//替换表达式中的值 and,or,参数 替换为实际值
func (it *ExpressionEngineGovaluate) repleaceExpression(expression string) string {
if expression == "" {
return expression
}
expression = strings.Replace(expression, ` and `, " && ", -1)
expression = strings.Replace(expression, ` or `, " || ", -1)
return expression
}