/
expeval.go
231 lines (187 loc) · 5.54 KB
/
expeval.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package expeval
import (
"errors"
"go/constant"
"go/token"
"go/types"
"reflect"
)
var (
unknownInt64Err = errors.New("unknown int64 value")
unknownUint64Err = errors.New("unknown uint64 value")
unknownFloat64Err = errors.New("unknown float64 value")
invalidValueErr = errors.New("invalid value")
)
// A ExpEval is expression evaluation
type ExpEval struct {
name string
path string
}
// NewExpEval returns the *expeval.ExpEval
func NewExpEval(name, path string) *ExpEval {
return &ExpEval{
name: name,
path: path,
}
}
func (e *ExpEval) setValues(pkg *types.Package, values map[string]interface{}) {
for key, val := range values {
r := reflect.ValueOf(val)
switch r.Kind() {
case reflect.Bool:
v := r.Bool()
pkg.Scope().Insert(types.NewConst(token.NoPos, pkg, key, types.Typ[types.Bool], constant.MakeBool(v)))
case reflect.String:
v := r.String()
pkg.Scope().Insert(types.NewConst(token.NoPos, pkg, key, types.Typ[types.String], constant.MakeString(v)))
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
v := r.Int()
pkg.Scope().Insert(types.NewConst(token.NoPos, pkg, key, types.Typ[types.Int64], constant.MakeInt64(v)))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
v := r.Uint()
pkg.Scope().Insert(types.NewConst(token.NoPos, pkg, key, types.Typ[types.Uint64], constant.MakeUint64(v)))
case reflect.Float32, reflect.Float64:
v := r.Float()
pkg.Scope().Insert(types.NewConst(token.NoPos, pkg, key, types.Typ[types.Float64], constant.MakeFloat64(v)))
}
}
}
// Eval returns the evaluated value(interface{})
func (e *ExpEval) Eval(expr string, values map[string]interface{}) (interface{}, error) {
pkg := types.NewPackage(e.name, e.path)
e.setValues(pkg, values)
var i interface{}
tav, err := types.Eval(token.NewFileSet(), pkg, token.NoPos, expr)
if err != nil {
return i, err
}
if tav.Type == nil {
return i, invalidValueErr
}
var b bool
switch tav.Type.(*types.Basic).Kind() {
case types.Bool, types.UntypedBool:
i = constant.BoolVal(tav.Value)
case types.String, types.UntypedString:
i = constant.StringVal(tav.Value)
case types.Int, types.Int8, types.Int16, types.Int32, types.Int64, types.UntypedInt:
i, b = constant.Int64Val(tav.Value)
if !b {
return i, unknownInt64Err
}
case types.Uint, types.Uint8, types.Uint16, types.Uint32, types.Uint64:
i, b = constant.Uint64Val(tav.Value)
if !b {
return i, unknownUint64Err
}
case types.Float32, types.Float64, types.UntypedFloat:
i, b = constant.Float64Val(tav.Value)
if !b {
return i, unknownFloat64Err
}
}
return i, nil
}
// EvalToBool returns the evaluated value(bool)
func (e *ExpEval) EvalToBool(expr string, values map[string]interface{}) (bool, error) {
pkg := types.NewPackage(e.name, e.path)
e.setValues(pkg, values)
tav, err := types.Eval(token.NewFileSet(), pkg, token.NoPos, expr)
if err != nil {
return false, err
}
if tav.Type == nil {
return false, invalidValueErr
}
switch tav.Type.(*types.Basic).Kind() {
case types.Bool, types.UntypedBool:
return constant.BoolVal(tav.Value), nil
}
return false, invalidValueErr
}
// EvalToString returns the evaluated value(string)
func (e *ExpEval) EvalToString(expr string, values map[string]interface{}) (string, error) {
pkg := types.NewPackage(e.name, e.path)
e.setValues(pkg, values)
tav, err := types.Eval(token.NewFileSet(), pkg, token.NoPos, expr)
if err != nil {
return "", err
}
if tav.Type == nil {
return "", invalidValueErr
}
switch tav.Type.(*types.Basic).Kind() {
case types.String, types.UntypedString:
return constant.StringVal(tav.Value), nil
}
return "", invalidValueErr
}
// EvalToInt64 returns the evaluated value(int64)
func (e *ExpEval) EvalToInt64(expr string, values map[string]interface{}) (int64, error) {
pkg := types.NewPackage(e.name, e.path)
e.setValues(pkg, values)
var i int64
tav, err := types.Eval(token.NewFileSet(), pkg, token.NoPos, expr)
if err != nil {
return i, err
}
if tav.Type == nil {
return i, invalidValueErr
}
switch tav.Type.(*types.Basic).Kind() {
case types.Int, types.Int8, types.Int16, types.Int32, types.Int64, types.UntypedInt:
var b bool
i, b = constant.Int64Val(tav.Value)
if !b {
return i, unknownInt64Err
}
return i, nil
}
return i, invalidValueErr
}
// EvalToUint64 returns the evaluated value(uint64)
func (e *ExpEval) EvalToUint64(expr string, values map[string]interface{}) (uint64, error) {
pkg := types.NewPackage(e.name, e.path)
e.setValues(pkg, values)
var i uint64
tav, err := types.Eval(token.NewFileSet(), pkg, token.NoPos, expr)
if err != nil {
return i, err
}
if tav.Type == nil {
return i, invalidValueErr
}
switch tav.Type.(*types.Basic).Kind() {
case types.Uint, types.Uint8, types.Uint16, types.Uint32, types.Uint64:
var b bool
i, b = constant.Uint64Val(tav.Value)
if !b {
return i, unknownUint64Err
}
return i, nil
}
return i, invalidValueErr
}
// EvalToFloat64 returns the evaluated value(float64)
func (e *ExpEval) EvalToFloat64(expr string, values map[string]interface{}) (float64, error) {
pkg := types.NewPackage(e.name, e.path)
e.setValues(pkg, values)
var f float64
tav, err := types.Eval(token.NewFileSet(), pkg, token.NoPos, expr)
if err != nil {
return f, err
}
if tav.Type == nil {
return f, invalidValueErr
}
switch tav.Type.(*types.Basic).Kind() {
case types.Float32, types.Float64, types.UntypedFloat:
var b bool
f, b = constant.Float64Val(tav.Value)
if !b {
return f, unknownFloat64Err
}
return f, nil
}
return f, invalidValueErr
}