-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreflect.go
318 lines (260 loc) · 6.78 KB
/
reflect.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
package utils
import (
"encoding/json"
"fmt"
"reflect"
"strconv"
"strings"
)
/*
* Assign a map[string]interface{} to a struct mapping the map pairs to
* the structure members by name using reflexion.
*/
func Assign(config interface{}, values map[string]interface{}) {
s := reflect.ValueOf(config).Elem()
t := reflect.TypeOf(config)
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
for key, val := range values {
if typ, ok := t.FieldByName(key); ok {
s.FieldByName(key).Set(reflect.ValueOf(val).Convert(typ.Type))
}
}
}
/*
* Transform []T to []interface{}
*/
func ToInterfaceArray(v interface{}) (res []interface{}) {
switch reflect.TypeOf(v).Kind() {
case reflect.Slice:
s := reflect.ValueOf(v)
res = make([]interface{}, s.Len())
for i := 0; i < s.Len(); i++ {
res[i] = s.Index(i).Interface()
}
default:
panic(fmt.Sprintf("unexpected type %T", v))
}
return res
}
//
// The following functions aim to override struct fields from string values
//
func IsPointer(kind reflect.Kind) bool {
return kind == reflect.Ptr
}
func IsString(kind reflect.Kind) bool {
return kind == reflect.String
}
func AssignStringString(elem reflect.Value, value string) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("unable to assign string value : %s", r)
}
}()
elem.SetString(value)
return nil
}
func IsBool(kind reflect.Kind) bool {
return kind == reflect.Bool
}
func AssignBoolString(elem reflect.Value, value string) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("unable to assign bool value : %s", r)
}
}()
if strings.ToLower(value) == "true" {
elem.SetBool(true)
} else if strings.ToLower(value) == "false" {
elem.SetBool(false)
} else {
return fmt.Errorf("invalid boolean value")
}
return nil
}
var intKinds = []reflect.Kind{
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
}
func IsInt(kind reflect.Kind) bool {
for _, k := range intKinds {
if kind == k {
return true
}
}
return false
}
func AssignIntString(elem reflect.Value, value string) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("unable to assign int value : %s", r)
}
}()
intValue, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return fmt.Errorf("invalid int value")
}
elem.SetInt(intValue)
return
}
var uintKinds = []reflect.Kind{
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
}
func IsUint(kind reflect.Kind) bool {
for _, k := range uintKinds {
if kind == k {
return true
}
}
return false
}
func AssignUintString(elem reflect.Value, value string) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("unable to assign uint value : %s", r)
}
}()
uintValue, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return fmt.Errorf("invalid uint value")
}
elem.SetUint(uintValue)
return
}
var floatKinds = []reflect.Kind{
reflect.Float32, reflect.Float64,
}
func IsFloat(kind reflect.Kind) bool {
for _, k := range floatKinds {
if kind == k {
return true
}
}
return false
}
func AssignFloatString(elem reflect.Value, value string) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("unable to assign float value : %s", r)
}
}()
floatValue, err := strconv.ParseFloat(value, 64)
if err != nil {
return fmt.Errorf("invalid float value")
}
elem.SetFloat(floatValue)
return
}
func IsSlice(kind reflect.Kind) bool {
return kind == reflect.Slice
}
func AssignJsonSliceString(elem reflect.Value, strValue string) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("unable to assign json string to slice : %s", r)
}
}()
var sliceValue []interface{}
err = json.Unmarshal([]byte(strValue), &sliceValue)
if err != nil {
return fmt.Errorf("invalid slice value : %s", err)
}
return AssignSliceValues(elem, sliceValue)
}
func AssignSliceValues(elem reflect.Value, values []interface{}) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("unable to assign slice values : %s", r)
}
}()
sliceType := elem.Type().Elem()
slice := reflect.Zero(reflect.SliceOf(sliceType)).Interface()
elem.Set(reflect.ValueOf(slice))
for _, value := range values {
elem.Set(reflect.Append(elem, reflect.ValueOf(value).Convert(sliceType)))
}
return nil
}
func IsMap(kind reflect.Kind) bool {
return kind == reflect.Map
}
func AssignJsonMapString(elem reflect.Value, strValue string) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("unable to assign json string to map : %s", r)
}
}()
mapValues := make(map[string]interface{})
err = json.Unmarshal([]byte(strValue), &mapValues)
if err != nil {
return fmt.Errorf("invalid map value : %s", err)
}
return AssignMapValues(elem, mapValues)
}
func AssignMapValues(elem reflect.Value, values map[string]interface{}) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("unable to assign map values : %s", r)
}
}()
if elem.IsNil() {
elem.Set(reflect.MakeMap(elem.Type()))
}
for key, val := range values {
elem.SetMapIndex(reflect.ValueOf(key), reflect.ValueOf(val))
}
return nil
}
func AssignString(elem reflect.Value, value string) (err error) {
kind := elem.Kind()
if IsString(kind) {
return AssignStringString(elem, value)
}
if IsBool(kind) {
return AssignBoolString(elem, value)
}
if IsInt(kind) {
return AssignIntString(elem, value)
}
if IsUint(kind) {
return AssignUintString(elem, value)
}
if IsFloat(kind) {
return AssignFloatString(elem, value)
}
if IsSlice(kind) {
return AssignJsonSliceString(elem, value)
}
if IsMap(kind) {
return AssignJsonMapString(elem, value)
}
return fmt.Errorf("unsupported assignment for type %s", kind.String())
}
// This function will call getStringValue with the name of each Exported/Public field of the given object
// If getStringValue return ok == true : then it will try to assign the returned string value will to the corresponding field
// If getStringValue return ok == false : the field is skipped
//
// For slice and maps types the string is deserialized from json
// This can be useful to override config from environment variables
// /!\ No support for pointers or custom structs /!\
func AssignStrings(value interface{}, getStringValue func(bool string) (strValue string, ok bool)) (err error) {
val := reflect.ValueOf(value).Elem()
for i := 0; i < val.NumField(); i++ {
field := val.Type().Field(i)
elem := val.Field(i)
// Skip unexported/private fields
if len(field.PkgPath) != 0 {
continue
}
stringValue, ok := getStringValue(field.Name)
if !ok {
continue
}
err = AssignString(elem, stringValue)
if err != nil {
return fmt.Errorf("unable to assign %s : %s", field.Name, err)
}
}
return nil
}