-
Notifications
You must be signed in to change notification settings - Fork 52
/
reflections.go
332 lines (268 loc) · 10 KB
/
reflections.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
// Copyright © 2013 Théo Crevon
//
// See the file LICENSE for copying permission.
// Package reflections provides high level abstractions over the Go standard [reflect] library.
//
// In practice, the `reflect` library's API proves somewhat low-level and un-intuitive.
// Using it can turn out pretty complex, daunting, and scary, when doing simple
// things like accessing a structure field value, a field tag, etc.
//
// The `reflections` package aims to make developers' life easier when it comes to introspect
// struct values at runtime. Its API takes inspiration in the python language's `getattr,` `setattr,` and `hasattr` set
// of methods and provides simplified access to structure fields and tags.
//
// [reflect]: http://golang.org/pkg/reflect/
package reflections
import (
"errors"
"fmt"
"reflect"
)
// ErrUnsupportedType indicates that the provided type doesn't support the requested reflection operation.
var ErrUnsupportedType = errors.New("unsupported type")
// ErrUnexportedField indicates that an operation failed as a result of
// applying to a non-exported struct field.
var ErrUnexportedField = errors.New("unexported field")
// GetField returns the value of the provided obj field.
// The `obj` can either be a structure or pointer to structure.
func GetField(obj interface{}, name string) (interface{}, error) {
if !isSupportedType(obj, []reflect.Kind{reflect.Struct, reflect.Ptr}) {
return nil, fmt.Errorf("cannot use GetField on a non-struct object: %w", ErrUnsupportedType)
}
objValue := reflectValue(obj)
field := objValue.FieldByName(name)
if !field.IsValid() {
return nil, fmt.Errorf("no such field: %s in obj", name)
}
return field.Interface(), nil
}
// GetFieldKind returns the kind of the provided obj field.
// The `obj` can either be a structure or pointer to structure.
func GetFieldKind(obj interface{}, name string) (reflect.Kind, error) {
if !isSupportedType(obj, []reflect.Kind{reflect.Struct, reflect.Ptr}) {
return reflect.Invalid, fmt.Errorf("cannot use GetFieldKind on a non-struct interface: %w", ErrUnsupportedType)
}
objValue := reflectValue(obj)
field := objValue.FieldByName(name)
if !field.IsValid() {
return reflect.Invalid, fmt.Errorf("no such field: %s in obj", name)
}
return field.Type().Kind(), nil
}
// GetFieldType returns the kind of the provided obj field.
// The `obj` can either be a structure or pointer to structure.
func GetFieldType(obj interface{}, name string) (string, error) {
if !isSupportedType(obj, []reflect.Kind{reflect.Struct, reflect.Ptr}) {
return "", fmt.Errorf("cannot use GetFieldType on a non-struct interface: %w", ErrUnsupportedType)
}
objValue := reflectValue(obj)
field := objValue.FieldByName(name)
if !field.IsValid() {
return "", fmt.Errorf("no such field: %s in obj", name)
}
return field.Type().String(), nil
}
// GetFieldTag returns the provided obj field tag value.
// The `obj` parameter can either be a structure or pointer to structure.
func GetFieldTag(obj interface{}, fieldName, tagKey string) (string, error) {
if !isSupportedType(obj, []reflect.Kind{reflect.Struct, reflect.Ptr}) {
return "", fmt.Errorf("cannot use GetFieldTag on a non-struct interface: %w", ErrUnsupportedType)
}
objValue := reflectValue(obj)
objType := objValue.Type()
field, ok := objType.FieldByName(fieldName)
if !ok {
return "", fmt.Errorf("no such field: %s in obj", fieldName)
}
if !isExportableField(field) {
return "", fmt.Errorf("cannot GetFieldTag on a non-exported struct field: %w", ErrUnexportedField)
}
return field.Tag.Get(tagKey), nil
}
// GetFieldNameByTagValue looks up a field with a matching `{tagKey}:"{tagValue}"` tag in the provided `obj` item.
// The `obj` parameter must be a `struct`, or a `pointer` to one. If the `obj` parameter doesn't have a field tagged
// with the `tagKey`, and the matching `tagValue`, this function returns an error.
func GetFieldNameByTagValue(obj interface{}, tagKey, tagValue string) (string, error) {
if !isSupportedType(obj, []reflect.Kind{reflect.Struct, reflect.Ptr}) {
return "", fmt.Errorf("cannot use GetFieldByTag on a non-struct interface: %w", ErrUnsupportedType)
}
objValue := reflectValue(obj)
objType := objValue.Type()
fieldsCount := objType.NumField()
for i := range fieldsCount {
structField := objType.Field(i)
if structField.Tag.Get(tagKey) == tagValue {
return structField.Name, nil
}
}
return "", errors.New("tag doesn't exist in the given struct")
}
// SetField sets the provided obj field with provided value.
//
// The `obj` parameter must be a pointer to a struct, otherwise it soundly fails.
// The provided `value` type should match with the struct field being set.
func SetField(obj interface{}, name string, value interface{}) error {
// Fetch the field reflect.Value
structValue := reflect.ValueOf(obj).Elem()
structFieldValue := structValue.FieldByName(name)
if !structFieldValue.IsValid() {
return fmt.Errorf("no such field: %s in obj", name)
}
if !structFieldValue.CanSet() {
return fmt.Errorf("cannot set %s field value", name)
}
structFieldType := structFieldValue.Type()
val := reflect.ValueOf(value)
if !val.Type().AssignableTo(structFieldType) {
invalidTypeError := errors.New("provided value type not assignable to obj field type")
return invalidTypeError
}
structFieldValue.Set(val)
return nil
}
// HasField checks if the provided `obj` struct has field named `name`.
// The `obj` can either be a structure or pointer to structure.
func HasField(obj interface{}, name string) (bool, error) {
if !isSupportedType(obj, []reflect.Kind{reflect.Struct, reflect.Ptr}) {
return false, fmt.Errorf("cannot use HasField on a non-struct interface: %w", ErrUnsupportedType)
}
objValue := reflectValue(obj)
objType := objValue.Type()
field, ok := objType.FieldByName(name)
if !ok || !isExportableField(field) {
return false, nil
}
return true, nil
}
// Fields returns the struct fields names list.
// The `obj` parameter can either be a structure or pointer to structure.
func Fields(obj interface{}) ([]string, error) {
return fields(obj, false)
}
// FieldsDeep returns "flattened" fields.
//
// Note that FieldsDeep treats fields from anonymous inner structs as normal fields.
func FieldsDeep(obj interface{}) ([]string, error) {
return fields(obj, true)
}
func fields(obj interface{}, deep bool) ([]string, error) {
if !isSupportedType(obj, []reflect.Kind{reflect.Struct, reflect.Ptr}) {
return nil, fmt.Errorf("cannot use fields on a non-struct interface: %w", ErrUnsupportedType)
}
objValue := reflectValue(obj)
objType := objValue.Type()
fieldsCount := objType.NumField()
var allFields []string
for i := range fieldsCount {
field := objType.Field(i)
if isExportableField(field) {
if !deep || !field.Anonymous {
allFields = append(allFields, field.Name)
continue
}
fieldValue := objValue.Field(i)
subFields, err := fields(fieldValue.Interface(), deep)
if err != nil {
return nil, fmt.Errorf("cannot get fields in %s: %w", field.Name, err)
}
allFields = append(allFields, subFields...)
}
}
return allFields, nil
}
// Items returns the field:value struct pairs as a map.
// The `obj` parameter can either be a structure or pointer to structure.
func Items(obj interface{}) (map[string]interface{}, error) {
return items(obj, false)
}
// ItemsDeep returns "flattened" items.
// Note that ItemsDeep will treat fields from anonymous inner structs as normal fields.
func ItemsDeep(obj interface{}) (map[string]interface{}, error) {
return items(obj, true)
}
func items(obj interface{}, deep bool) (map[string]interface{}, error) {
if !isSupportedType(obj, []reflect.Kind{reflect.Struct, reflect.Ptr}) {
return nil, fmt.Errorf("cannot use items on a non-struct interface: %w", ErrUnsupportedType)
}
objValue := reflectValue(obj)
objType := objValue.Type()
fieldsCount := objType.NumField()
allItems := make(map[string]interface{})
for i := range fieldsCount {
field := objType.Field(i)
fieldValue := objValue.Field(i)
if isExportableField(field) {
if !deep || !field.Anonymous {
allItems[field.Name] = fieldValue.Interface()
continue
}
m, err := items(fieldValue.Interface(), deep)
if err != nil {
return nil, fmt.Errorf("cannot get items in %s: %w", field.Name, err)
}
for k, v := range m {
allItems[k] = v
}
}
}
return allItems, nil
}
// Tags lists the struct tag fields.
// The `obj` can whether be a structure or pointer to structure.
func Tags(obj interface{}, key string) (map[string]string, error) {
return tags(obj, key, false)
}
// TagsDeep returns "flattened" tags.
// Note that TagsDeep treats fields from anonymous
// inner structs as normal fields.
func TagsDeep(obj interface{}, key string) (map[string]string, error) {
return tags(obj, key, true)
}
func tags(obj interface{}, key string, deep bool) (map[string]string, error) {
if !isSupportedType(obj, []reflect.Kind{reflect.Struct, reflect.Ptr}) {
return nil, fmt.Errorf("cannot use tags on a non-struct interface: %w", ErrUnsupportedType)
}
objValue := reflectValue(obj)
objType := objValue.Type()
fieldsCount := objType.NumField()
allTags := make(map[string]string)
for i := range fieldsCount {
structField := objType.Field(i)
if isExportableField(structField) {
if !deep || !structField.Anonymous {
allTags[structField.Name] = structField.Tag.Get(key)
continue
}
fieldValue := objValue.Field(i)
m, err := tags(fieldValue.Interface(), key, deep)
if err != nil {
return nil, fmt.Errorf("cannot get items in %s: %w", structField.Name, err)
}
for k, v := range m {
allTags[k] = v
}
}
}
return allTags, nil
}
func reflectValue(obj interface{}) reflect.Value {
var val reflect.Value
if reflect.TypeOf(obj).Kind() == reflect.Ptr {
val = reflect.ValueOf(obj).Elem()
} else {
val = reflect.ValueOf(obj)
}
return val
}
func isExportableField(field reflect.StructField) bool {
// PkgPath is empty for exported fields.
return field.PkgPath == ""
}
func isSupportedType(obj interface{}, types []reflect.Kind) bool {
for _, t := range types {
if reflect.TypeOf(obj).Kind() == t {
return true
}
}
return false
}