-
Notifications
You must be signed in to change notification settings - Fork 10
/
reflect.go
152 lines (124 loc) · 3.13 KB
/
reflect.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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package util
import (
"reflect"
)
// ReplaceFuncVar for replace funcVar with fn
func ReplaceFuncVar(funcVarPtr interface{}, fn func(in []reflect.Value) (out []reflect.Value)) {
v, ok := funcVarPtr.(reflect.Value)
if !ok {
v = reflect.ValueOf(funcVarPtr)
}
v = reflect.Indirect(v)
if v.Kind() != reflect.Func {
panic("funcVarPtr must point to a func")
}
v.Set(reflect.MakeFunc(v.Type(), fn))
}
// Func2Value wraps a func with reflect.Value
func Func2Value(fun interface{}) reflect.Value {
v := reflect.ValueOf(fun)
if v.Kind() != reflect.Func {
panic("fun must be a func")
}
return v
}
// FuncInputTypes for retrieve func input types
func FuncInputTypes(fun interface{}) (result []reflect.Type) {
fv, ok := fun.(reflect.Value)
if !ok {
fv = reflect.ValueOf(fun)
}
if fv.Kind() != reflect.Func {
panic("fun must be a func")
}
tp := fv.Type()
n := tp.NumIn()
for i := 0; i < n; i++ {
result = append(result, tp.In(i))
}
return
}
// FuncOutputTypes for retrieve func output types
func FuncOutputTypes(fun interface{}) (result []reflect.Type) {
fv, ok := fun.(reflect.Value)
if !ok {
fv = reflect.ValueOf(fun)
}
if fv.Kind() != reflect.Func {
panic("fun must be a func")
}
tp := fv.Type()
n := tp.NumOut()
for i := 0; i < n; i++ {
result = append(result, tp.Out(i))
}
return
}
// TypeByPointer for retrieve reflect.Type by a pointer value
func TypeByPointer(tp interface{}) reflect.Type {
return reflect.TypeOf(tp).Elem()
}
// InstanceByType returns a instance of reflect.Type wrapped in interface{}
func InstanceByType(t reflect.Type) interface{} {
return reflect.New(t).Elem().Interface()
}
// InstancePtrByType returns a pointer to instance
func InstancePtrByType(t reflect.Type) interface{} {
return reflect.New(t).Interface()
}
// InstancePtrByClone creates an instance ptr by clone
func InstancePtrByClone(v reflect.Value) interface{} {
cv := reflect.New(v.Type())
cv.Elem().Set(v)
return cv.Interface()
}
// StructFieldValues for manipulate field values
func StructFieldValues(s interface{}, filter func(name string, f reflect.Value) bool) (fields map[string]reflect.Value) {
v, ok := s.(reflect.Value)
if !ok {
v = reflect.ValueOf(s)
}
v = reflect.Indirect(v)
fields = make(map[string]reflect.Value)
t := v.Type()
count := v.NumField()
for i := 0; i < count; i++ {
field := v.Field(i)
name := t.Field(i).Name
if filter == nil {
fields[name] = field
} else if filter(name, field) {
fields[name] = field
}
}
return
}
// StructFields for general struct field info
func StructFields(s interface{}) (fields []reflect.StructField) {
v, ok := s.(reflect.Value)
if !ok {
v = reflect.ValueOf(s)
}
v = reflect.Indirect(v)
t := v.Type()
count := v.NumField()
for i := 0; i < count; i++ {
field := t.Field(i)
fields = append(fields, field)
}
return
}
// ScanMethods for scan methods of s
func ScanMethods(s interface{}) (methods map[string]reflect.Value) {
v, ok := s.(reflect.Value)
if !ok {
v = reflect.ValueOf(s)
}
methods = make(map[string]reflect.Value)
t := v.Type()
count := v.NumMethod()
for i := 0; i < count; i++ {
methods[t.Method(i).Name] = v.Method(i)
}
return
}