Skip to content

Commit

Permalink
Fix builtin type checks for any
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Aug 21, 2023
1 parent cc5d294 commit c2c7603
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
8 changes: 8 additions & 0 deletions builtin/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,8 @@ var Builtins = []*Function{
}
for _, arg := range args {
switch kind(arg) {
case reflect.Interface:
return anyType, nil
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:
default:
return anyType, fmt.Errorf("invalid argument for max (type %s)", arg)
Expand All @@ -306,6 +308,8 @@ var Builtins = []*Function{
}
for _, arg := range args {
switch kind(arg) {
case reflect.Interface:
return anyType, nil
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:
default:
return anyType, fmt.Errorf("invalid argument for min (type %s)", arg)
Expand Down Expand Up @@ -493,6 +497,8 @@ var Builtins = []*Function{
return anyType, fmt.Errorf("invalid number of arguments (expected 1, got %d)", len(args))
}
switch kind(args[0]) {
case reflect.Interface:
return arrayType, nil
case reflect.Map:
return arrayType, nil
}
Expand Down Expand Up @@ -521,6 +527,8 @@ var Builtins = []*Function{
return anyType, fmt.Errorf("invalid number of arguments (expected 1, got %d)", len(args))
}
switch kind(args[0]) {
case reflect.Interface:
return arrayType, nil
case reflect.Map:
return arrayType, nil
}
Expand Down
27 changes: 27 additions & 0 deletions builtin/builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package builtin_test
import (
"fmt"
"reflect"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -104,6 +105,32 @@ func TestBuiltin(t *testing.T) {
}
}

func TestBuiltin_works_with_any(t *testing.T) {
config := map[string]struct {
arity int
}{
"get": {2},
}

for _, b := range builtin.Builtins {
t.Run(b.Name, func(t *testing.T) {
arity := 1
if c, ok := config[b.Name]; ok {
arity = c.arity
}
if len(b.Types) > 0 {
arity = b.Types[0].NumIn()
}
args := make([]string, arity)
for i := 1; i <= arity; i++ {
args[i-1] = fmt.Sprintf("arg%d", i)
}
_, err := expr.Compile(fmt.Sprintf(`%s(%s)`, b.Name, strings.Join(args, ", "))) // expr.Env(env) is not needed
assert.NoError(t, err)
})
}
}

func TestBuiltin_errors(t *testing.T) {
var errorTests = []struct {
input string
Expand Down

0 comments on commit c2c7603

Please sign in to comment.