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

feat(runtime): support pointer equality and custom integer comparisons #752

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
24 changes: 24 additions & 0 deletions expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,7 @@ func TestExpr(t *testing.T) {
date := time.Date(2017, time.October, 23, 18, 30, 0, 0, time.UTC)
oneDay, _ := time.ParseDuration("24h")
timeNowPlusOneDay := date.Add(oneDay)
mode := mock.ModeEnum(1)

env := mock.Env{
Embed: mock.Embed{},
Expand All @@ -643,6 +644,7 @@ func TestExpr(t *testing.T) {
IntPtr: nil,
IntPtrPtr: nil,
StringPtr: nil,
ModePtr: &mode,
Foo: mock.Foo{
Value: "foo",
Bar: mock.Bar{
Expand Down Expand Up @@ -1291,6 +1293,11 @@ func TestExpr(t *testing.T) {
`1 < 2 < 3 == true`,
true,
},
{
// Test pointer dereferencing with custom integer type
`ModePtr == 1`,
true,
},
}

for _, tt := range tests {
Expand Down Expand Up @@ -2736,3 +2743,20 @@ func TestExpr_env_types_map_error(t *testing.T) {
_, err = expr.Run(program, envTypes)
require.Error(t, err)
}

func Example_pointerDereference() {
type Mode int
mode := Mode(1)
env := map[string]any{
"Mode": &mode,
}

output, err := expr.Eval(`Mode == 1`, env)
if err != nil {
fmt.Printf("err: %v", err)
return
}

fmt.Printf("%v", output)
// Output: true
}
3 changes: 3 additions & 0 deletions test/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"github.com/expr-lang/expr/ast"
)

type ModeEnum int

type Env struct {
Embed
Ambiguous string
Expand Down Expand Up @@ -51,6 +53,7 @@ type Env struct {
Time time.Time
TimePlusDay time.Time
Duration time.Duration
ModePtr *ModeEnum
}

func (p Env) FuncFoo(_ Foo) int {
Expand Down
53 changes: 53 additions & 0 deletions vm/runtime/helpers/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,33 @@ func cases(op string, xs ...[]string) string {
echo := func(s string, xs ...any) {
out += fmt.Sprintf(s, xs...) + "\n"
}

// Only generate pointer cases for equality operations
if op == "==" {
for _, a := range types {
// Handle *T cases
echo(`case *%v:`, a)
echo(`switch y := b.(type) {`)
echo(`case %v:`, a)
echo(`return *x == y`)
echo(`case *%v:`, a)
echo(`return *x == *y`)
echo(`}`)
}
}

// Generate regular cases
for _, a := range types {
echo(`case %v:`, a)
echo(`switch y := b.(type) {`)

// Add pointer case for equality
if op == "==" {
echo(`case *%v:`, a)
echo(`return x == *y`)
}

// Add regular type cases
for _, b := range types {
t := "int"
if isDuration(a) || isDuration(b) {
Expand Down Expand Up @@ -172,6 +196,35 @@ func Equal(a, b interface{}) bool {
return x == y
}
}

// Handle unknown pointer types and custom types
va := reflect.ValueOf(a)
vb := reflect.ValueOf(b)

// Handle pointers to unknown types
if va.Kind() == reflect.Ptr {
if !va.IsValid() || va.IsNil() {
return vb.Kind() == reflect.Ptr && (!vb.IsValid() || vb.IsNil())
}
va = va.Elem()
}
if vb.Kind() == reflect.Ptr {
if !vb.IsValid() || vb.IsNil() {
return va.Kind() == reflect.Ptr && (!va.IsValid() || va.IsNil())
}
vb = vb.Elem()
}

// Handle custom integer types by converting to int64
if va.IsValid() && vb.IsValid() {
ka := va.Kind()
kb := vb.Kind()
if (ka == reflect.Int || ka == reflect.Int8 || ka == reflect.Int16 || ka == reflect.Int32 || ka == reflect.Int64) &&
(kb == reflect.Int || kb == reflect.Int8 || kb == reflect.Int16 || kb == reflect.Int32 || kb == reflect.Int64) {
return va.Int() == vb.Int()
}
}

if IsNil(a) && IsNil(b) {
return true
}
Expand Down
137 changes: 137 additions & 0 deletions vm/runtime/helpers[generated].go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion vm/runtime/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/expr-lang/expr/vm/runtime"
)

type CustomInt int

var tests = []struct {
name string
a, b any
Expand All @@ -33,6 +35,11 @@ var tests = []struct {
{"deep []any != []any", []any{[]int{1}, 2, []any{"3", "42"}}, []any{[]any{1}, 2, []string{"3"}}, false},
{"map[string]any == map[string]any", map[string]any{"a": 1}, map[string]any{"a": 1}, true},
{"map[string]any != map[string]any", map[string]any{"a": 1}, map[string]any{"a": 1, "b": 2}, false},
{name: "*CustomInt == int", a: func() any { x := CustomInt(1); return &x }(), b: 1, want: true},
{name: "int == *CustomInt", a: 1, b: func() any { x := CustomInt(1); return &x }(), want: true},
{name: "*CustomInt != int", a: func() any { x := CustomInt(2); return &x }(), b: 1, want: false},
{name: "*CustomInt == *CustomInt", a: func() any { x := CustomInt(1); return &x }(), b: func() any { x := CustomInt(1); return &x }(), want: true},
{name: "*CustomInt != *CustomInt", a: func() any { x := CustomInt(1); return &x }(), b: func() any { x := CustomInt(2); return &x }(), want: false},
}

func TestEqual(t *testing.T) {
Expand All @@ -44,7 +51,6 @@ func TestEqual(t *testing.T) {
assert.Equal(t, tt.want, got, "Equal(%v, %v) = %v; want %v", tt.b, tt.a, got, tt.want)
})
}

}

func BenchmarkEqual(b *testing.B) {
Expand Down
Loading