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

Operator overloading behavior change since 1.15.4 #548

Closed
illyabusigin opened this issue Feb 3, 2024 · 3 comments
Closed

Operator overloading behavior change since 1.15.4 #548

illyabusigin opened this issue Feb 3, 2024 · 3 comments
Labels

Comments

@illyabusigin
Copy link

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

  1. Run the following commands:
mkdir exprbug
cd exprbug
go mod init exprbug
go get github.com/antonmedv/[email protected]
go get github.com/expr-lang/expr
go get github.com/stretchr/testify
touch exprbug_test.go
  1. 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)
}
@antonmedv antonmedv added the bug label Feb 6, 2024
@antonmedv
Copy link
Member

The bug is reproducible. But we need to debug to understand root cause.

@antonmedv
Copy link
Member

I've fixed the bug! I was surprised on why this was working before.

I added you tests case and one mine, so this should work in the future for sure.

Added recursive appliance of operator overload patcher.

@illyabusigin
Copy link
Author

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants