fix: unittests for CallFunc

This commit is contained in:
debugtalk
2022-01-15 21:39:48 +08:00
parent 875f2fadd3
commit f41c489699
2 changed files with 221 additions and 20 deletions

View File

@@ -3,24 +3,97 @@ package shared
import (
"fmt"
"reflect"
"github.com/rs/zerolog/log"
)
// CallFunc calls function with arguments
// it is used when calling go plugin or builtin functions
func CallFunc(fn reflect.Value, args ...interface{}) (interface{}, error) {
fnArgsNum := fn.Type().NumIn()
if fnArgsNum > 0 && fn.Type().In(fnArgsNum-1).Kind() == reflect.Slice {
// last argument is slice, do not check arguments number
// e.g. ...interface{}
// e.g. a, b string, c ...interface{}
} else if fnArgsNum != len(args) {
// function arguments not match
return nil, fmt.Errorf("function arguments number not match, expect %d, got %d", fnArgsNum, len(args))
}
// arguments do not have slice, and arguments number matched
argumentsValue := make([]reflect.Value, len(args))
for index, argument := range args {
// function expect 0 argument
if fnArgsNum == 0 {
if len(args) > 0 {
return nil, fmt.Errorf("function expect 0 argument, but got %d", len(args))
}
return call(fn, nil)
}
// fnArgsNum > 0: function expect more than 0 arguments
// function last argument is not slice
if fn.Type().In(fnArgsNum-1).Kind() != reflect.Slice {
// function arguments should match exactly
if fnArgsNum != len(args) {
return nil, fmt.Errorf("function expect %d arguments, but got %d", fnArgsNum, len(args))
}
// prepare arguments
argumentsValue, err := convertFirstNArgs(fn, fnArgsNum, args...)
if err != nil {
return nil, err
}
return call(fn, argumentsValue)
}
// function last argument is slice
// e.g.
// ...interface{}
// a, b string, c ...interface{}
// []int
// a, b string, c []int
// arguments number should >= fnArgsNum-1
if len(args) < fnArgsNum-1 {
return nil, fmt.Errorf("function expect at least %d arguments, but got %d", fnArgsNum-1, len(args))
}
// prepare first n-1 arguments
argumentsValue, err := convertFirstNArgs(fn, fnArgsNum-1, args...)
if err != nil {
return nil, err
}
// prepare last slice argument
fnLastSliceElemType := fn.Type().In(fnArgsNum - 1).Elem() // slice element type
for i := fnArgsNum - 1; i < len(args); i++ { // loop last len(args)-fnArgsNum+1 arguments
argValue := reflect.ValueOf(args[i])
argValueType := reflect.TypeOf(args[i])
// check if slice element type match
if fnLastSliceElemType == argValueType {
argumentsValue = append(argumentsValue, argValue)
continue
}
// last argument is also slice, e.g. []int
if argValueType.Kind() == reflect.Slice {
if argValueType.Elem() != fnLastSliceElemType {
err := fmt.Errorf("function argument %d's slice element type is not match, expect %v, actual %v",
i, fnLastSliceElemType, argValueType)
return nil, err
}
argumentsValue = append(argumentsValue, argValue)
continue
}
// type not match, check if convertible
if !argValueType.ConvertibleTo(fnLastSliceElemType) {
// function argument type not match and not convertible
err := fmt.Errorf("function argument %d's type is neither match nor convertible, expect %v, actual %v",
i, fnLastSliceElemType, argValueType)
return nil, err
}
// convert argument to expect type
argumentsValue = append(argumentsValue, argValue.Convert(fnLastSliceElemType))
}
return call(fn, argumentsValue)
}
func convertFirstNArgs(fn reflect.Value, n int, args ...interface{}) ([]reflect.Value, error) {
argumentsValue := make([]reflect.Value, n)
for index := 0; index < n; index++ {
argument := args[index]
if argument == nil {
argumentsValue[index] = reflect.Zero(fn.Type().In(index))
continue
@@ -41,13 +114,17 @@ func CallFunc(fn reflect.Value, args ...interface{}) (interface{}, error) {
// function argument type not match and not convertible
err := fmt.Errorf("function argument %d's type is neither match nor convertible, expect %v, actual %v",
index, expectArgumentType, actualArgumentType)
log.Error().Err(err).Msg("convert arguments failed")
return nil, err
}
// convert argument to expect type
argumentsValue[index] = argumentValue.Convert(expectArgumentType)
}
return argumentsValue, nil
}
resultValues := fn.Call(argumentsValue)
func call(fn reflect.Value, args []reflect.Value) (interface{}, error) {
resultValues := fn.Call(args)
if resultValues == nil {
// no returns
return nil, nil
@@ -69,7 +146,7 @@ func CallFunc(fn reflect.Value, args ...interface{}) (interface{}, error) {
}
} else {
// return more than 2 arguments, unexpected
err := fmt.Errorf("function should return at most 2 arguments")
err := fmt.Errorf("function should return at most 2 values")
return nil, err
}
}