-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode.go
458 lines (410 loc) · 12.1 KB
/
decode.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
package opt
import (
"fmt"
"math"
"net/url"
"reflect"
"strconv"
"strings"
)
// unmarshalOpt parses variables from the command-line and sets them into
// fields of object.
//
// Returns an error if it is impossible to parse the command line, for example:
// there are an arg not provided in the go-structure; several values will be
// passed for a field that is not a slice or array; etc.
//
// Generates panic if the structure has fields of the wrong type, for example:
// field with the "?" key is not a string; the field with the "[]" key is not
// a slice or array; the object is not a pointer; for unsupported field types
// like: chan, map, etc..
//
// unmarshalOpt method supports the following field's types: int, int8, int16,
// int32, int64, uin, uint8, uin16, uint32, in64, float32, float64, string,
// bool, url.URL and pointers, array or slice from thous types (i.e. *int, ...,
// []int, ..., []bool, ..., [2]*url.URL, etc.).
func unmarshalOpt(obj interface{}, args []string) []error {
var errs []error
// Analyze the structure and return a list of molds
// of each field: field name, tag group and field pointer.
//
// If it's returns an error - be critical!
// This is a problem with an incorrect structure and we need to
// stop the program until the developer fixes this error.
fcl, err := getFieldCastList(obj)
if err != nil {
// Convert this error to panic because it's a problem of
// structure's fields (it's developer's problem).
panic(err)
}
// Parse options.
am := argMap{}
if err := am.parse(args, fcl.flags()); err != nil {
errs = append(errs, err)
}
// Insert values into the fields of the structure
// from the command line arguments.
//
// Important: Do not interrupt the cycle on the first error:
// - if there is a field for reference information,
// it should be processed in any case;
// - need to collect all possible errors.
for _, fc := range fcl {
if fc.tagGroup.shortFlag == "?" {
// Generate help info.
// The field must be of the string type, see in
// the getFieldCastList function.
help := getHelp(fcl, am)
fc.item.Set(reflect.ValueOf(help))
continue
}
// Contains a couple of arguments like: U, users, U or/and users -
// where U and users is synonyms.
// arg := fmt.Sprintf(
// "%s tmp/and %s", // <- tmp/and is a temporary string
// fc.tagGroup.shortFlag,
// fc.tagGroup.longFlag,
// )
// arg = strings.Trim(arg, " or/and ")
arg := strings.ReplaceAll(
fmt.Sprintf("%s or/and %s",
fc.tagGroup.shortFlag,
fc.tagGroup.longFlag),
" or/and ",
"",
)
value, kind, ok := []string{}, fc.item.Kind(), false
switch f := fc.tagGroup.shortFlag; {
case f == "[]":
// Get positional arguments.
value = am.posValues()
default:
// Get the values of the argument.
value, ok = am.flagValue(
fc.tagGroup.shortFlag,
fc.tagGroup.longFlag,
fc.tagGroup.defValue,
fc.tagGroup.sepList,
)
// The user in the command line tries to pass arguments as
// list to a field that doesn't have the slice or array type.
if len(value) > 1 {
if kind != reflect.Array && kind != reflect.Slice {
// In this situation, we need to take the
// last value in the list.
//
// return fmt.Errorf("%s used more than once", arg)
value = []string{value[len(value)-1]}
}
}
}
// Set values of the desired type.
switch kind {
case reflect.Array:
// If a separator is specified, the elements must be separated.
var result []string
// If the argMap.flagValue hasn't value it's returns
// []string{defValue} where defValue can be like "" -
// this is not valid for the list because if the command line
// argument has no data for the list this list must be empty!
if !ok && len(value) == 1 && value[0] == "" {
break
}
if sep := fc.tagGroup.sepList; sep != "" {
for _, item := range value {
tmp := strings.Split(item, sep)
result = append(result, tmp...)
}
} else {
result = value
}
if max := fc.item.Type().Len(); len(result) > max {
// Array overflow.
// -> "%d items overflow [%d]%v array", len(result), max, kind,
// kind := fs.item.Index(0).Kind()
return []error{fmt.Errorf(
"maximum number of values for %s argument "+
"is %d but passed %d values",
arg, max, len(result),
)}
}
err = setSequence(fc.item, result)
case reflect.Slice:
// Be sure to set Len equal Cap and more than zero.
// The slice must have at least one element to determine
// the type of the one.
// If a separator is specified, the elements must be separated.
var result []string
// If the argMap.flagValue hasn't value it's returns
// []string{defValue} where defValue can be like "" -
// this is not valid for the list because if the command line
// argument has no data for the list this list must be empty!
if !ok && len(value) == 1 && value[0] == "" {
break
}
if sep := fc.tagGroup.sepList; sep != "" {
for _, item := range value {
tmp := strings.Split(item, sep)
result = append(result, tmp...)
}
} else {
result = value
}
if len(result) != 0 {
size := len(result)
tmp := reflect.MakeSlice(fc.item.Type(), size, size)
err = setSequence(&tmp, result)
if err == nil {
fc.item.Set(reflect.AppendSlice(*fc.item, tmp))
}
}
case reflect.Ptr:
if fc.item.Type().Elem().Kind() != reflect.Struct {
// If the pointer is not to a structure.
tmp := reflect.Indirect(*fc.item)
err = setValue(tmp, value[len(value)-1])
} else {
// If a pointer to a structure of the url.URL.
err = setValue(*fc.item, value[len(value)-1])
}
case reflect.Struct:
// Structure of the url.URL.
err = setValue(*fc.item, value[len(value)-1])
default:
// Set any type.
err = setValue(*fc.item, value[len(value)-1])
}
if err != nil {
errs = append(errs, err)
}
}
return errs
}
// The setSequence sets slice into item.
func setSequence(item *reflect.Value, seq []string) (err error) {
// defer func() {
// // Catch the panic and return an exception as a value.
// if r := recover(); r != nil {
// err = fmt.Errorf("%v", r)
// }
// }()
if item.Len() != len(seq) {
return nil
}
// Set values from sequence.
for i, value := range seq {
elem := item.Index(i)
err := setValue(elem, value)
if err != nil {
return err
}
}
return nil
}
// The setValue sets value into field.
func setValue(item reflect.Value, value string) (err error) {
defer func() {
// Catch the panic and return an exception as a value.
if r := recover(); r != nil {
err = fmt.Errorf("%v", r)
}
}()
kind := item.Kind()
switch kind {
case reflect.Int, reflect.Int8, reflect.Int16,
reflect.Int32, reflect.Int64:
r, err := strToIntKind(value, kind)
if err != nil {
return err
}
item.SetInt(r)
case reflect.Uint, reflect.Uint8, reflect.Uint16,
reflect.Uint32, reflect.Uint64:
r, err := strToUintKind(value, kind)
if err != nil {
return err
}
item.SetUint(r)
case reflect.Float32, reflect.Float64:
r, err := strToFloatKind(value, kind)
if err != nil {
return err
}
item.SetFloat(r)
case reflect.Bool:
r, err := strToBool(value)
if err != nil {
return err
}
item.SetBool(r)
case reflect.String:
item.SetString(value)
case reflect.Ptr:
// // Will not be allowed by the getFieldCast method.
// if item.Type() != reflect.TypeOf((*url.URL)(nil)) {
// return fmt.Errorf("%s field has invalid type", item.Type().Name())
// }
// The url.URL struct only.
u, err := url.Parse(value)
if err != nil {
return err
}
item.Set(reflect.ValueOf(u))
case reflect.Struct:
// // Will not be allowed by the getFieldCast method.
// if item.Type() != reflect.TypeOf(url.URL{}) {
// return fmt.Errorf("%s field has invalid type", item.Type().Name())
// }
// The url.URL struct only.
u, err := url.Parse(value)
if err != nil {
return err
}
item.Set(reflect.ValueOf(*u))
default:
return fmt.Errorf("%s field has invalid type", item.Type().Name())
}
return nil
}
// The strToIntKind convert string to int64 type with checking for conversion
// to intX type. Returns default value for int type if value is empty.
//
// P.s. The intX determined by reflect.Kind.
func strToIntKind(value string, kind reflect.Kind) (r int64, err error) {
// For empty string returns zero.
if len(value) == 0 {
return 0, nil
}
// Convert string to int64.
r, err = strconv.ParseInt(value, 10, 64)
if err != nil {
return 0, fmt.Errorf("'%v' is incorrect value", value)
}
switch kind {
case reflect.Int:
// If there was no exception during the conversion,
// then we have exactly the number in the uint64 range, but
// for 32-bit platform it is necessary to check overflow.
if strconv.IntSize == 32 {
if r < math.MinInt32 || r > math.MaxInt32 {
return 0, fmt.Errorf("%d overflows int32", r)
}
}
case reflect.Int8:
if r < math.MinInt8 || r > math.MaxInt8 {
return 0, fmt.Errorf("%d overflows int8", r)
}
case reflect.Int16:
if r < math.MinInt16 || r > math.MaxInt16 {
return 0, fmt.Errorf("%d overflows int16", r)
}
case reflect.Int32:
if r < math.MinInt32 || r > math.MaxInt32 {
return 0, fmt.Errorf("%d overflows int32", r)
}
case reflect.Int64:
// If there was no exception during the conversion,
// then we have exactly the number in the int64 range.
default:
r, err = 0, fmt.Errorf("incorrect kind %v", kind)
}
return
}
// strToUintKind convert string to uint64 type with checking for conversion
// to uintX type. Returns default value for uint type if value is empty.
//
// P.s. The uintX determined by reflect.Kind.
func strToUintKind(value string, kind reflect.Kind) (r uint64, err error) {
// For empty string returns zero.
if len(value) == 0 {
return 0, nil
}
// Convert string to uint64.
r, err = strconv.ParseUint(value, 10, 64)
if err != nil {
return 0, fmt.Errorf(
"'%v' has incorrect type, positive number expected",
value,
)
}
switch kind {
case reflect.Uint:
// If there was no exception during the conversion,
// then we have exactly the number in the uint64 range, but
// for 32-bit platform it is necessary to check overflow.
if strconv.IntSize == 32 && r > math.MaxUint32 {
return 0, fmt.Errorf("%d overflows uint32", r)
}
case reflect.Uint8:
if r > math.MaxUint8 {
return 0, fmt.Errorf("%d overflows uint8", r)
}
case reflect.Uint16:
if r > math.MaxUint16 {
return 0, fmt.Errorf("%d overflows uint16", r)
}
case reflect.Uint32:
if r > math.MaxUint32 {
return 0, fmt.Errorf("strToUint32: %d overflows uint32", r)
}
case reflect.Uint64:
// If there was no exception during the conversion,
// then we have exactly the number in the uint64 range.
default:
r, err = 0, fmt.Errorf("incorrect kind %v", kind)
}
return
}
// strToFloatKind convert string to float64 type with checking for conversion
// to floatX type. Returns default value for float64 type if value is empty.
//
// P.s. The floatX determined by reflect.Kind.
func strToFloatKind(value string, kind reflect.Kind) (r float64, err error) {
// For empty string returns zero.
if len(value) == 0 {
return 0.0, nil
}
// Convert string to Float64.
r, err = strconv.ParseFloat(value, 64)
if err != nil {
return 0.0, fmt.Errorf(
"'%v' has incorrect type, number expected",
value,
)
}
switch kind {
case reflect.Float32:
if math.Abs(r) > math.MaxFloat32 {
return 0.0, fmt.Errorf("%f overflows float32", r)
}
case reflect.Float64:
// If there was no exception during the conversion,
// then we have exactly the number in the float64 range.
default:
r, err = 0, fmt.Errorf("incorrect kind")
}
return
}
// strToBool convert string to bool type. Returns: result, error.
// Returns default value for bool type if value is empty.
func strToBool(value string) (bool, error) {
epsilon := math.Nextafter(1, 2) - 1
// For empty string returns false.
if len(value) == 0 {
return false, nil
}
r, errB := strconv.ParseBool(value)
if errB != nil {
f, errF := strconv.ParseFloat(value, 64)
if errF != nil {
return r, fmt.Errorf(
"'%v' has incorrect type, bool expected",
value,
)
}
if math.Abs(f) > epsilon {
r = true
}
}
return r, nil
}