-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathget_field.go
358 lines (310 loc) · 9.63 KB
/
get_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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package xreflect
import (
"errors"
"fmt"
"reflect"
"strings"
)
// Field returns the reflect.Value of the provided obj field.
// The obj can either be a structure or pointer to structure.
func Field(obj interface{}, fieldName string) (reflect.Value, error) {
var empty reflect.Value
if obj == nil {
return empty, errors.New("obj must not be nil")
}
val := Value(obj)
if !isSupportedKind(val.Kind(), []reflect.Kind{reflect.Struct}) {
return empty, errors.New("obj must be struct")
}
field := val.FieldByName(fieldName)
if !field.IsValid() {
return empty, fmt.Errorf("no such field: %s", fieldName)
}
return field, nil
}
// FieldValue returns the actual value of the provided obj field.
// The obj can either be a structure or pointer to structure.
func FieldValue(obj interface{}, fieldName string) (interface{}, error) {
field, err := Field(obj, fieldName)
if err != nil {
return nil, err
}
return field.Interface(), nil
}
// FieldKind returns the reflect.Kind of the provided obj field.
// The obj can either be a structure or pointer to structure.
func FieldKind(obj interface{}, fieldName string) (reflect.Kind, error) {
field, err := Field(obj, fieldName)
if err != nil {
return reflect.Invalid, err
}
return field.Kind(), nil
}
// FieldType returns the reflect.Type of the provided obj field.
// The obj can either be a structure or pointer to structure.
func FieldType(obj interface{}, fieldName string) (reflect.Type, error) {
field, err := Field(obj, fieldName)
if err != nil {
return nil, err
}
return field.Type(), nil
}
// FieldTypeStr returns the string of reflect.Type of the provided obj field.
// The obj can either be a structure or pointer to structure.
func FieldTypeStr(obj interface{}, fieldName string) (string, error) {
field, err := Field(obj, fieldName)
if err != nil {
return "", err
}
return field.Type().String(), nil
}
// EmbedField returns the reflect.Value of a field in the nested structure of obj based on the specified fieldPath.
// The obj can either be a structure or a pointer to a structure.
func EmbedField(obj interface{}, fieldPath string) (reflect.Value, error) {
var empty reflect.Value
if obj == nil {
return empty, errors.New("obj must not be nil")
}
if fieldPath == "" {
return empty, errors.New("field path must not be empty")
}
target := Value(obj)
if !isSupportedKind(target.Kind(), []reflect.Kind{reflect.Struct}) {
return empty, errors.New("obj must be struct")
}
fieldNames := strings.Split(fieldPath, ".")
for i, fieldName := range fieldNames {
if fieldName == "" {
return empty, fmt.Errorf("field path:%s is invalid", fieldPath)
}
target = target.FieldByName(fieldName)
if !target.IsValid() {
return empty, fmt.Errorf("no such field: %s", fieldName)
}
if i == len(fieldNames)-1 {
break
}
if target.Kind() == reflect.Pointer {
if target.IsNil() {
return empty, fmt.Errorf("field: %s is nil", fieldName)
}
target = reflect.ValueOf(target.Interface()).Elem()
}
if !isSupportedKind(target.Kind(), []reflect.Kind{reflect.Struct}) {
return empty, fmt.Errorf("field: %s is not struct", fieldName)
}
}
return target, nil
}
// EmbedFieldValue returns the actual value of a field in the nested structure of obj based on the specified fieldPath.
// The obj can either be a structure or a pointer to a structure.
func EmbedFieldValue(obj interface{}, fieldPath string) (interface{}, error) {
field, err := EmbedField(obj, fieldPath)
if err != nil {
return nil, err
}
return field.Interface(), nil
}
// EmbedFieldKind returns the reflect.Kind of a field in the nested structure of obj based on the specified fieldPath.
// The obj can either be a structure or a pointer to a structure.
func EmbedFieldKind(obj interface{}, fieldPath string) (reflect.Kind, error) {
field, err := EmbedField(obj, fieldPath)
if err != nil {
return reflect.Invalid, err
}
return field.Kind(), nil
}
// EmbedFieldType returns the reflect.Type of a field in the nested structure of obj based on the specified fieldPath.
// The obj can either be a structure or a pointer to a structure.
func EmbedFieldType(obj interface{}, fieldPath string) (reflect.Type, error) {
field, err := EmbedField(obj, fieldPath)
if err != nil {
return nil, err
}
return field.Type(), nil
}
// EmbedFieldTypeStr returns the string of the reflect.Type of a field in the nested structure of obj
// based on the specified fieldPath.
// The obj can either be a structure or a pointer to a structure.
func EmbedFieldTypeStr(obj interface{}, fieldPath string) (string, error) {
field, err := EmbedField(obj, fieldPath)
if err != nil {
return "", err
}
return field.Type().String(), nil
}
// Fields returns a map of reflect.Value containing all the fields of the obj, with the field names as keys.
// The obj can either be a structure or a pointer to a structure.
func Fields(obj interface{}) (map[string]reflect.Value, error) {
return fields(obj, false, "")
}
// FieldsDeep traverses the obj deeply, including all nested structures, and returns all fields as reflect.Value
// in the form of a map, where the key is the path of the field.
// The obj can either be a structure or pointer to structure.
func FieldsDeep(obj interface{}) (map[string]reflect.Value, error) {
return fields(obj, true, "")
}
func fields(obj interface{}, deep bool, prefix string) (map[string]reflect.Value, error) {
if obj == nil {
return nil, errors.New("obj must not be nil")
}
typ := Type(obj)
val := Value(obj)
if !isSupportedKind(typ.Kind(), []reflect.Kind{reflect.Struct}) {
return nil, errors.New("obj must be struct")
}
res := make(map[string]reflect.Value)
for i := 0; i < typ.NumField(); i++ {
ct := typ.Field(i)
cf := val.Field(i)
key := ct.Name
if prefix != "" {
key = prefix + "." + ct.Name
}
res[key] = cf
if deep {
// deal struct
if cf.Kind() == reflect.Struct {
m, err := fields(cf, deep, key)
if err != nil {
return nil, err
}
for k, v := range m {
res[k] = v
}
continue
}
// deal struct pointer
if cf.Kind() == reflect.Ptr && !cf.IsNil() {
cf = cf.Elem()
m, err := fields(cf, deep, key)
if err != nil {
return nil, err
}
for k, v := range m {
res[k] = v
}
continue
}
}
}
return res, nil
}
// SelectFields has the same functionality as Fields, but only the fields for which the function f returns true
// will be returned.
// The obj can either be a structure or pointer to structure.
func SelectFields(obj interface{}, f func(string, reflect.StructField, reflect.Value) bool) (map[string]reflect.Value,
error) {
return selectFields(obj, f, false, "")
}
// SelectFieldsDeep has the same functionality as FieldsDeep, but only the fields for which the function f returns true
// will be returned.
// The obj can either be a structure or pointer to structure.
func SelectFieldsDeep(obj interface{},
f func(string, reflect.StructField, reflect.Value) bool) (map[string]reflect.Value,
error) {
return selectFields(obj, f, true, "")
}
func selectFields(obj interface{}, f func(string, reflect.StructField, reflect.Value) bool,
deep bool, prefix string) (map[string]reflect.Value, error) {
if obj == nil {
return nil, errors.New("obj must not be nil")
}
typ := Type(obj)
val := Value(obj)
if !isSupportedKind(typ.Kind(), []reflect.Kind{reflect.Struct}) {
return nil, errors.New("obj must be struct")
}
res := make(map[string]reflect.Value)
for i := 0; i < typ.NumField(); i++ {
ct := typ.Field(i)
cf := val.Field(i)
key := ct.Name
if prefix != "" {
key = prefix + "." + ct.Name
}
if f(key, ct, cf) {
res[key] = cf
}
if deep {
// deal struct
if cf.Kind() == reflect.Struct {
m, err := selectFields(cf, f, deep, key)
if err != nil {
return nil, err
}
for k, v := range m {
res[k] = v
}
continue
}
// deal struct pointer
if cf.Kind() == reflect.Ptr && !cf.IsNil() {
cf = cf.Elem()
m, err := selectFields(cf, f, deep, key)
if err != nil {
return nil, err
}
for k, v := range m {
res[k] = v
}
continue
}
}
}
return res, nil
}
// RangeFields iterates over all fields of obj and calls function f on each field.
// If function f returns false, the iteration stops.
// The obj can either be a structure or pointer to structure.
func RangeFields(obj interface{}, f func(string, reflect.StructField, reflect.Value) bool) error {
return rangeFields(obj, f, false, "")
}
// RangeFieldsDeep performs a deep traversal of obj and its nested structures, and calls function f on each field.
// If the function f returns false, the iteration stops.
// The obj can either be a structure or pointer to structure.
func RangeFieldsDeep(obj interface{}, f func(string, reflect.StructField, reflect.Value) bool) error {
return rangeFields(obj, f, true, "")
}
func rangeFields(obj interface{}, f func(string, reflect.StructField, reflect.Value) bool,
deep bool, prefix string) error {
if obj == nil {
return errors.New("obj must not be nil")
}
typ := Type(obj)
val := Value(obj)
if !isSupportedKind(typ.Kind(), []reflect.Kind{reflect.Struct}) {
return errors.New("obj must be struct")
}
for i := 0; i < typ.NumField(); i++ {
ct := typ.Field(i)
cf := val.Field(i)
key := ct.Name
if prefix != "" {
key = prefix + "." + ct.Name
}
if !f(key, ct, cf) {
return nil
}
if deep {
// deal struct
if cf.Kind() == reflect.Struct {
err := rangeFields(cf, f, deep, key)
if err != nil {
return err
}
continue
}
// deal struct pointer
if cf.Kind() == reflect.Ptr && !cf.IsNil() {
cf = cf.Elem()
err := rangeFields(cf, f, deep, key)
if err != nil {
return err
}
continue
}
}
}
return nil
}