Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do operator check after env is set #309

Merged
merged 1 commit into from
Jan 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,6 @@ func (c *Config) WithEnv(env interface{}) {

func (c *Config) Operator(operator string, fns ...string) {
c.Operators[operator] = append(c.Operators[operator], fns...)
for _, fn := range fns {
fnType, ok := c.Types[fn]
if !ok || fnType.Type.Kind() != reflect.Func {
panic(fmt.Errorf("function %s for %s operator does not exist in the environment", fn, operator))
}
requiredNumIn := 2
if fnType.Method {
requiredNumIn = 3 // As first argument of method is receiver.
}
if fnType.Type.NumIn() != requiredNumIn || fnType.Type.NumOut() != 1 {
panic(fmt.Errorf("function %s for %s operator does not have a correct signature", fn, operator))
}
}
}

func (c *Config) ConstExpr(name string) {
Expand All @@ -76,3 +63,21 @@ func (c *Config) ConstExpr(name string) {
}
c.ConstFns[name] = fn
}

func (c *Config) Check() {
for operator, fns := range c.Operators {
for _, fn := range fns {
fnType, ok := c.Types[fn]
if !ok || fnType.Type.Kind() != reflect.Func {
panic(fmt.Errorf("function %s for %s operator does not exist in the environment", fn, operator))
}
requiredNumIn := 2
if fnType.Method {
requiredNumIn = 3 // As first argument of method is receiver.
}
if fnType.Type.NumIn() != requiredNumIn || fnType.Type.NumOut() != 1 {
panic(fmt.Errorf("function %s for %s operator does not have a correct signature", fn, operator))
}
}
}
}
1 change: 1 addition & 0 deletions expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ func Compile(input string, ops ...Option) (*vm.Program, error) {
for _, op := range ops {
op(config)
}
config.Check()

if len(config.Operators) > 0 {
config.Visitors = append(config.Visitors, &conf.OperatorPatcher{
Expand Down
13 changes: 13 additions & 0 deletions expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,19 @@ func TestOperator_struct(t *testing.T) {
require.Equal(t, true, output)
}

func TestOperator_options_another_order(t *testing.T) {
code := `BirthDay == "2017-10-23"`
_, err := expr.Compile(code, expr.Operator("==", "DateEqual"), expr.Env(&mockEnv{}))
require.NoError(t, err)
}

func TestOperator_no_env(t *testing.T) {
code := `BirthDay == "2017-10-23"`
require.Panics(t, func() {
_, _ = expr.Compile(code, expr.Operator("==", "DateEqual"))
})
}

func TestOperator_interface(t *testing.T) {
env := &mockEnv{
Ticket: &ticket{Price: 100},
Expand Down