-
Notifications
You must be signed in to change notification settings - Fork 1
/
reflection.go
202 lines (155 loc) · 4.83 KB
/
reflection.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
package ffparser
import (
"errors"
"fmt"
"reflect"
)
func getField(obj interface{}, name string) (interface{}, error) {
if !hasValidType(obj, []reflect.Kind{reflect.Struct, reflect.Ptr}) {
return nil, errors.New("Cannot use GetField on a non-struct interface")
}
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
}
func getFieldKind(obj interface{}, name string) (reflect.Kind, error) {
if !hasValidType(obj, []reflect.Kind{reflect.Struct, reflect.Ptr}) {
return reflect.Invalid, errors.New("Cannot use GetField on a non-struct interface")
}
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
}
func getFieldTag(obj interface{}, fieldName, tagKey string) (string, error) {
if !hasValidType(obj, []reflect.Kind{reflect.Struct, reflect.Ptr}) {
return "", errors.New("Cannot use GetField on a non-struct interface")
}
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 "", errors.New("Cannot GetFieldTag on a non-exported struct field")
}
return field.Tag.Get(tagKey), nil
}
func setField(obj interface{}, name string, value interface{}) error {
// Fetch the field reflect.Value
structValue := reflect.ValueOf(obj).Elem()
if structValue.Kind() == reflect.Ptr {
structValue = structValue.Elem()
}
structFieldValue := structValue.FieldByName(name)
if !structFieldValue.IsValid() {
return fmt.Errorf("No such field: %s in obj", name)
}
// If obj field value is not settable an error is thrown
if !structFieldValue.CanSet() {
return fmt.Errorf("Cannot set %s field value", name)
}
structFieldType := structFieldValue.Type()
val := reflect.ValueOf(value)
if structFieldType != val.Type() {
invalidTypeError := errors.New("Provided value type didn't match obj field type")
return invalidTypeError
}
structFieldValue.Set(val)
return nil
}
func hasField(obj interface{}, name string) (bool, error) {
if !hasValidType(obj, []reflect.Kind{reflect.Struct, reflect.Ptr}) {
return false, errors.New("Cannot use GetField on a non-struct interface")
}
objValue := reflectValue(obj)
objType := objValue.Type()
field, ok := objType.FieldByName(name)
if !ok || !isExportableField(field) {
return false, nil
}
return true, nil
}
func fields(obj interface{}) ([]string, error) {
if !hasValidType(obj, []reflect.Kind{reflect.Struct, reflect.Ptr}) {
return nil, errors.New("Cannot use GetField on a non-struct interface")
}
objValue := reflectValue(obj)
objType := objValue.Type()
fieldsCount := objType.NumField()
var fields []string
for i := 0; i < fieldsCount; i++ {
field := objType.Field(i)
if isExportableField(field) {
fields = append(fields, field.Name)
}
}
return fields, nil
}
func items(obj interface{}) (map[string]interface{}, error) {
if !hasValidType(obj, []reflect.Kind{reflect.Struct, reflect.Ptr}) {
return nil, errors.New("Cannot use GetField on a non-struct interface")
}
objValue := reflectValue(obj)
objType := objValue.Type()
fieldsCount := objType.NumField()
items := make(map[string]interface{})
for i := 0; i < fieldsCount; i++ {
field := objType.Field(i)
fieldValue := objValue.Field(i)
// Make sure only exportable and addressable fields are
// returned by Items
if isExportableField(field) {
items[field.Name] = fieldValue.Interface()
}
}
return items, nil
}
func tags(obj interface{}, key string) (map[string]string, error) {
if !hasValidType(obj, []reflect.Kind{reflect.Struct, reflect.Ptr}) {
return nil, errors.New("Cannot use GetField on a non-struct interface")
}
objValue := reflectValue(obj)
objType := objValue.Type()
fieldsCount := objType.NumField()
tags := make(map[string]string)
for i := 0; i < fieldsCount; i++ {
structField := objType.Field(i)
if isExportableField(structField) {
tags[structField.Name] = structField.Tag.Get(key)
}
}
return tags, 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 {
return field.PkgPath == ""
}
func hasValidType(obj interface{}, types []reflect.Kind) bool {
for _, t := range types {
if reflect.TypeOf(obj).Kind() == t {
return true
}
}
return false
}
func isStruct(obj interface{}) bool {
return reflect.TypeOf(obj).Kind() == reflect.Struct
}
func isPointer(obj interface{}) bool {
return reflect.TypeOf(obj).Kind() == reflect.Ptr
}