-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcoerce.go
395 lines (328 loc) · 9.25 KB
/
coerce.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/**
* coerce is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* coerce is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
** Authors:
*
* - Daniel <SeeSpotRun> T. 2016-2016 (https://github.com/SeeSpotRun)
*
** Hosted on https://github.com/SeeSpotRun/coerce
*
**/
package coerce
// package coerce coerces map[string]interface{} values into struct fields
import (
"fmt"
"reflect"
"regexp"
"strconv"
"strings"
"time"
"unsafe"
)
// Struct attempts to unmarshall the values in 'from' into the fields
// in the structure pointed to by 'target'. The field names are used as
// map keys. Optional format strings can be used to morph the field
// names into keys, eg "--%s" will map field "foo" to key "--foo".
// If more than one format is supplied, these will be tried in order
// until the first matching key is found.
// When coercing from string to any integer types, if the string ends
// with B|K|M|G|T (case-insensitive) then these will be interpreted
// as multipliers of 1, 1024, etc.
//
// Example:
// type x struct{
// intslice []int
// boolval bool
// s string
// }
//
// mymap := map[string]interface{} {
// "--intslice": []string {"5", "12", "0.5k"},
// "--boolval" : true,
// "-s" : "hello",
// }
//
// var myx x
//
// err := coerce.Struct(&myx, mymap, "--%s", "-%s")
// fmt.Println(err, myx) // <nil> {[5 12 512] true hello}
//
// Note: coercing unexported fields uses 'unsafe' pointers
//
func Struct(to interface{}, from map[string]interface{}, formats ...string) error {
// parse errors are accumulated into errstr
errstr := ""
// get target as reflect.Value and check kind:
pt := reflect.ValueOf(to)
vt := reflect.Indirect(pt)
if vt.Kind() != reflect.Struct || pt.Kind() != reflect.Ptr {
return fmt.Errorf("expected *struct for 'to', got %v", pt.Kind())
}
// iterate over struct fields
for i := 0; i < vt.NumField(); i++ {
// get field type and pointer to value
f := vt.Type().Field(i)
vf := vt.Field(i)
if !vf.CanSet() {
// use 'unsafe' workaround for unexported fields:
if string(f.Name[0]) == strings.ToLower(string(f.Name[0])) {
pu := unsafe.Pointer(vf.Addr().Pointer())
vf = reflect.Indirect(reflect.NewAt(vf.Type(), pu))
}
if !vf.CanSet() {
errstr += "field " + f.Name + "not setable\n"
continue
}
}
// look for field name in map keys
v, err := findVal(f.Name, from, formats)
if err != nil {
continue
}
if v == nil {
// nil value in map - leave the field alone
continue
}
vv := reflect.ValueOf(v)
err = unmarshall(vf, vv)
if err != nil {
errstr += err.Error() + "\n"
}
}
if errstr != "" {
return fmt.Errorf("%s", errstr[:len(errstr)-1]) // strips trailling newline
}
return nil
}
// Var attempts to cast the content of 'from' into the variable pointed to by 'pto'
func Var(pto interface{}, from interface{}) error {
return unmarshall(reflect.Indirect(reflect.ValueOf(pto)), reflect.ValueOf(from))
}
// unmarshallString parses string s to in vto
func unmarshallString(vto reflect.Value, tto reflect.Type, s string) error {
// custom handlers for non-builtin types:
switch tto.String() {
case "time.Duration":
d, e := time.ParseDuration(s)
if e != nil {
return e
}
vto.Set(reflect.ValueOf(d))
return nil
}
// handle builtin types:
switch vto.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
ival, err := strconv.ParseInt(s, 10, tto.Bits())
if err != nil {
// try again looking for B/K/M/G/T
ival, err = getBytes(s, err)
if err != nil {
return err
}
}
vto.SetInt(ival)
return nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
uval, err := strconv.ParseUint(s, 10, tto.Bits())
if err != nil {
// try again looking for B/K/M/G/T
ival, e := getBytes(s, err)
if e != nil {
return e
}
uval = uint64(ival)
}
vto.SetUint(uval)
return nil
case reflect.Float32, reflect.Float64:
fval, err := strconv.ParseFloat(s, tto.Bits())
if err != nil {
return err
}
vto.SetFloat(fval)
return nil
}
return fmt.Errorf("don't know how to unmarshall string to %v\n", tto)
}
// unmarshallFloat marshalls a float value into vto
func unmarshallFloat(vto reflect.Value, tto reflect.Type, f float64) error {
switch tto.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
vto.SetInt(int64(f))
return nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
vto.SetUint(uint64(f))
return nil
}
return fmt.Errorf("don't know how to unmarshall float to %v\n", tto)
}
// unmarshall tries to parse vfrom value into vto
func unmarshall(vto reflect.Value, vfrom reflect.Value) error {
// try for direct assign:
tto := vto.Type()
if vfrom.Type().AssignableTo(tto) {
vto.Set(vfrom)
return nil
}
// unmarshall from slices recursively:
if vfrom.Kind() == reflect.Slice {
if vto.Kind() == reflect.Slice {
// ...to a slice:
// set slice size:
vto.Set(reflect.MakeSlice(vto.Type(), vfrom.Len(), vfrom.Len()))
for j := 0; j < vfrom.Len(); j++ {
// unmarshall slice elements
err := unmarshall(vto.Index(j), vfrom.Index(j))
if err != nil {
return err
}
}
return nil
} else if vfrom.Len() == 1 {
// tolerate mapping of slices with length==1 to a single field
return unmarshall(vto, vfrom.Index(0))
} else {
return fmt.Errorf("can't coerce %v from multi-value slice", tto)
}
}
// unmarshalling to string is easy: let fmt do the thinking:
if tto.Kind() == reflect.String {
vto.SetString(fmt.Sprintf("%v", vfrom.Interface()))
return nil
}
// case-by-case for everything else:
switch vfrom.Kind() {
case reflect.String:
return unmarshallString(vto, tto, vfrom.String())
case reflect.Float32, reflect.Float64:
return unmarshallFloat(vto, tto, vfrom.Float())
// case Int, Uint etc should generally be handled by AssignableTo or fmt.Sprintf
}
return fmt.Errorf("Don't know how to unmarshall %v to %v\n", vfrom.Type(), tto)
}
// Int tries to return an int value based on content of 'from'
func Int(from interface{}) (i int, e error) {
e = Var(&i, from)
return
}
// Int64 tries to return an int64 value based on content of 'from'
func Int64(from interface{}) (i int64, e error) {
e = Var(&i, from)
return
}
// Uint tries to return a uint value based on content of 'from'
func Uint(from interface{}) (u uint, e error) {
e = Var(&u, from)
return
}
// Uint64 tries to return a uint64 value based on content of 'from'
func Uint64(from interface{}) (u int64, e error) {
e = Var(&u, from)
return
}
// Float32 tries to return a float32 value based on content of 'from'
func Float32(from interface{}) (f float32, e error) {
e = Var(&f, from)
return
}
// Float64 tries to return a float64 value based on content of 'from'
func Float64(from interface{}) (f float64, e error) {
e = Var(&f, from)
return
}
// String is equivalent to fmt.Sprint(from)
func String(from interface{}) (s string) {
Var(&s, from)
return
}
// findVal tries to find map key matching field name formatted as per formats
func findVal(baseName string, from map[string]interface{}, formats []string) (interface{}, error) {
if len(formats) == 0 {
// handle case where no formats supplied
formats = []string{"%s"}
}
var result interface{}
var ok bool
tried := "" // accumulates patterns tried, for possible error reporting
Found:
for _, name := range nameVariants(baseName) {
for _, pat := range formats {
key := fmt.Sprintf(pat, name)
result, ok = from[key]
if ok {
break Found
}
tried += key + "|"
}
}
if !ok {
return nil, fmt.Errorf("[%s] not found in map", tried[:len(tried)-1])
}
return result, nil
}
var uppersRE = regexp.MustCompile(`[[:upper:]]`)
func nameVariants(base string) []string {
hyphens := strings.TrimLeft(uppersRE.ReplaceAllStringFunc(base, func(ch string) string {
return "-" + ch
}), "-")
unders := strings.TrimLeft(uppersRE.ReplaceAllStringFunc(base, func(ch string) string {
return "_" + ch
}), "_")
allM := map[string]struct{}{
base: struct{}{},
strings.ToLower(base): struct{}{},
hyphens: struct{}{},
unders: struct{}{},
strings.ToLower(hyphens): struct{}{},
strings.ToLower(unders): struct{}{},
}
var all []string
for n := range allM {
all = append(all, n)
}
return all
}
// getBytes parses strings of the format '1.2G' and interprets a kB, MB,
// GB etc.
func getBytes(s string, err error) (int64, error) {
const (
b = 1
k = b << 10
m = k << 10
g = m << 10
t = g << 10
)
var mult int64
switch strings.ToUpper(string(s[len(s)-1])) {
case "B":
mult = b
case "K":
mult = k
case "M":
mult = m
case "G":
mult = g
case "T":
mult = t
default:
return 0, err
}
ival, err := strconv.ParseInt(s[:len(s)-1], 10, 64)
if err != nil {
fval, err := strconv.ParseFloat(s[:len(s)-1], 64)
if err != nil {
return 0, err
}
return int64(fval * float64(mult)), nil
}
return ival * mult, nil
}