-
Notifications
You must be signed in to change notification settings - Fork 0
/
reflect_helper.go
116 lines (100 loc) · 3.09 KB
/
reflect_helper.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
package reflectx
import (
"reflect"
)
// Create new instance(interface{}) by type
func CreateInstance(objectType reflect.Type) interface{} {
var ins reflect.Value
ins = reflect.New(objectType)
if objectType.Kind() == reflect.Struct {
ins = ins.Elem()
}
return ins.Interface()
}
// Create new instance(Ptr) by type
func CreateInstancePtr(objectType reflect.Type) interface{} {
var ins reflect.Value
ins = reflect.New(objectType)
return ins
}
// Get ctor function return type's name and reflect.Type.
func GetCtorFuncOutTypeName(ctorFunc interface{}) (string, reflect.Type) {
typeInfo, err := GetTypeInfo(ctorFunc)
if err != nil {
panic(err.Error())
}
return typeInfo.Name, typeInfo.Type
}
// Get method info
func getMethodInfo(method reflect.Method) MethodInfo {
methodInfo := MethodInfo{}
methodInfo.Name = method.Name
methodInfo.MethodInfoType = method.Type
paramsCount := method.Type.NumIn()
methodInfo.Parameters = make([]MethodParameterInfo, paramsCount)
for idx := 0; idx < paramsCount; idx++ {
methodInfo.Parameters[idx].ParameterType = methodInfo.MethodInfoType.In(idx)
parameterType := methodInfo.Parameters[idx].ParameterType
if parameterType.Kind() == reflect.Ptr {
parameterType = parameterType.Elem()
}
methodInfo.Parameters[idx].Name = parameterType.Name()
}
if methodInfo.MethodInfoType.NumOut() > 0 {
methodInfo.OutType = methodInfo.MethodInfoType.Out(0)
}
return methodInfo
}
// Get instance's method list
func GetObjectMethodInfoList(object interface{}) []MethodInfo {
objectType := reflect.TypeOf(object)
return GetObjectMethodInfoListWithValueType(objectType)
}
// Get method list by reflect.Type
func GetObjectMethodInfoListWithValueType(objectType reflect.Type) []MethodInfo {
methodCount := objectType.NumMethod()
methodInfos := make([]MethodInfo, methodCount)
for idx := 0; idx < methodCount; idx++ {
methodInfo := getMethodInfo(objectType.Method(idx))
methodInfos[idx] = methodInfo
}
return methodInfos
}
// Get Instance's MethodInfo By Method Name and reflect.Type
func GetObjectMethodInfoByName(object interface{}, methodName string) (MethodInfo, bool) {
objType := reflect.TypeOf(object)
return GetObjectMethodInfoByNameWithType(objType, methodName)
}
// Get Instance's MethodInfo By Method Name and reflect.Type
func GetObjectMethodInfoByNameWithType(objectType reflect.Type, methodName string) (MethodInfo, bool) {
var methodInfo MethodInfo
methodType, rbl := objectType.MethodByName(methodName)
if rbl {
methodInfo = getMethodInfo(methodType)
}
return methodInfo, rbl
}
// Get final type, means the type is Ptr elem or else.
func GetFinalType(outType reflect.Type) (reflect.Type, bool) {
var cType reflect.Type
isPtr := false
if outType.Kind() != reflect.Ptr {
cType = outType
} else {
isPtr = true
cType = outType.Elem()
}
return cType, isPtr
}
// Get final value, means the type is Ptr elem or else.
func GetFinalValue(outValue reflect.Value) (reflect.Value, bool) {
var cValue reflect.Value
isPtr := false
if outValue.Kind() != reflect.Ptr {
cValue = outValue
} else {
isPtr = true
cValue = outValue.Elem()
}
return cValue, isPtr
}