Skip to content

Commit 6382f82

Browse files
authored
Do operator check after env is set (#309)
1 parent 154081e commit 6382f82

File tree

3 files changed

+32
-13
lines changed

3 files changed

+32
-13
lines changed

conf/config.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,6 @@ func (c *Config) WithEnv(env interface{}) {
5151

5252
func (c *Config) Operator(operator string, fns ...string) {
5353
c.Operators[operator] = append(c.Operators[operator], fns...)
54-
for _, fn := range fns {
55-
fnType, ok := c.Types[fn]
56-
if !ok || fnType.Type.Kind() != reflect.Func {
57-
panic(fmt.Errorf("function %s for %s operator does not exist in the environment", fn, operator))
58-
}
59-
requiredNumIn := 2
60-
if fnType.Method {
61-
requiredNumIn = 3 // As first argument of method is receiver.
62-
}
63-
if fnType.Type.NumIn() != requiredNumIn || fnType.Type.NumOut() != 1 {
64-
panic(fmt.Errorf("function %s for %s operator does not have a correct signature", fn, operator))
65-
}
66-
}
6754
}
6855

6956
func (c *Config) ConstExpr(name string) {
@@ -76,3 +63,21 @@ func (c *Config) ConstExpr(name string) {
7663
}
7764
c.ConstFns[name] = fn
7865
}
66+
67+
func (c *Config) Check() {
68+
for operator, fns := range c.Operators {
69+
for _, fn := range fns {
70+
fnType, ok := c.Types[fn]
71+
if !ok || fnType.Type.Kind() != reflect.Func {
72+
panic(fmt.Errorf("function %s for %s operator does not exist in the environment", fn, operator))
73+
}
74+
requiredNumIn := 2
75+
if fnType.Method {
76+
requiredNumIn = 3 // As first argument of method is receiver.
77+
}
78+
if fnType.Type.NumIn() != requiredNumIn || fnType.Type.NumOut() != 1 {
79+
panic(fmt.Errorf("function %s for %s operator does not have a correct signature", fn, operator))
80+
}
81+
}
82+
}
83+
}

expr.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ func Compile(input string, ops ...Option) (*vm.Program, error) {
135135
for _, op := range ops {
136136
op(config)
137137
}
138+
config.Check()
138139

139140
if len(config.Operators) > 0 {
140141
config.Visitors = append(config.Visitors, &conf.OperatorPatcher{

expr_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,19 @@ func TestOperator_struct(t *testing.T) {
490490
require.Equal(t, true, output)
491491
}
492492

493+
func TestOperator_options_another_order(t *testing.T) {
494+
code := `BirthDay == "2017-10-23"`
495+
_, err := expr.Compile(code, expr.Operator("==", "DateEqual"), expr.Env(&mockEnv{}))
496+
require.NoError(t, err)
497+
}
498+
499+
func TestOperator_no_env(t *testing.T) {
500+
code := `BirthDay == "2017-10-23"`
501+
require.Panics(t, func() {
502+
_, _ = expr.Compile(code, expr.Operator("==", "DateEqual"))
503+
})
504+
}
505+
493506
func TestOperator_interface(t *testing.T) {
494507
env := &mockEnv{
495508
Ticket: &ticket{Price: 100},

0 commit comments

Comments
 (0)