-
Notifications
You must be signed in to change notification settings - Fork 4
/
get_struct_field.go
265 lines (226 loc) · 7.85 KB
/
get_struct_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
package xreflect
import (
"errors"
"fmt"
"reflect"
"strings"
)
// StructField returns the reflect.StructField of the provided obj field.
// The obj can either be a structure or pointer to structure.
func StructField(obj interface{}, fieldName string) (reflect.StructField, error) {
var empty reflect.StructField
if obj == nil {
return empty, errors.New("obj must not be nil")
}
typ := Type(obj)
if !isSupportedKind(typ.Kind(), []reflect.Kind{reflect.Struct}) {
return empty, errors.New("obj must be struct")
}
field, ok := typ.FieldByName(fieldName)
if !ok {
return empty, fmt.Errorf("no such field: %s in obj", fieldName)
}
return field, nil
}
// StructFieldKind returns the reflect.Kind of the provided obj field.
// The obj can either be a structure or pointer to structure.
func StructFieldKind(obj interface{}, fieldName string) (reflect.Kind, error) {
field, err := StructField(obj, fieldName)
if err != nil {
return reflect.Invalid, err
}
return field.Type.Kind(), nil
}
// StructFieldType returns the reflect.Type of the provided obj field.
// The obj can either be a structure or pointer to structure.
func StructFieldType(obj interface{}, fieldName string) (reflect.Type, error) {
field, err := StructField(obj, fieldName)
if err != nil {
return nil, err
}
return field.Type, nil
}
// StructFieldTypeStr returns the string of reflect.Type of the provided obj field.
// The obj can either be a structure or pointer to structure.
func StructFieldTypeStr(obj interface{}, fieldName string) (string, error) {
field, err := StructField(obj, fieldName)
if err != nil {
return "", err
}
return field.Type.String(), nil
}
// HasStructField checks if the provided obj struct has field named fieldName.
// The obj can either be a structure or pointer to structure.
func HasStructField(obj interface{}, fieldName string) (bool, error) {
_, err := StructField(obj, fieldName)
if err != nil {
return false, err
}
return true, nil
}
// StructFieldTag returns the reflect.StructTag of the provided obj field.
// The obj parameter can either be a structure or pointer to structure.
func StructFieldTag(obj interface{}, fieldName string) (reflect.StructTag, error) {
structField, err := StructField(obj, fieldName)
if err != nil {
return "", err
}
return structField.Tag, nil
}
// StructFieldTagValue returns the provided obj field tag value.
// The obj parameter can either be a structure or pointer to structure.
func StructFieldTagValue(obj interface{}, fieldName, tagKey string) (string, error) {
tag, err := StructFieldTag(obj, fieldName)
if err != nil {
return "", err
}
return tag.Get(tagKey), nil
}
// EmbedStructField 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 EmbedStructField(obj interface{}, fieldPath string) (reflect.StructField, error) {
var empty reflect.StructField
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 := Type(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)
}
structField, ok := target.FieldByName(fieldName)
if !ok {
return empty, fmt.Errorf("no such field: %s", fieldName)
}
target = structField.Type
if i == len(fieldNames)-1 {
return structField, nil
}
if target.Kind() == reflect.Pointer {
target = target.Elem()
}
if !isSupportedKind(target.Kind(), []reflect.Kind{reflect.Struct}) {
return empty, fmt.Errorf("field: %s is not struct", fieldName)
}
}
return empty, nil
}
// EmbedStructFieldKind 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 EmbedStructFieldKind(obj interface{}, fieldPath string) (reflect.Kind, error) {
field, err := EmbedStructField(obj, fieldPath)
if err != nil {
return reflect.Invalid, err
}
return field.Type.Kind(), nil
}
// EmbedStructFieldType 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 EmbedStructFieldType(obj interface{}, fieldPath string) (reflect.Type, error) {
field, err := EmbedStructField(obj, fieldPath)
if err != nil {
return nil, err
}
return field.Type, nil
}
// EmbedStructFieldTypeStr 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 EmbedStructFieldTypeStr(obj interface{}, fieldPath string) (string, error) {
field, err := EmbedStructField(obj, fieldPath)
if err != nil {
return "", err
}
return field.Type.String(), nil
}
// StructFields returns a slice of reflect.StructField containing all the fields of the obj.
// The obj can either be a structure or a pointer to a structure.
func StructFields(obj interface{}) ([]reflect.StructField, error) {
return structFields(obj, false)
}
// StructFieldsFlatten returns "flattened" struct fields.
// Note that StructFieldsFlatten treats fields from anonymous inner structs as normal fields.
func StructFieldsFlatten(obj interface{}) ([]reflect.StructField, error) {
return structFields(obj, true)
}
func structFields(obj interface{}, flatten bool) ([]reflect.StructField, error) {
if obj == nil {
return nil, errors.New("obj must not be nil")
}
typ := Type(obj)
if !isSupportedKind(typ.Kind(), []reflect.Kind{reflect.Struct}) {
return nil, errors.New("obj must be struct")
}
var res []reflect.StructField
for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i)
if !flatten {
res = append(res, field)
continue
}
if field.Anonymous && field.Type.Kind() == reflect.Struct {
subFields, err := structFields(field.Type, flatten)
if err != nil {
return nil, fmt.Errorf("cannot get fields in %s: %w", field.Name, err)
}
res = append(res, subFields...)
} else {
res = append(res, field)
}
}
return res, nil
}
// SelectStructFields has the same functionality as StructFields, 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 SelectStructFields(obj interface{}, f func(int, reflect.StructField) bool) ([]reflect.StructField, error) {
if obj == nil {
return nil, errors.New("obj must not be nil")
}
typ := Type(obj)
if !isSupportedKind(typ.Kind(), []reflect.Kind{reflect.Struct}) {
return nil, errors.New("obj must be struct")
}
var res []reflect.StructField
for i := 0; i < typ.NumField(); i++ {
if f(i, typ.Field(i)) {
res = append(res, typ.Field(i))
}
}
return res, nil
}
// RangeStructFields iterates over all struct 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 RangeStructFields(obj interface{}, f func(int, reflect.StructField) bool) error {
if obj == nil {
return errors.New("obj must not be nil")
}
typ := Type(obj)
if !isSupportedKind(typ.Kind(), []reflect.Kind{reflect.Struct}) {
return errors.New("obj must be struct")
}
for i := 0; i < typ.NumField(); i++ {
if !f(i, typ.Field(i)) {
break
}
}
return nil
}
// AnonymousStructFields returns the slice of reflect.StructField of all anonymous fields in obj.
// The obj can either be a structure or pointer to structure.
func AnonymousStructFields(obj interface{}) ([]reflect.StructField, error) {
return SelectStructFields(obj, func(i int, field reflect.StructField) bool {
return field.Anonymous
})
}