Skip to content

Commit 4b677b2

Browse files
fix: add test
1 parent 7094458 commit 4b677b2

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

conf/operators.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ func FindSuitableOperatorOverload(fns []string, types TypesTable, funcs Function
2020

2121
func FindSuitableOperatorOverloadInTypes(fns []string, types TypesTable, l, r reflect.Type) (reflect.Type, string, bool) {
2222
for _, fn := range fns {
23-
fnType := types[fn]
23+
fnType, ok := types[fn]
24+
if !ok {
25+
continue
26+
}
2427
firstInIndex := 0
2528
if fnType.Method {
2629
firstInIndex = 1 // As first argument to method is receiver.

test/operator/operator_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,33 @@ func TestOperator_CanBeDefinedEitherInTypesOrInFunctions(t *testing.T) {
191191
require.NoError(t, err)
192192
require.Equal(t, 3, output)
193193
}
194+
195+
func TestOperator_Polymorphic(t *testing.T) {
196+
env := struct {
197+
Add func(a, b int) int
198+
Foo Value
199+
Bar Value
200+
}{
201+
Add: func(a, b int) int {
202+
return a + b
203+
},
204+
Foo: Value{1},
205+
Bar: Value{2},
206+
}
207+
208+
program, err := expr.Compile(
209+
`1 + 2 + (Foo + Bar)`,
210+
expr.Env(env),
211+
expr.Operator("+", "Add", "AddValues"),
212+
expr.Function("AddValues", func(args ...interface{}) (interface{}, error) {
213+
return args[0].(Value).Int + args[1].(Value).Int, nil
214+
},
215+
new(func(_ Value, __ Value) int),
216+
),
217+
)
218+
require.NoError(t, err)
219+
220+
output, err := expr.Run(program, env)
221+
require.NoError(t, err)
222+
require.Equal(t, 6, output)
223+
}

0 commit comments

Comments
 (0)