-
-
Notifications
You must be signed in to change notification settings - Fork 98
/
ExpressionEngineProxy.go
111 lines (99 loc) · 3.47 KB
/
ExpressionEngineProxy.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package GoMybatis
import (
"github.com/zhuxiujia/GoMybatis/ast"
"github.com/zhuxiujia/GoMybatis/utils"
)
type ExpressionEngineProxy struct {
expressionEngineLexerCache ExpressionEngineLexerCache //lexer缓存接口,默认使用ExpressionEngineLexerMapCache
expressionEngine ast.ExpressionEngine
lexerCacheable bool //是否使用lexer缓存,默认false
}
//engine :表达式引擎,useLexerCache:是否缓存Lexer表达式编译结果
func (ExpressionEngineProxy) New(engine ast.ExpressionEngine, useLexerCache bool) ExpressionEngineProxy {
var it = ExpressionEngineProxy{
expressionEngine: engine,
lexerCacheable: useLexerCache,
}
if it.expressionEngineLexerCache == nil {
var cache = ExpressionEngineLexerMapCache{}.New()
it.SetLexerCache(&cache)
}
return it
}
//引擎名称
func (it *ExpressionEngineProxy) SetExpressionEngine(engine ast.ExpressionEngine) {
it.expressionEngine = engine
}
//引擎名称
func (it ExpressionEngineProxy) Name() string {
if it.expressionEngine == nil {
return ""
}
return it.expressionEngine.Name()
}
//编译一个表达式
//参数:lexerArg 表达式内容
//返回:interface{} 编译结果,error 错误
func (it *ExpressionEngineProxy) Lexer(expression string) (interface{}, error) {
if it.expressionEngine == nil {
return nil, utils.NewError("ExpressionEngineProxy", "ExpressionEngineProxy not init for ExpressionEngineProxy{}.New(...)")
}
if it.lexerCacheable {
if it.LexerCache() == nil {
panic(utils.NewError("ExpressionEngineProxy", "lexerCacheable =true! lexerCache can not be nil! you must set the cache!"))
}
//如果 提供缓存,则使用缓存
cacheResult, cacheErr := it.LexerCache().Get(expression)
if cacheErr != nil {
return nil, cacheErr
}
if cacheResult != nil {
return cacheResult, nil
}
}
var result, err = it.expressionEngine.Lexer(expression)
if it.lexerCacheable && err == nil {
//如果 提供缓存,则使用缓存
it.LexerCache().Set(expression, result)
}
return result, err
}
//执行一个表达式
//参数:lexerResult=编译结果,arg=参数
//返回:执行结果,错误
func (it *ExpressionEngineProxy) Eval(lexerResult interface{}, arg interface{}, operation int) (interface{}, error) {
if it.expressionEngine == nil {
return nil, utils.NewError("ExpressionEngineProxy", "ExpressionEngineProxy not init for ExpressionEngineProxy{}.New(...)")
}
return it.expressionEngine.Eval(lexerResult, arg, operation)
}
func (it *ExpressionEngineProxy) LexerCache() ExpressionEngineLexerCache {
return it.expressionEngineLexerCache
}
func (it *ExpressionEngineProxy) SetLexerCache(cache ExpressionEngineLexerCache) {
it.expressionEngineLexerCache = cache
}
func (it *ExpressionEngineProxy) SetUseLexerCache(isUseCache bool) error {
it.lexerCacheable = isUseCache
return nil
}
func (it *ExpressionEngineProxy) LexerCacheable() bool {
return it.lexerCacheable
}
//执行
func (it *ExpressionEngineProxy) LexerAndEval(expression string, arg interface{}) (interface{}, error) {
var funcItem = arg.(map[string]interface{})["func_"+expression]
if funcItem != nil {
var f = funcItem.(func(arg map[string]interface{}) interface{})
return f(arg.(map[string]interface{})), nil
}
ifElementevalExpression, err := it.Lexer(expression)
if err != nil {
return false, utils.NewError("ExpressionEngineProxy", err)
}
result, err := it.Eval(ifElementevalExpression, arg, 0)
if err != nil {
return false, utils.NewError("ExpressionEngineProxy", err)
}
return result, nil
}