forked from zlyuancn/zutils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreflect.func.go
More file actions
112 lines (102 loc) · 3.18 KB
/
reflect.func.go
File metadata and controls
112 lines (102 loc) · 3.18 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package zutils
import (
"fmt"
"reflect"
"runtime"
"strings"
)
type ReflectMethod struct {
Name string // 方法名
MethodValue *reflect.Value // 反射的方法实体
Method *reflect.Method // 反射的方法
}
func (r *ReflectMethod) call(callMethod func(in []reflect.Value) []reflect.Value, in []interface{}) []interface{} {
input := make([]reflect.Value, len(in))
for i, v := range in {
input[i] = reflect.ValueOf(v)
}
result := callMethod(input)
output := make([]interface{}, len(result))
for i, r := range result {
output[i] = r.Interface()
}
return output
}
func (r *ReflectMethod) callValueN(callMethdo func(in []reflect.Value) []reflect.Value, n int, in ...interface{}) []interface{} {
result := r.call(callMethdo, in)
if len(result) != n {
panic(fmt.Errorf("%s: expect len(result) = %d, but got %d", r.Name, n, len(result)))
}
return result
}
func (r *ReflectMethod) Call(in ...interface{}) []interface{} {
return r.call(r.MethodValue.Call, in)
}
func (r *ReflectMethod) Call1(in ...interface{}) interface{} {
result := r.call(r.MethodValue.Call, in)
return result[0]
}
func (r *ReflectMethod) Call2(in ...interface{}) (interface{}, interface{}) {
result := r.call(r.MethodValue.Call, in)
return result[0], result[1]
}
func (r *ReflectMethod) Call3(in ...interface{}) (interface{}, interface{}, interface{}) {
result := r.call(r.MethodValue.Call, in)
return result[0], result[1], result[2]
}
func (r *ReflectMethod) CallSlice(in ...interface{}) []interface{} {
return r.call(r.MethodValue.CallSlice, in)
}
func (r *ReflectMethod) CallSlice1(in ...interface{}) interface{} {
result := r.call(r.MethodValue.CallSlice, in)
return result[0]
}
func (r *ReflectMethod) CallSlice2(in ...interface{}) (interface{}, interface{}) {
result := r.call(r.MethodValue.CallSlice, in)
return result[0], result[1]
}
func (r *ReflectMethod) CallSlice3(in ...interface{}) (interface{}, interface{}, interface{}) {
result := r.call(r.MethodValue.CallSlice, in)
return result[0], result[1], result[2]
}
// GetMethods 获取a的方法列表
//
// example:
// type A int
// func (*A) A(a int) int { return a + 1 }
// func (A) B(b int) int { return b + b }
// func main() {
// methods := zutils.Reflect.GetMethods(new(A))
// fmt.Println(methods["A"].Call1(123)) // 124
// fmt.Println(methods["B"].Call1(123)) // 246
// }
func (*reflectUtil) GetMethods(a interface{}) map[string]*ReflectMethod {
aType := reflect.TypeOf(a)
aValue := reflect.ValueOf(a)
out := make(map[string]*ReflectMethod)
for i := 0; i < aType.NumMethod(); i++ {
method := aType.Method(i)
if method.PkgPath != "" {
continue
}
methodValue := aValue.Method(i)
out[method.Name] = &ReflectMethod{
Name: method.Name,
MethodValue: &methodValue,
Method: &method,
}
}
return out
}
// GetFuncName 获取函数或方法的名称
func (*reflectUtil) GetFuncName(a interface{}) string {
aValue := reflect.ValueOf(a)
if aValue.Kind() != reflect.Func {
panic("a must a func")
}
rawName := runtime.FuncForPC(aValue.Pointer()).Name()
name := strings.TrimSuffix(rawName, ".func1")
ss := strings.Split(name, ".")
name = strings.TrimSuffix(ss[len(ss)-1], "-fm")
return name
}