-
Notifications
You must be signed in to change notification settings - Fork 8
/
cvte.go
541 lines (477 loc) · 12.3 KB
/
cvte.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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
package cvt
import (
"encoding/json"
"errors"
"fmt"
"math"
"reflect"
"sort"
"strconv"
"strings"
)
var errConvFail = errors.New("convert failed")
var errFieldNotFound = errors.New("field not found")
var errUnsupportedTypeNil = errors.New("unsupported type: nil")
var formatOutOfLimitInt = "%w, out of max limit value(%d)"
var formatOutOfLimitFloat = "%w, out of max limit value(%f)"
var formatExtend = "%v, %w"
// BoolE convert an interface to a bool type
func BoolE(val interface{}) (bool, error) {
v, rk, rv := Indirect(val)
switch vv := v.(type) {
case bool:
return vv, nil
case int, int8, int16, int32, int64:
return rv.Int() != 0, nil
case uint, uint8, uint16, uint32, uint64:
return rv.Uint() != 0, nil
case float32, float64:
return rv.Float() != 0, nil
case []byte:
return str2bool(string(vv))
case string:
return str2bool(vv)
case nil:
return false, nil
}
switch rk.Kind() {
// by elem length
case reflect.Array, reflect.Slice, reflect.Map:
return rv.Len() > 0, nil
}
return false, newErr(val, "bool")
}
// returns the boolean value represented by the string
func str2bool(str string) (bool, error) {
if val, err := strconv.ParseBool(str); err == nil {
return val, nil
} else if val, err := strconv.ParseFloat(str, 64); err == nil {
return val != 0, nil
}
switch strings.ToLower(strings.TrimSpace(str)) {
case "on":
return true, nil
case "off":
return false, nil
default:
return false, newErr(str, "bool")
}
}
// Uint64E convert an interface to a uint64 type
func Uint64E(val interface{}) (uint64, error) {
v, e := convUint64(val)
if e := catch("uint64", val, e); e != nil {
return 0, e
}
return v, nil
}
// Uint32E convert an interface to a uint32 type
func Uint32E(val interface{}) (uint32, error) {
v, e := convUint64(val)
if e := catch("uint32", val, e); e != nil {
return 0, e
}
if v > math.MaxUint32 {
return 0, fmt.Errorf(formatOutOfLimitInt, newErr(val, "uint32"), uint32(math.MaxUint32))
}
return uint32(v), nil
}
// Uint16E convert an interface to a uint16 type
func Uint16E(val interface{}) (uint16, error) {
v, e := convUint64(val)
if e := catch("uint16", val, e); e != nil {
return 0, e
}
if v > math.MaxUint16 {
return 0, fmt.Errorf(formatOutOfLimitInt, newErr(val, "uint16"), uint16(math.MaxUint16))
}
return uint16(v), nil
}
// Uint8E convert an interface to a uint8 type
func Uint8E(val interface{}) (uint8, error) {
v, e := convUint64(val)
if e := catch("uint8", val, e); e != nil {
return 0, e
}
if v > math.MaxUint8 {
return 0, fmt.Errorf(formatOutOfLimitInt, newErr(val, "uint8"), uint8(math.MaxUint8))
}
return uint8(v), nil
}
// UintE convert an interface to a uint type
func UintE(val interface{}) (uint, error) {
v, e := convUint64(val)
if e := catch("uint", val, e); e != nil {
return 0, e
}
if v > uint64(^uint(0)) {
return 0, fmt.Errorf(formatOutOfLimitInt, newErr(val, "uint"), ^uint(0))
}
return uint(v), nil
}
func convUint64(val interface{}) (uint64, error) {
v, _, rv := Indirect(val)
switch vv := v.(type) {
case nil:
return 0, nil
case bool:
if vv {
return 1, nil
}
return 0, nil
case string:
vvv, err := strconv.ParseFloat(vv, 64)
if err == nil && vvv >= 0 && vvv <= math.MaxUint64 {
return uint64(math.Trunc(vvv)), nil
}
case []byte:
vvv, err := strconv.ParseFloat(string(vv), 64)
if err == nil && vvv >= 0 && vvv <= math.MaxUint64 {
return uint64(math.Trunc(vvv)), nil
}
case uint, uint8, uint16, uint32, uint64:
return rv.Uint(), nil
case int, int8, int16, int32, int64:
if rv.Int() >= 0 {
return uint64(rv.Int()), nil
}
case float32, float64:
if rv.Float() >= 0 && rv.Float() <= math.MaxUint64 {
return uint64(math.Trunc(rv.Float())), nil
}
}
return 0, errConvFail
}
// Int64E convert an interface to a int64 type
func Int64E(val interface{}) (int64, error) {
v, e := convInt64(val)
if e := catch("int64", val, e); e != nil {
return 0, e
}
return v, nil
}
// Int32E convert an interface to a int32 type
func Int32E(val interface{}) (int32, error) {
v, e := convInt64(val)
if e := catch("int32", val, e); e != nil {
return 0, e
}
if v > math.MaxInt32 {
return 0, fmt.Errorf(formatOutOfLimitInt, newErr(val, "int32"), int32(math.MaxInt32))
}
return int32(v), nil
}
// Int16E convert an interface to a int16 type
func Int16E(val interface{}) (int16, error) {
v, e := convInt64(val)
if e := catch("int16", val, e); e != nil {
return 0, e
}
if v > math.MaxInt16 {
return 0, fmt.Errorf(formatOutOfLimitInt, newErr(val, "int16"), int16(math.MaxInt16))
}
return int16(v), nil
}
// Int8E convert an interface to a int8 type
func Int8E(val interface{}) (int8, error) {
v, e := convInt64(val)
if e := catch("int8", val, e); e != nil {
return 0, e
}
if v > math.MaxInt8 {
return 0, fmt.Errorf(formatOutOfLimitInt, newErr(val, "int8"), int8(math.MaxInt8))
}
return int8(v), nil
}
// IntE convert an interface to a int type
func IntE(val interface{}) (int, error) {
v, e := convInt64(val)
if e := catch("int", val, e); e != nil {
return 0, e
}
if strconv.IntSize == 32 && v > math.MaxInt32 {
return 0, fmt.Errorf(formatOutOfLimitInt, newErr(val, "int"), int32(math.MaxInt32))
}
return int(v), nil
}
func convInt64(val interface{}) (int64, error) {
v, _, rv := Indirect(val)
switch vv := v.(type) {
case nil:
return 0, nil
case bool:
if vv {
return 1, nil
}
return 0, nil
case string:
vvv, err := strconv.ParseFloat(vv, 64)
if err == nil && vvv <= math.MaxInt64 {
return int64(math.Trunc(vvv)), nil
}
case []byte:
vvv, err := strconv.ParseFloat(string(vv), 64)
if err == nil && vvv <= math.MaxInt64 {
return int64(math.Trunc(vvv)), nil
}
case uint, uint8, uint16, uint32, uint64, uintptr:
if rv.Uint() <= math.MaxInt64 {
return int64(rv.Uint()), nil
}
case int, int8, int16, int32, int64:
return rv.Int(), nil
case float32, float64:
if rv.Float() <= math.MaxInt64 {
return int64(math.Trunc(rv.Float())), nil
}
}
return 0, errConvFail
}
// Float64E convert an interface to a float64 type
func Float64E(val interface{}) (float64, error) {
v, _, rv := Indirect(val)
switch vv := v.(type) {
case nil:
return 0, nil
case bool:
if vv {
return 1, nil
}
return 0, nil
case string:
vvv, err := strconv.ParseFloat(vv, 64)
if err == nil {
return vvv, nil
}
case []byte:
vvv, err := strconv.ParseFloat(string(vv), 64)
if err == nil {
return vvv, nil
}
case uint, uint8, uint16, uint32, uint64, uintptr:
return float64(rv.Uint()), nil
case int, int8, int16, int32, int64:
return float64(rv.Int()), nil
case float32:
// use fmt to fix float32 -> float64 precision loss
// eg: cvt.Float64E(float32(8.31))
vvv, err := strconv.ParseFloat(fmt.Sprintf("%f", vv), 64)
if err == nil {
return vvv, nil
}
case float64:
return vv, nil
}
return 0, errConvFail
}
// Float32E convert an interface to a float32 type
func Float32E(val interface{}) (float32, error) {
v, e := Float64E(val)
if e := catch("float32", val, e); e != nil {
return 0, e
}
if v > math.MaxFloat32 {
return 0, fmt.Errorf(formatOutOfLimitFloat, newErr(val, "float32"), float32(math.MaxFloat32))
}
return float32(v), nil
}
// StringE convert an interface to a string type
func StringE(val interface{}) (string, error) {
v, _, rv := Indirect(val)
// interface implements
switch vv := val.(type) {
case fmt.Stringer:
return vv.String(), nil
case error:
return vv.Error(), nil
case json.Marshaler:
vvv, e := vv.MarshalJSON()
if e == nil {
return string(vvv), nil
}
}
// source type
switch vv := v.(type) {
case nil:
return "", nil
case bool:
return strconv.FormatBool(vv), nil
case string:
return vv, nil
case []byte:
return string(vv), nil
case []rune:
return string(vv), nil
case uint, uint8, uint16, uint32, uint64, uintptr:
return strconv.FormatUint(rv.Uint(), 10), nil
case int, int8, int16, int32, int64:
return strconv.FormatInt(rv.Int(), 10), nil
case float64:
return strconv.FormatFloat(vv, 'f', -1, 64), nil
case float32:
return strconv.FormatFloat(float64(vv), 'f', -1, 32), nil
}
return "", newErr(val, "string")
}
// SliceE convert an interface to a []interface{} type
func SliceE(val interface{}) (sl []interface{}, err error) {
if val == nil {
return nil, errUnsupportedTypeNil
}
_, rt, rv := Indirect(val)
switch rt.Kind() {
case reflect.String:
for _, vvv := range rv.String() {
sl = append(sl, vvv)
}
return
case reflect.Slice, reflect.Array:
for j := 0; j < rv.Len(); j++ {
sl = append(sl, rv.Index(j).Interface())
}
return
case reflect.Map:
for _, key := range sortedMapKeys(rv) {
sl = append(sl, rv.MapIndex(key).Interface())
}
return
case reflect.Struct:
sl = deepStructValues(rt, rv)
return
}
return nil, newErr(val, "slice")
}
// return the values of struct fields, and deep find the embedded fields
func deepStructValues(rt reflect.Type, rv reflect.Value) (sl []interface{}) {
for j := 0; j < rv.NumField(); j++ {
if rt.Field(j).Anonymous {
sl = append(sl, deepStructValues(rt.Field(j).Type, rv.Field(j))...)
} else if rv.Field(j).CanInterface() {
sl = append(sl, rv.Field(j).Interface())
}
}
return
}
// return the map keys, which sorted by asc
func sortedMapKeys(v reflect.Value) (s []reflect.Value) {
s = v.MapKeys()
sort.Slice(s, func(i, j int) bool {
return strings.Compare(String(s[i].Interface()), String(s[j].Interface())) < 0
})
return
}
// FieldE return the field value from map/struct, ignore the filed type
func FieldE(val interface{}, field interface{}) (interface{}, error) {
if val == nil {
return nil, errUnsupportedTypeNil
}
sf := String(field) // match with the String of field, so field can be any type
_, rt, rv := Indirect(val)
switch rt.Kind() {
case reflect.Map: // key of map
for _, key := range rv.MapKeys() {
if String(key.Interface()) == sf {
return rv.MapIndex(key).Interface(), nil
}
}
case reflect.Struct: // field of struct
vv := rv.FieldByName(sf)
if vv.IsValid() {
return vv.Interface(), nil
}
}
return nil, fmt.Errorf("%w(%s)", errFieldNotFound, sf)
}
// ColumnsE return the values from a single column in the input array/slice/map of struct/map
func ColumnsE(val interface{}, field interface{}) (sl []interface{}, err error) {
if val == nil {
return nil, errUnsupportedTypeNil
}
_, rt, rv := Indirect(val)
switch rt.Kind() {
case reflect.Slice, reflect.Array:
for j := 0; j < rv.Len(); j++ {
vv, e := FieldE(rv.Index(j).Interface(), field)
if e == nil {
sl = append(sl, vv)
}
}
case reflect.Map:
for _, key := range sortedMapKeys(rv) {
vv, e := FieldE(rv.MapIndex(key).Interface(), field)
if e == nil {
sl = append(sl, vv)
}
}
}
// non valid field value, means error
if len(sl) > 0 {
return
}
return nil, fmt.Errorf("unsupported type: %s", rt.Kind())
}
// Indirect returns the value with base type
func Indirect(a interface{}) (val interface{}, rt reflect.Type, rv reflect.Value) {
if a == nil {
return
}
rt = reflect.TypeOf(a)
rv = reflect.ValueOf(a)
switch rt.Kind() {
case reflect.Ptr: // indirect the base type, if is be referenced many times
for rv.Kind() == reflect.Ptr && !rv.IsNil() {
rv = rv.Elem()
}
return Indirect(rv.Interface())
case reflect.Bool:
val = rv.Bool()
case reflect.Int:
val = int(rv.Int())
case reflect.Int8:
val = int8(rv.Int())
case reflect.Int16:
val = int16(rv.Int())
case reflect.Int32:
val = int32(rv.Int())
case reflect.Int64:
val = rv.Int()
case reflect.Uint:
val = uint(rv.Uint())
case reflect.Uint8:
val = uint8(rv.Uint())
case reflect.Uint16:
val = uint16(rv.Uint())
case reflect.Uint32:
val = uint32(rv.Uint())
case reflect.Uint64:
val = rv.Uint()
case reflect.Uintptr:
val = uintptr(rv.Uint())
case reflect.Float32:
val = float32(rv.Float())
case reflect.Float64:
val = rv.Float()
case reflect.Complex64:
val = complex64(rv.Complex())
case reflect.Complex128:
val = rv.Complex()
case reflect.String:
val = rv.String()
default:
val = rv.Interface()
}
return
}
func newErr(val interface{}, t string) error {
return fmt.Errorf("unable to convert %#v of type %T to %s", val, val, t)
}
// catching an error and return a new
func catch(t string, val interface{}, e error) error {
if e != nil {
if errors.Is(e, errConvFail) {
return newErr(val, t)
}
return fmt.Errorf(formatExtend, newErr(val, t), e)
}
return nil
}