refactor: plugin structure

This commit is contained in:
debugtalk
2022-01-17 15:18:30 +08:00
parent 24546e98e6
commit 675ded099d
8 changed files with 97 additions and 96 deletions

View File

@@ -1,103 +0,0 @@
package shared
import (
"fmt"
"reflect"
"github.com/rs/zerolog/log"
)
// CallFunc calls function with arguments
func CallFunc(fn reflect.Value, args ...interface{}) (interface{}, error) {
argumentsValue, err := convertArgs(fn, args...)
if err != nil {
log.Error().Err(err).Msg("convert arguments failed")
return nil, err
}
return call(fn, argumentsValue)
}
func convertArgs(fn reflect.Value, args ...interface{}) ([]reflect.Value, error) {
fnArgsNum := fn.Type().NumIn()
// function arguments should match exactly if function's last argument is not slice
if len(args) != fnArgsNum && (fnArgsNum == 0 || fn.Type().In(fnArgsNum-1).Kind() != reflect.Slice) {
return nil, fmt.Errorf("function expect %d arguments, but got %d", fnArgsNum, len(args))
}
argumentsValue := make([]reflect.Value, len(args))
for index := 0; index < len(args); index++ {
argument := args[index]
if argument == nil {
argumentsValue[index] = reflect.Zero(fn.Type().In(index))
continue
}
argumentValue := reflect.ValueOf(argument)
actualArgumentType := reflect.TypeOf(argument)
var expectArgumentType reflect.Type
if (index == fnArgsNum-1 && fn.Type().In(fnArgsNum-1).Kind() == reflect.Slice) || index > fnArgsNum-1 {
// last fn argument is slice
expectArgumentType = fn.Type().In(fnArgsNum - 1).Elem() // slice element type
// last argument is also slice, e.g. []int
if actualArgumentType.Kind() == reflect.Slice {
if actualArgumentType.Elem() != expectArgumentType {
err := fmt.Errorf("function argument %d's slice element type is not match, expect %v, actual %v",
index, expectArgumentType, actualArgumentType)
return nil, err
}
argumentsValue[index] = argumentValue
continue
}
} else {
expectArgumentType = fn.Type().In(index)
}
// type match
if expectArgumentType == actualArgumentType {
argumentsValue[index] = argumentValue
continue
}
// type not match, check if convertible
if !actualArgumentType.ConvertibleTo(expectArgumentType) {
// 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)
return nil, err
}
// convert argument to expect type
argumentsValue[index] = argumentValue.Convert(expectArgumentType)
}
return argumentsValue, nil
}
func call(fn reflect.Value, args []reflect.Value) (interface{}, error) {
resultValues := fn.Call(args)
if resultValues == nil {
// no returns
return nil, nil
} else if len(resultValues) == 2 {
// return two arguments: interface{}, error
if resultValues[1].Interface() != nil {
return resultValues[0].Interface(), resultValues[1].Interface().(error)
} else {
return resultValues[0].Interface(), nil
}
} else if len(resultValues) == 1 {
// return one argument
if err, ok := resultValues[0].Interface().(error); ok {
// return error
return nil, err
} else {
// return interface{}
return resultValues[0].Interface(), nil
}
} else {
// return more than 2 arguments, unexpected
err := fmt.Errorf("function should return at most 2 values")
return nil, err
}
}

View File

@@ -1,208 +0,0 @@
package shared
import (
"errors"
"fmt"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
type data struct {
f interface{}
args []interface{}
expVal interface{}
expErr error
}
func TestCallFuncBasic(t *testing.T) {
params := []data{
// zero argument, zero return
{f: func() {}, args: []interface{}{}, expVal: nil, expErr: nil},
// zero argument, return one value
{f: func() int { return 1 }, args: []interface{}{}, expVal: 1, expErr: nil},
{f: func() string { return "a" }, args: []interface{}{}, expVal: "a", expErr: nil},
{f: func() interface{} { return 1.23 }, args: []interface{}{}, expVal: 1.23, expErr: nil},
// zero argument, return error
{f: func() error { return errors.New("xxx") }, args: []interface{}{}, expVal: nil, expErr: errors.New("xxx")},
// zero argument, return one value and error
{f: func() (int, error) { return 1, errors.New("xxx") }, args: []interface{}{}, expVal: 1, expErr: errors.New("xxx")},
{f: func() (interface{}, error) { return 1.23, errors.New("xxx") }, args: []interface{}{}, expVal: 1.23, expErr: errors.New("xxx")},
// one argument, zero return
{f: func(n int) {}, args: []interface{}{1}, expVal: nil, expErr: nil},
// one argument, return one value
{f: func(n int) int { return n * n }, args: []interface{}{2}, expVal: 4},
{f: func(c string) string { return c + c }, args: []interface{}{"p"}, expVal: "pp"},
{f: func(arg interface{}) interface{} { return fmt.Sprintf("%v", arg) }, args: []interface{}{1.23}, expVal: "1.23"},
// one argument, return one value and error
{f: func(arg interface{}) (interface{}, error) { return 1.23, errors.New("xxx") }, args: []interface{}{"a"}, expVal: 1.23, expErr: errors.New("xxx")},
// two arguments in same type
{f: func(a, b int) int { return a * b }, args: []interface{}{2, 3}, expVal: 6},
// two arguments in different type
{
f: func(n int, c string) string {
var s string
for i := 0; i < n; i++ {
s += c
}
return s
},
args: []interface{}{3, "p"},
expVal: "ppp",
},
// variable arguments list: ...int, ...interface{}
{
f: func(n ...int) int {
var sum int
for _, arg := range n {
sum += arg
}
return sum
},
args: []interface{}{1, 2, 3},
expVal: 6,
},
{
f: func(args ...interface{}) (interface{}, error) {
var result string
for _, arg := range args {
result += fmt.Sprintf("%v", arg)
}
return result, nil
},
args: []interface{}{1, 2.3, "4.5", "p"},
expVal: "12.34.5p",
},
{
f: func(a, b int8, n ...int) int {
var sum int
for _, arg := range n {
sum += arg
}
sum += int(a) + int(b)
return sum
},
args: []interface{}{1, 2, 3, 4.5},
expVal: 10,
},
{
f: func(a, b int8, n ...int) int {
sum := int(a) + int(b)
for _, arg := range n {
sum += arg
}
return sum
},
args: []interface{}{1, 2},
expVal: 3,
},
{
f: func(a []int, n ...int) int {
var sum int
for _, arg := range a {
sum += arg
}
for _, arg := range n {
sum += arg
}
return sum
},
args: []interface{}{[]int{1, 2}, 3, 4},
expVal: 10,
},
}
for _, p := range params {
fn := reflect.ValueOf(p.f)
val, err := CallFunc(fn, p.args...)
if !assert.Equal(t, p.expErr, err) {
t.Fatal(err)
}
if !assert.Equal(t, p.expVal, val) {
t.Fatal()
}
}
}
func TestCallFuncComplex(t *testing.T) {
params := []data{
// arguments include slice
{
f: func(a int, n []int, b int) int {
sum := a
for _, arg := range n {
sum += arg
}
sum += b
return sum
},
args: []interface{}{1, []int{2, 3}, 4},
expVal: 10,
},
// last argument is slice
{
f: func(n []int) int {
var sum int
for _, arg := range n {
sum += arg
}
return sum
},
args: []interface{}{[]int{1, 2, 3}},
expVal: 6,
},
{
f: func(a, b int, n []int) int {
sum := a + b
for _, arg := range n {
sum += arg
}
return sum
},
args: []interface{}{1, 2, []int{3, 4}},
expVal: 10,
},
}
for _, p := range params {
fn := reflect.ValueOf(p.f)
val, err := CallFunc(fn, p.args...)
if !assert.Equal(t, p.expErr, err) {
t.Fatal(err)
}
if !assert.Equal(t, p.expVal, val) {
t.Fatal()
}
}
}
func TestCallFuncAbnormal(t *testing.T) {
params := []data{
// return more than 2 values
{
f: func() (int, int, error) { return 1, 2, nil },
args: []interface{}{},
expVal: nil,
expErr: fmt.Errorf("function should return at most 2 values"),
},
}
for _, p := range params {
fn := reflect.ValueOf(p.f)
val, err := CallFunc(fn, p.args...)
if !assert.Equal(t, p.expErr, err) {
t.Fatal(err)
}
if !assert.Equal(t, p.expVal, val) {
t.Fatal()
}
}
}