-
Notifications
You must be signed in to change notification settings - Fork 4
/
reflect_accessor.go
213 lines (193 loc) · 6.74 KB
/
reflect_accessor.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package reflectkit
import (
"reflect"
"unsafe"
)
//go:generate minimock -i github.com/insolar/assured-ledger/ledger-core/vanilla/reflectkit.TypedReceiver -o ./mocks -s _mock.go -g
type TypedReceiver interface {
ReceiveBool(reflect.Kind, bool)
ReceiveInt(reflect.Kind, int64)
ReceiveUint(reflect.Kind, uint64)
ReceiveFloat(reflect.Kind, float64)
ReceiveComplex(reflect.Kind, complex128)
ReceiveString(reflect.Kind, string)
ReceiveZero(reflect.Kind)
ReceiveNil(reflect.Kind)
ReceiveIface(reflect.Kind, interface{})
ReceiveElse(t reflect.Kind, v interface{}, isZero bool)
}
type FieldGetterFunc func(reflect.Value) reflect.Value
type ValueToReceiverFunc func(value reflect.Value, out TypedReceiver)
type IfaceToReceiverFunc func(value interface{}, k reflect.Kind, out TypedReceiver)
type IfaceToReceiverFactoryFunc func(t reflect.Type, checkZero bool) IfaceToReceiverFunc
type ValueToReceiverFactoryFunc func(unexported bool, t reflect.Type, checkZero bool) (bool, ValueToReceiverFunc)
func ValueToReceiverFactory(k reflect.Kind, custom IfaceToReceiverFactoryFunc) ValueToReceiverFactoryFunc {
switch k {
// ======== Simple values, are safe to read from unexported fields ============
case reflect.Bool:
return func(_ bool, _ reflect.Type, checkZero bool) (bool, ValueToReceiverFunc) {
return false, func(value reflect.Value, outFn TypedReceiver) {
v := value.Bool()
if !checkZero || v {
outFn.ReceiveBool(value.Kind(), v)
} else {
outFn.ReceiveZero(value.Kind())
}
}
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return func(_ bool, _ reflect.Type, checkZero bool) (bool, ValueToReceiverFunc) {
return false, func(value reflect.Value, outFn TypedReceiver) {
v := value.Int()
if !checkZero || v != 0 {
outFn.ReceiveInt(value.Kind(), v)
} else {
outFn.ReceiveZero(value.Kind())
}
}
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return func(_ bool, _ reflect.Type, checkZero bool) (bool, ValueToReceiverFunc) {
return false, func(value reflect.Value, outFn TypedReceiver) {
v := value.Uint()
if !checkZero || v != 0 {
outFn.ReceiveUint(value.Kind(), v)
} else {
outFn.ReceiveZero(value.Kind())
}
}
}
case reflect.Float32, reflect.Float64:
return func(_ bool, _ reflect.Type, checkZero bool) (bool, ValueToReceiverFunc) {
return false, func(value reflect.Value, outFn TypedReceiver) {
v := value.Float()
if !checkZero || v != 0 {
outFn.ReceiveFloat(value.Kind(), v)
} else {
outFn.ReceiveZero(value.Kind())
}
}
}
case reflect.Complex64, reflect.Complex128:
return func(_ bool, _ reflect.Type, checkZero bool) (bool, ValueToReceiverFunc) {
return false, func(value reflect.Value, outFn TypedReceiver) {
v := value.Complex()
if !checkZero || v != 0 {
outFn.ReceiveComplex(value.Kind(), v)
} else {
outFn.ReceiveZero(value.Kind())
}
}
}
case reflect.String:
return func(_ bool, _ reflect.Type, checkZero bool) (bool, ValueToReceiverFunc) {
return false, func(value reflect.Value, outFn TypedReceiver) {
v := value.String()
if !checkZero || v != "" {
outFn.ReceiveString(value.Kind(), v)
} else {
outFn.ReceiveZero(value.Kind())
}
}
}
// ============ Types that require special handling for unexported fields ===========
// nillable types fully defined at compile time
case reflect.Func, reflect.Ptr, reflect.Map, reflect.Slice, reflect.Chan: //, reflect.UnsafePointer:
return func(unexported bool, t reflect.Type, checkZero bool) (bool, ValueToReceiverFunc) {
if custom != nil {
if fn := custom(t, checkZero); fn != nil {
return unexported, func(value reflect.Value, outFn TypedReceiver) {
if value.IsNil() {
outFn.ReceiveNil(k)
} else {
fn(value.Interface(), value.Kind(), outFn)
}
}
}
}
return unexported, func(value reflect.Value, outFn TypedReceiver) {
if value.IsNil() { // avoids interface nil
outFn.ReceiveNil(k)
} else {
outFn.ReceiveElse(value.Kind(), value.Interface(), false)
}
}
}
// nillable types fully undefined at compile time
case reflect.Interface:
return func(unexported bool, t reflect.Type, checkZero bool) (b bool, getterFunc ValueToReceiverFunc) {
if custom != nil {
if fn := custom(t, checkZero); fn != nil {
return unexported, func(value reflect.Value, outFn TypedReceiver) {
v := value.Interface()
fn(v, value.Elem().Kind(), outFn)
}
}
}
return unexported, func(value reflect.Value, outFn TypedReceiver) {
if value.IsNil() {
outFn.ReceiveNil(value.Kind())
} else {
v := value.Interface()
outFn.ReceiveElse(value.Kind(), v, false)
}
}
}
// non-nillable
case reflect.Struct, reflect.Array:
return func(unexported bool, t reflect.Type, checkZero bool) (b bool, getterFunc ValueToReceiverFunc) {
valueFn := custom(t, checkZero)
if checkZero {
zeroValue := reflect.Zero(t).Interface()
return unexported, func(value reflect.Value, outFn TypedReceiver) {
if v := value.Interface(); valueFn == nil {
outFn.ReceiveElse(value.Kind(), v, v == zeroValue)
} else {
valueFn(v, value.Kind(), outFn)
}
}
}
return unexported, func(value reflect.Value, outFn TypedReceiver) {
if v := value.Interface(); valueFn == nil {
outFn.ReceiveElse(value.Kind(), v, false)
} else {
valueFn(v, value.Kind(), outFn)
}
}
}
// ============ Excluded ===================
// reflect.UnsafePointer
default:
return nil
}
}
func MakeAddressable(value reflect.Value) reflect.Value {
if value.CanAddr() {
return value
}
valueCopy := reflect.New(value.Type()).Elem()
valueCopy.Set(value)
return valueCopy
}
func FieldValueGetter(index int, fd reflect.StructField, useAddr bool, baseOffset uintptr) FieldGetterFunc {
if !useAddr {
return func(value reflect.Value) reflect.Value {
return value.Field(index)
}
}
fieldOffset := fd.Offset + baseOffset
fieldType := fd.Type
return func(value reflect.Value) reflect.Value {
return offsetFieldGetter(value, fieldOffset, fieldType)
}
}
func offsetFieldGetter(v reflect.Value, fieldOffset uintptr, fieldType reflect.Type) reflect.Value {
// base has to be here as a separate variable because of new checkptr check in go 1.14
// By default it is enabled when -race flag is present.
// It checks the following: If the result of pointer arithmetic points into
// a Go heap object, one of the unsafe.Pointer-typed operands must point
// into the same object.
// See go release notes for details: https://golang.org/doc/go1.14#compiler
base := unsafe.Pointer(v.UnsafeAddr())
return reflect.NewAt(fieldType, unsafe.Pointer(uintptr(base)+fieldOffset)).Elem()
}