Closed
Description
This code runs correctly in 1.16.0
type customOperation struct {
name string
operation string
}
func (o customOperation) Name() string {
return o.name
}
func (o customOperation) Code() string {
return o.operation
}
func (o customOperation) Expr() func(interface{}, interface{}) interface{} {
return func(arg1 interface{}, arg2 interface{}) interface{} {
a, _ := arg1.(float64)
b, _ := arg2.(float64)
return a * b
}
}
func TestExprLanguage(t *testing.T) {
exp := "263.5 * 4.63"
operator := customOperation{name: "*", operation: "multiplyNumbers"}
options := []expr.Option{
expr.Operator(operator.Code(), operator.Name()),
expr.Env(map[string]interface{}{operator.Name(): operator.Expr()}),
}
_, err := expr.Compile(exp, options...)
assert.True(t, err == nil)
}
But after upgrade to 1.16.1 this expression not compile.
--- FAIL: TestExprLanguage (0.00s)
panic: function * for multiplyNumbers operator does not exist in the environment [recovered]
panic: function * for multiplyNumbers operator does not exist in the environment
goroutine 36 [running]:
testing.tRunner.func1.2({0x10132ada0, 0x14000285a30})
/opt/go/libexec/src/testing/testing.go:1631 +0x1c4
testing.tRunner.func1()
/go/libexec/src/testing/testing.go:1634 +0x33c
panic({0x10132ada0?, 0x14000285a30?})
/opt/go/libexec/src/runtime/panic.go:770 +0x124
github.com/expr-lang/expr/patcher.(*OperatorOverloading).Check(0x140002b3280)
/vendor/github.com/expr-lang/expr/patcher/operator_override.go:109 +0x21c
github.com/expr-lang/expr/conf.(*Config).Check(0x140002fe080?)
/vendor/github.com/expr-lang/expr/conf/config.go:88 +0x70
github.com/expr-lang/expr.Compile({0x10116a75d, 0xc}, {0x14000088f00, 0x2, 0x14000312f18?})
/vendor/github.com/expr-lang/expr/expr.go:195 +0x1f0
github.com/melisource/rule-engine-api/src/api/expression_test.TestExprLanguage(0x140001b3d40)
/expression/expression_test.go:1232 +0x124
testing.tRunner(0x140001b3d40, 0x101434100)
/opt/go/libexec/src/testing/testing.go:1689 +0xec
created by testing.(*T).Run in goroutine 1
/opt/go/libexec/src/testing/testing.go:1742 +0x318