-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreflect.go
112 lines (95 loc) · 2.67 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
package go_reflect
import (
"reflect"
)
type (
// Reflection struct to hold reflection data
Reflection struct {
instance interface{}
reflectedValue reflect.Value
reflectedType reflect.Type
reflectedTypeName string
}
)
// GetValue returns the value reflection
func GetValue(instance interface{}) reflect.Value {
// Check if the instance is nil
if instance == nil {
return reflect.Value{}
}
return reflect.ValueOf(instance)
}
// GetDereferencedValue returns the dereferenced value reflection
func GetDereferencedValue(instance interface{}) reflect.Value {
// Reflect data
valueReflection := GetValue(instance)
// If data is a pointer, dereference it
if valueReflection.Kind() == reflect.Ptr {
valueReflection = valueReflection.Elem()
}
return valueReflection
}
// GetType returns the type reflection
func GetType(instance interface{}) reflect.Type {
// Check if the instance is nil
if instance == nil {
return nil
}
return reflect.TypeOf(instance)
}
// GetDereferencedType returns the dereferenced type reflection
func GetDereferencedType(instance interface{}) reflect.Type {
// Reflect data
typeReflection := GetType(instance)
// If data is a pointer, dereference it
if typeReflection.Kind() == reflect.Ptr {
typeReflection = typeReflection.Elem()
}
return typeReflection
}
// GetTypeName returns the type name
func GetTypeName(typeReflection reflect.Type) string {
return typeReflection.Name()
}
// NewReflection creates a new reflection from an instance
func NewReflection(instance interface{}) *Reflection {
// Reflect data
reflectedValue := GetValue(instance)
reflectedType := GetType(instance)
reflectedTypeName := GetTypeName(reflectedType)
return &Reflection{
instance,
reflectedValue,
reflectedType,
reflectedTypeName,
}
}
// NewDereferencedReflection creates a new reflection from a dereferenced instance
func NewDereferencedReflection(instance interface{}) *Reflection {
// Reflect data
reflectedValue := GetDereferencedValue(instance)
reflectedType := GetDereferencedType(instance)
reflectedTypeName := GetTypeName(reflectedType)
return &Reflection{
instance,
reflectedValue,
reflectedType,
reflectedTypeName,
}
}
// GetInstance returns the instance
func (r *Reflection) GetInstance() interface{} {
return r.instance
}
// GetReflectedValue returns the reflected value
func (r *Reflection) GetReflectedValue() reflect.Value {
return r.reflectedValue
}
// GetReflectedType returns the reflected type
func (r *Reflection) GetReflectedType() reflect.Type {
return r.reflectedType
}
// GetReflectedTypeName returns the reflected type name
func (r *Reflection) GetReflectedTypeName() string {
return r.reflectedTypeName
}