-
Notifications
You must be signed in to change notification settings - Fork 2
/
field.go
299 lines (277 loc) · 7.87 KB
/
field.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package fmap
import (
"reflect"
"strings"
"unsafe"
)
type field struct {
reflect.StructField
structPath string
parent *field
dereferenceType reflect.Type
}
func (f *field) GetName() string {
return f.Name
}
func (f *field) GetPkgPath() string {
return f.PkgPath
}
func (f *field) GetType() reflect.Type {
return f.Type
}
func (f *field) GetTag() reflect.StructTag {
return f.Tag
}
func (f *field) GetOffset() uintptr {
return f.Offset
}
func (f *field) GetIndex() []int {
return f.Index
}
func (f *field) GetAnonymous() bool {
return f.Anonymous
}
func (f *field) IsExported() bool {
return f.PkgPath == ""
}
func (f *field) GetStructPath() string {
return f.structPath
}
func (f *field) GetParent() Field {
return f.parent
}
func (f *field) GetPtr(obj interface{}) interface{} {
return reflect.NewAt(f.Type, f.getPtr(obj)).Interface()
}
// Get returns the value of the storage in the provided object.
// It takes a parameter `obj` of type `interface{}`, representing the object.
// It returns the value of the storage as an `interface{}`.
func (f *field) Get(obj interface{}) interface{} {
ptrToField := f.getPtr(obj)
kind := f.Type.Kind()
isPtr := false
if kind == reflect.Ptr {
isPtr = true
kind = f.Type.Elem().Kind()
}
if isPtr {
switch kind {
case reflect.String:
return getPtrValue[*string](ptrToField)
case reflect.Int:
return getPtrValue[*int](ptrToField)
case reflect.Int8:
return getPtrValue[*int8](ptrToField)
case reflect.Int16:
return getPtrValue[*int16](ptrToField)
case reflect.Int32:
return getPtrValue[*int32](ptrToField)
case reflect.Int64:
return getPtrValue[*int64](ptrToField)
case reflect.Uint:
return getPtrValue[*uint](ptrToField)
case reflect.Uint8:
return getPtrValue[*uint8](ptrToField)
case reflect.Uint16:
return getPtrValue[*uint16](ptrToField)
case reflect.Uint32:
return getPtrValue[*uint32](ptrToField)
case reflect.Uint64:
return getPtrValue[*uint64](ptrToField)
case reflect.Float32:
return getPtrValue[*float32](ptrToField)
case reflect.Float64:
return getPtrValue[*float64](ptrToField)
case reflect.Bool:
return getPtrValue[*bool](ptrToField)
default:
return reflect.NewAt(f.Type, ptrToField).Elem().Interface()
}
} else {
switch kind {
case reflect.String:
return getPtrValue[string](ptrToField)
case reflect.Int:
return getPtrValue[int](ptrToField)
case reflect.Int8:
return getPtrValue[int8](ptrToField)
case reflect.Int16:
return getPtrValue[int16](ptrToField)
case reflect.Int32:
return getPtrValue[int32](ptrToField)
case reflect.Int64:
return getPtrValue[int64](ptrToField)
case reflect.Uint:
return getPtrValue[uint](ptrToField)
case reflect.Uint8:
return getPtrValue[uint8](ptrToField)
case reflect.Uint16:
return getPtrValue[uint16](ptrToField)
case reflect.Uint32:
return getPtrValue[uint32](ptrToField)
case reflect.Uint64:
return getPtrValue[uint64](ptrToField)
case reflect.Float32:
return getPtrValue[float32](ptrToField)
case reflect.Float64:
return getPtrValue[float64](ptrToField)
case reflect.Bool:
return getPtrValue[bool](ptrToField)
default:
return reflect.NewAt(f.Type, ptrToField).Elem().Interface()
}
}
}
func (f *field) GetTagPath(tag string, ignoreParentTagMissing bool) string {
tagPath := ""
if val, ok := f.Tag.Lookup(tag); ok {
vals := strings.Split(val, ",")
if len(vals) > 0 && len(vals[0]) > 0 {
tagPath = vals[0]
}
}
if tagPath == "" {
return tagPath
}
if f.parent == nil {
return tagPath
}
parentTag := f.parent.GetTagPath(tag, ignoreParentTagMissing)
if parentTag == "" && !ignoreParentTagMissing {
return ""
}
if parentTag == "" {
return tagPath
}
return parentTag + "." + tagPath
}
// getPtr returns a pointer to the field's value in the provided configuration object.
// It takes a parameter `conf` of type `any`, representing the configuration object.
// It returns an `unsafe.Pointer` to the `field's` value in the configuration object.
func (f *field) getPtr(obj interface{}) unsafe.Pointer {
confPointer := ((*[2]unsafe.Pointer)(unsafe.Pointer(&obj)))[1]
ptToField := unsafe.Add(confPointer, f.Offset)
return ptToField
}
func setPtrValue[T any](ptr unsafe.Pointer, val any) {
valSet := (*T)(ptr)
*valSet = val.(T)
}
func getPtrValue[T any](ptr unsafe.Pointer) T {
return *(*T)(ptr)
}
// Set updates the value of the storage in the provided object with the provided value.
// It takes two parameters:
// - obj: interface{}, representing the object containing the field.
// - val: interface{}, representing the new value for the field.
//
// The Set method uses the getPtr method to get a pointer to the storage in the object.
// It then performs a type switch on the kind of the storage to determine its type, and sets the value accordingly.
// The supported storage types are string, int, and bool.
// If the storage type is not one of the supported types, it panics with the message "unhandled default case".
func (f *field) Set(obj interface{}, val interface{}) {
ptrToField := f.getPtr(obj)
kind := f.Type.Kind()
isPtr := false
if kind == reflect.Ptr {
isPtr = true
kind = f.Type.Elem().Kind()
}
if isPtr {
switch kind {
case reflect.String:
setPtrValue[*string](ptrToField, val)
case reflect.Int:
setPtrValue[*int](ptrToField, val)
case reflect.Int8:
setPtrValue[*int8](ptrToField, val)
case reflect.Int16:
setPtrValue[*int16](ptrToField, val)
case reflect.Int32:
setPtrValue[*int32](ptrToField, val)
case reflect.Int64:
setPtrValue[*int64](ptrToField, val)
case reflect.Uint:
setPtrValue[*uint](ptrToField, val)
case reflect.Uint8:
setPtrValue[*uint8](ptrToField, val)
case reflect.Uint16:
setPtrValue[*uint16](ptrToField, val)
case reflect.Uint32:
setPtrValue[*uint32](ptrToField, val)
case reflect.Uint64:
setPtrValue[*uint64](ptrToField, val)
case reflect.Float32:
setPtrValue[*float32](ptrToField, val)
case reflect.Float64:
setPtrValue[*float64](ptrToField, val)
case reflect.Bool:
setPtrValue[*bool](ptrToField, val)
default:
dest := reflect.NewAt(f.Type, ptrToField)
dest = dest.Elem()
source := reflect.ValueOf(val)
dest.Set(source)
}
} else {
switch kind {
case reflect.String:
setPtrValue[string](ptrToField, val)
case reflect.Int:
setPtrValue[int](ptrToField, val)
case reflect.Int8:
setPtrValue[int8](ptrToField, val)
case reflect.Int16:
setPtrValue[int16](ptrToField, val)
case reflect.Int32:
setPtrValue[int32](ptrToField, val)
case reflect.Int64:
setPtrValue[int64](ptrToField, val)
case reflect.Uint:
setPtrValue[uint](ptrToField, val)
case reflect.Uint8:
setPtrValue[uint8](ptrToField, val)
case reflect.Uint16:
setPtrValue[uint16](ptrToField, val)
case reflect.Uint32:
setPtrValue[uint32](ptrToField, val)
case reflect.Uint64:
setPtrValue[uint64](ptrToField, val)
case reflect.Float32:
setPtrValue[float32](ptrToField, val)
case reflect.Float64:
setPtrValue[float64](ptrToField, val)
case reflect.Bool:
setPtrValue[bool](ptrToField, val)
default:
dest := reflect.NewAt(f.Type, ptrToField)
dest = dest.Elem()
source := reflect.ValueOf(val)
dest.Set(source)
}
}
}
func (f *field) GetDereferencedType() reflect.Type {
if f.dereferenceType != nil {
return f.dereferenceType
}
indirectType := f.Type
for indirectType.Kind() == reflect.Ptr {
indirectType = indirectType.Elem()
}
f.dereferenceType = indirectType
return f.dereferenceType
}
// GetDereferenced - uses reflect package for casting field value from obj to direct field value, i.e. dereferenced value.
func (f *field) GetDereferenced(obj any) (any, bool) {
derefType := f.GetDereferencedType()
value := f.Get(obj)
valOf := reflect.ValueOf(value)
for valOf.IsValid() && valOf.Kind() == reflect.Ptr {
valOf = valOf.Elem()
}
if valOf.IsValid() && derefType == valOf.Type() {
return valOf.Interface(), true
}
return nil, false
}