Skip to content

Commit 4d40254

Browse files
committed
Add extra check of correct eval usage
1 parent 9402acd commit 4d40254

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

expr.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package expr
22

33
import (
4+
"fmt"
45
"reflect"
56

67
"github.com/antonmedv/expr/checker"
@@ -13,6 +14,10 @@ import (
1314

1415
// Eval parses, compiles and runs given input.
1516
func Eval(input string, env interface{}) (interface{}, error) {
17+
if _, ok := env.(conf.Option); ok {
18+
return nil, fmt.Errorf("misused expr.Eval: second argument (env) should be passed without expr.Env")
19+
}
20+
1621
tree, err := parser.Parse(input)
1722
if err != nil {
1823
return nil, err

expr_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,3 +903,9 @@ type segment struct {
903903
Destination string
904904
Date time.Time
905905
}
906+
907+
func TestExpr_eval_with_env(t *testing.T) {
908+
_, err := expr.Eval("true", expr.Env(map[string]interface{}{}))
909+
assert.Error(t, err)
910+
assert.Contains(t, err.Error(), "misused")
911+
}

0 commit comments

Comments
 (0)