Closed
Description
When adding operator overloads for and
I see the following error on github.com/antonmedv/expr v1.15.4 and higher". The instructions and code the reproduce the error and the success prior to v1.15.4 are below. Is there anything I can do to get this code working in later versions of expr?
interface conversion: interface {} is exprbug.Condition, not bool
Steps to Reproduce
- Run the following commands:
mkdir exprbug
cd exprbug
go mod init exprbug
go get github.com/antonmedv/expr@v1.15.3
go get github.com/expr-lang/expr
go get github.com/stretchr/testify
touch exprbug_test.go
- Paste in the following code and run the tests:
package exprbug
import (
"testing"
exprold "github.com/antonmedv/expr"
"github.com/expr-lang/expr"
"github.com/stretchr/testify/assert"
)
type Env struct{}
type Program struct {
}
func (p *Program) Foo() Value {
return func(e *Env) float64 {
return 5
}
}
func (p *Program) Bar() Value {
return func(e *Env) float64 {
return 100
}
}
func (p *Program) AndCondition(a, b Condition) Conditions {
return Conditions{a, b}
}
func (p *Program) AndConditions(a Conditions, b Condition) Conditions {
return append(a, b)
}
func (p *Program) ValueGreaterThan_float(v Value, i float64) Condition {
return func(e *Env) bool {
realized := v(e)
return realized > i
}
}
func (p *Program) ValueLessThan_float(v Value, i float64) Condition {
return func(e *Env) bool {
realized := v(e)
return realized < i
}
}
type Condition func(e *Env) bool
type Conditions []Condition
type Value func(e *Env) float64
func TestExprv1153Working(t *testing.T) {
code := `Foo() > 1.5 and Bar() < 200.0`
p := &Program{}
opt := []exprold.Option{
exprold.Env(p),
exprold.Operator("and", "AndCondition", "AndConditions"),
exprold.Operator(">", "ValueGreaterThan_float"),
exprold.Operator("<", "ValueLessThan_float"),
}
program, err := exprold.Compile(code, opt...)
assert.Nil(t, err)
state, err := exprold.Run(program, p)
assert.Nil(t, err)
assert.NotNil(t, state)
}
func TestExprv116Failing(t *testing.T) {
code := `Foo() > 1.5 and Bar() < 200.0`
p := &Program{}
opt := []expr.Option{
expr.Env(p),
expr.Operator("and", "AndCondition", "AndConditions"),
expr.Operator(">", "ValueGreaterThan_float"),
expr.Operator("<", "ValueLessThan_float"),
}
program, err := expr.Compile(code, opt...)
assert.Nil(t, err)
state, err := expr.Run(program, p)
assert.Nil(t, err)
assert.NotNil(t, state)
}