-
Notifications
You must be signed in to change notification settings - Fork 58
/
func.go
55 lines (44 loc) · 941 Bytes
/
func.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package cherryReflect
import (
"reflect"
cerr "github.com/cherry-game/cherry/error"
)
var (
nilFuncInfo = FuncInfo{}
)
type FuncInfo struct {
Type reflect.Type
Value reflect.Value
InArgs []reflect.Type
InArgsLen int
OutArgs []reflect.Type
OutArgsLen int
}
func GetFuncInfo(fn interface{}) (FuncInfo, error) {
if fn == nil {
return nilFuncInfo, cerr.FuncIsNil
}
typ := reflect.TypeOf(fn)
if typ.Kind() != reflect.Func {
return nilFuncInfo, cerr.FuncTypeError
}
var inArgs []reflect.Type
for i := 0; i < typ.NumIn(); i++ {
t := typ.In(i)
inArgs = append(inArgs, t)
}
var outArgs []reflect.Type
for i := 0; i < typ.NumOut(); i++ {
t := typ.Out(i)
outArgs = append(outArgs, t)
}
funcInfo := FuncInfo{
Type: typ,
Value: reflect.ValueOf(fn),
InArgs: inArgs,
InArgsLen: typ.NumIn(),
OutArgs: outArgs,
OutArgsLen: typ.NumOut(),
}
return funcInfo, nil
}