Skip to content

Commit c2c7603

Browse files
committed
Fix builtin type checks for any
1 parent cc5d294 commit c2c7603

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

Diff for: builtin/builtin.go

+8
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,8 @@ var Builtins = []*Function{
289289
}
290290
for _, arg := range args {
291291
switch kind(arg) {
292+
case reflect.Interface:
293+
return anyType, nil
292294
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Float32, reflect.Float64:
293295
default:
294296
return anyType, fmt.Errorf("invalid argument for max (type %s)", arg)
@@ -306,6 +308,8 @@ var Builtins = []*Function{
306308
}
307309
for _, arg := range args {
308310
switch kind(arg) {
311+
case reflect.Interface:
312+
return anyType, nil
309313
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Float32, reflect.Float64:
310314
default:
311315
return anyType, fmt.Errorf("invalid argument for min (type %s)", arg)
@@ -493,6 +497,8 @@ var Builtins = []*Function{
493497
return anyType, fmt.Errorf("invalid number of arguments (expected 1, got %d)", len(args))
494498
}
495499
switch kind(args[0]) {
500+
case reflect.Interface:
501+
return arrayType, nil
496502
case reflect.Map:
497503
return arrayType, nil
498504
}
@@ -521,6 +527,8 @@ var Builtins = []*Function{
521527
return anyType, fmt.Errorf("invalid number of arguments (expected 1, got %d)", len(args))
522528
}
523529
switch kind(args[0]) {
530+
case reflect.Interface:
531+
return arrayType, nil
524532
case reflect.Map:
525533
return arrayType, nil
526534
}

Diff for: builtin/builtin_test.go

+27
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package builtin_test
33
import (
44
"fmt"
55
"reflect"
6+
"strings"
67
"testing"
78
"time"
89

@@ -104,6 +105,32 @@ func TestBuiltin(t *testing.T) {
104105
}
105106
}
106107

108+
func TestBuiltin_works_with_any(t *testing.T) {
109+
config := map[string]struct {
110+
arity int
111+
}{
112+
"get": {2},
113+
}
114+
115+
for _, b := range builtin.Builtins {
116+
t.Run(b.Name, func(t *testing.T) {
117+
arity := 1
118+
if c, ok := config[b.Name]; ok {
119+
arity = c.arity
120+
}
121+
if len(b.Types) > 0 {
122+
arity = b.Types[0].NumIn()
123+
}
124+
args := make([]string, arity)
125+
for i := 1; i <= arity; i++ {
126+
args[i-1] = fmt.Sprintf("arg%d", i)
127+
}
128+
_, err := expr.Compile(fmt.Sprintf(`%s(%s)`, b.Name, strings.Join(args, ", "))) // expr.Env(env) is not needed
129+
assert.NoError(t, err)
130+
})
131+
}
132+
}
133+
107134
func TestBuiltin_errors(t *testing.T) {
108135
var errorTests = []struct {
109136
input string

0 commit comments

Comments
 (0)