@@ -12,9 +12,12 @@ import (
12
12
"github.com/antonmedv/expr/vm"
13
13
)
14
14
15
+ // Option for configuring config.
16
+ type Option func (c * conf.Config )
17
+
15
18
// Eval parses, compiles and runs given input.
16
19
func Eval (input string , env interface {}) (interface {}, error ) {
17
- if _ , ok := env .(conf. Option ); ok {
20
+ if _ , ok := env .(Option ); ok {
18
21
return nil , fmt .Errorf ("misused expr.Eval: second argument (env) should be passed without expr.Env" )
19
22
}
20
23
@@ -41,7 +44,7 @@ func Eval(input string, env interface{}) (interface{}, error) {
41
44
// as well as all fields of embedded structs and struct itself.
42
45
// If map is passed, all items will be treated as variables.
43
46
// Methods defined on this type will be available as functions.
44
- func Env (i interface {}) conf. Option {
47
+ func Env (i interface {}) Option {
45
48
return func (c * conf.Config ) {
46
49
if _ , ok := i .(map [string ]interface {}); ok {
47
50
c .MapEnv = true
@@ -51,42 +54,42 @@ func Env(i interface{}) conf.Option {
51
54
}
52
55
53
56
// Operator allows to override binary operator with function.
54
- func Operator (operator string , fn ... string ) conf. Option {
57
+ func Operator (operator string , fn ... string ) Option {
55
58
return func (c * conf.Config ) {
56
59
c .Operators [operator ] = append (c .Operators [operator ], fn ... )
57
60
}
58
61
}
59
62
60
63
// AsBool tells the compiler to expect boolean result.
61
- func AsBool () conf. Option {
64
+ func AsBool () Option {
62
65
return func (c * conf.Config ) {
63
66
c .Expect = reflect .Bool
64
67
}
65
68
}
66
69
67
70
// AsInt64 tells the compiler to expect int64 result.
68
- func AsInt64 () conf. Option {
71
+ func AsInt64 () Option {
69
72
return func (c * conf.Config ) {
70
73
c .Expect = reflect .Int64
71
74
}
72
75
}
73
76
74
77
// AsFloat64 tells the compiler to expect float64 result.
75
- func AsFloat64 () conf. Option {
78
+ func AsFloat64 () Option {
76
79
return func (c * conf.Config ) {
77
80
c .Expect = reflect .Float64
78
81
}
79
82
}
80
83
81
84
// Optimize turns optimizations on or off.
82
- func Optimize (b bool ) conf. Option {
85
+ func Optimize (b bool ) Option {
83
86
return func (c * conf.Config ) {
84
87
c .Optimize = b
85
88
}
86
89
}
87
90
88
91
// Compile parses and compiles given input expression to bytecode program.
89
- func Compile (input string , ops ... conf. Option ) (* vm.Program , error ) {
92
+ func Compile (input string , ops ... Option ) (* vm.Program , error ) {
90
93
config := & conf.Config {
91
94
Operators : make (map [string ][]string ),
92
95
Optimize : true ,
@@ -128,7 +131,7 @@ func Compile(input string, ops ...conf.Option) (*vm.Program, error) {
128
131
// Parse parses input string to a program.
129
132
//
130
133
// Deprecated: use expr.Compile instead.
131
- func Parse (input string , ops ... conf. Option ) (* vm.Program , error ) {
134
+ func Parse (input string , ops ... Option ) (* vm.Program , error ) {
132
135
return Compile (input , ops ... )
133
136
}
134
137
0 commit comments