-
Notifications
You must be signed in to change notification settings - Fork 82
/
interface.go
812 lines (782 loc) · 21.6 KB
/
interface.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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
package ameda
import (
"fmt"
"reflect"
)
// InterfaceToInterfacePtr converts interface to *interface.
func InterfaceToInterfacePtr(i interface{}) *interface{} {
return &i
}
// InterfaceToString converts interface to string.
func InterfaceToString(i interface{}) string {
return fmt.Sprintf("%v", i)
}
// InterfaceToStringPtr converts interface to *string.
func InterfaceToStringPtr(i interface{}) *string {
v := InterfaceToString(i)
return &v
}
// InterfaceToBool converts interface to bool.
// NOTE:
// 0 is false, other numbers are true
func InterfaceToBool(i interface{}, emptyAsFalse ...bool) (bool, error) {
switch v := i.(type) {
case bool:
return v, nil
case nil:
return false, nil
case float32:
return Float32ToBool(v), nil
case float64:
return Float64ToBool(v), nil
case int:
return IntToBool(v), nil
case int8:
return Int8ToBool(v), nil
case int16:
return Int16ToBool(v), nil
case int32:
return Int32ToBool(v), nil
case int64:
return Int64ToBool(v), nil
case uint:
return UintToBool(v), nil
case uint8:
return Uint8ToBool(v), nil
case uint16:
return Uint16ToBool(v), nil
case uint32:
return Uint32ToBool(v), nil
case uint64:
return Uint64ToBool(v), nil
case uintptr:
return v != 0, nil
case string:
return StringToBool(v, emptyAsFalse...)
default:
r := IndirectValue(reflect.ValueOf(i))
switch r.Kind() {
case reflect.Bool:
return r.Bool(), nil
case reflect.Invalid:
return false, nil
case reflect.Float32, reflect.Float64:
return Float64ToBool(r.Float()), nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return Int64ToBool(r.Int()), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return Uint64ToBool(r.Uint()), nil
case reflect.String:
return StringToBool(r.String(), emptyAsFalse...)
}
if isEmptyAsZero(emptyAsFalse) {
return !isZero(r), nil
}
return false, fmt.Errorf("cannot convert %#v of type %T to bool", i, i)
}
}
// InterfaceToBoolPtr converts interface to *bool.
// NOTE:
// 0 is false, other numbers are true
func InterfaceToBoolPtr(i interface{}, emptyAsFalse ...bool) (*bool, error) {
r, err := InterfaceToBool(i, emptyAsFalse...)
return &r, err
}
// InterfaceToFloat32 converts interface to float32.
func InterfaceToFloat32(i interface{}, emptyStringAsZero ...bool) (float32, error) {
switch v := i.(type) {
case bool:
return BoolToFloat32(v), nil
case nil:
return 0, nil
case int:
return IntToFloat32(v), nil
case int8:
return Int8ToFloat32(v), nil
case int16:
return Int16ToFloat32(v), nil
case int32:
return Int32ToFloat32(v), nil
case int64:
return Int64ToFloat32(v), nil
case uint:
return UintToFloat32(v), nil
case uint8:
return Uint8ToFloat32(v), nil
case uint16:
return Uint16ToFloat32(v), nil
case uint32:
return Uint32ToFloat32(v), nil
case uint64:
return Uint64ToFloat32(v), nil
case uintptr:
return UintToFloat32(uint(v)), nil
case string:
return StringToFloat32(v, emptyStringAsZero...)
default:
r := IndirectValue(reflect.ValueOf(i))
switch r.Kind() {
case reflect.Bool:
return BoolToFloat32(r.Bool()), nil
case reflect.Invalid:
return 0, nil
case reflect.Float32:
return float32(r.Float()), nil
case reflect.Float64:
return Float64ToFloat32(r.Float())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return Int64ToFloat32(r.Int()), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return Uint64ToFloat32(r.Uint()), nil
case reflect.String:
return StringToFloat32(r.String(), emptyStringAsZero...)
}
if isEmptyAsZero(emptyStringAsZero) {
return BoolToFloat32(!isZero(r)), nil
}
return 0, fmt.Errorf("cannot convert %#v of type %T to float32", i, i)
}
}
// InterfaceToFloat32Ptr converts interface to *float32.
func InterfaceToFloat32Ptr(i interface{}, emptyAsFalse ...bool) (*float32, error) {
r, err := InterfaceToFloat32(i, emptyAsFalse...)
return &r, err
}
// InterfaceToFloat64 converts interface to float64.
func InterfaceToFloat64(i interface{}, emptyStringAsZero ...bool) (float64, error) {
switch v := i.(type) {
case bool:
return BoolToFloat64(v), nil
case nil:
return 0, nil
case int:
return IntToFloat64(v), nil
case int8:
return Int8ToFloat64(v), nil
case int16:
return Int16ToFloat64(v), nil
case int32:
return Int32ToFloat64(v), nil
case int64:
return Int64ToFloat64(v), nil
case uint:
return UintToFloat64(v), nil
case uint8:
return Uint8ToFloat64(v), nil
case uint16:
return Uint16ToFloat64(v), nil
case uint32:
return Uint32ToFloat64(v), nil
case uint64:
return Uint64ToFloat64(v), nil
case uintptr:
return UintToFloat64(uint(v)), nil
case string:
return StringToFloat64(v, emptyStringAsZero...)
default:
r := IndirectValue(reflect.ValueOf(i))
switch r.Kind() {
case reflect.Bool:
return BoolToFloat64(r.Bool()), nil
case reflect.Invalid:
return 0, nil
case reflect.Float32, reflect.Float64:
return r.Float(), nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return Int64ToFloat64(r.Int()), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return Uint64ToFloat64(r.Uint()), nil
case reflect.String:
return StringToFloat64(r.String(), emptyStringAsZero...)
}
if isEmptyAsZero(emptyStringAsZero) {
return BoolToFloat64(!isZero(r)), nil
}
return 0, fmt.Errorf("cannot convert %#v of type %T to float64", i, i)
}
}
// InterfaceToFloat64Ptr converts interface to *float64.
func InterfaceToFloat64Ptr(i interface{}, emptyAsFalse ...bool) (*float64, error) {
r, err := InterfaceToFloat64(i, emptyAsFalse...)
return &r, err
}
// InterfaceToInt converts interface to int.
func InterfaceToInt(i interface{}, emptyStringAsZero ...bool) (int, error) {
switch v := i.(type) {
case bool:
return BoolToInt(v), nil
case nil:
return 0, nil
case int:
return v, nil
case int8:
return Int8ToInt(v), nil
case int16:
return Int16ToInt(v), nil
case int32:
return Int32ToInt(v), nil
case int64:
return Int64ToInt(v)
case uint:
return UintToInt(v)
case uint8:
return Uint8ToInt(v), nil
case uint16:
return Uint16ToInt(v), nil
case uint32:
return Uint32ToInt(v), nil
case uint64:
return Uint64ToInt(v), nil
case uintptr:
return UintToInt(uint(v))
case string:
return StringToInt(v, emptyStringAsZero...)
default:
r := IndirectValue(reflect.ValueOf(i))
switch r.Kind() {
case reflect.Bool:
return BoolToInt(r.Bool()), nil
case reflect.Invalid:
return 0, nil
case reflect.Float32, reflect.Float64:
return Float64ToInt(r.Float())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return Int64ToInt(r.Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return Uint64ToInt(r.Uint()), nil
case reflect.String:
return StringToInt(r.String(), emptyStringAsZero...)
}
if isEmptyAsZero(emptyStringAsZero) {
return BoolToInt(!isZero(r)), nil
}
return 0, fmt.Errorf("cannot convert %#v of type %T to int", i, i)
}
}
// InterfaceToIntPtr converts interface to *float64.
func InterfaceToIntPtr(i interface{}, emptyAsFalse ...bool) (*int, error) {
r, err := InterfaceToInt(i, emptyAsFalse...)
return &r, err
}
// InterfaceToInt8 converts interface to int8.
func InterfaceToInt8(i interface{}, emptyStringAsZero ...bool) (int8, error) {
switch v := i.(type) {
case bool:
return BoolToInt8(v), nil
case nil:
return 0, nil
case int:
return IntToInt8(v)
case int8:
return v, nil
case int16:
return Int16ToInt8(v)
case int32:
return Int32ToInt8(v)
case int64:
return Int64ToInt8(v)
case uint:
return UintToInt8(v)
case uint8:
return Uint8ToInt8(v)
case uint16:
return Uint16ToInt8(v)
case uint32:
return Uint32ToInt8(v)
case uint64:
return Uint64ToInt8(v)
case uintptr:
return UintToInt8(uint(v))
case string:
return StringToInt8(v, emptyStringAsZero...)
default:
r := IndirectValue(reflect.ValueOf(i))
switch r.Kind() {
case reflect.Bool:
return BoolToInt8(r.Bool()), nil
case reflect.Invalid:
return 0, nil
case reflect.Float32, reflect.Float64:
return Float64ToInt8(r.Float())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return Int64ToInt8(r.Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return Uint64ToInt8(r.Uint())
case reflect.String:
return StringToInt8(r.String(), emptyStringAsZero...)
}
if isEmptyAsZero(emptyStringAsZero) {
return BoolToInt8(!isZero(r)), nil
}
return 0, fmt.Errorf("cannot convert %#v of type %T to int8", i, i)
}
}
// InterfaceToInt8Ptr converts interface to *int8.
func InterfaceToInt8Ptr(i interface{}, emptyAsFalse ...bool) (*int8, error) {
r, err := InterfaceToInt8(i, emptyAsFalse...)
return &r, err
}
// InterfaceToInt16 converts interface to int16.
func InterfaceToInt16(i interface{}, emptyStringAsZero ...bool) (int16, error) {
switch v := i.(type) {
case bool:
return BoolToInt16(v), nil
case nil:
return 0, nil
case int:
return IntToInt16(v)
case int8:
return Int8ToInt16(v), nil
case int16:
return v, nil
case int32:
return Int32ToInt16(v)
case int64:
return Int64ToInt16(v)
case uint:
return UintToInt16(v)
case uint8:
return Uint8ToInt16(v), nil
case uint16:
return Uint16ToInt16(v)
case uint32:
return Uint32ToInt16(v)
case uint64:
return Uint64ToInt16(v)
case uintptr:
return UintToInt16(uint(v))
case string:
return StringToInt16(v, emptyStringAsZero...)
default:
r := IndirectValue(reflect.ValueOf(i))
switch r.Kind() {
case reflect.Bool:
return BoolToInt16(r.Bool()), nil
case reflect.Invalid:
return 0, nil
case reflect.Float32, reflect.Float64:
return Float64ToInt16(r.Float())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return Int64ToInt16(r.Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return Uint64ToInt16(r.Uint())
case reflect.String:
return StringToInt16(r.String(), emptyStringAsZero...)
}
if isEmptyAsZero(emptyStringAsZero) {
return BoolToInt16(!isZero(r)), nil
}
return 0, fmt.Errorf("cannot convert %#v of type %T to int16", i, i)
}
}
// InterfaceToInt16Ptr converts interface to *int16.
func InterfaceToInt16Ptr(i interface{}, emptyAsFalse ...bool) (*int16, error) {
r, err := InterfaceToInt16(i, emptyAsFalse...)
return &r, err
}
// InterfaceToInt32 converts interface to int32.
func InterfaceToInt32(i interface{}, emptyStringAsZero ...bool) (int32, error) {
switch v := i.(type) {
case bool:
return BoolToInt32(v), nil
case nil:
return 0, nil
case int:
return IntToInt32(v)
case int8:
return Int8ToInt32(v), nil
case int16:
return Int16ToInt32(v), nil
case int32:
return v, nil
case int64:
return Int64ToInt32(v)
case uint:
return UintToInt32(v)
case uint8:
return Uint8ToInt32(v), nil
case uint16:
return Uint16ToInt32(v), nil
case uint32:
return Uint32ToInt32(v)
case uint64:
return Uint64ToInt32(v)
case uintptr:
return UintToInt32(uint(v))
case string:
return StringToInt32(v, emptyStringAsZero...)
default:
r := IndirectValue(reflect.ValueOf(i))
switch r.Kind() {
case reflect.Bool:
return BoolToInt32(r.Bool()), nil
case reflect.Invalid:
return 0, nil
case reflect.Float32, reflect.Float64:
return Float64ToInt32(r.Float())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return Int64ToInt32(r.Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return Uint64ToInt32(r.Uint())
case reflect.String:
return StringToInt32(r.String(), emptyStringAsZero...)
}
if isEmptyAsZero(emptyStringAsZero) {
return BoolToInt32(!isZero(r)), nil
}
return 0, fmt.Errorf("cannot convert %#v of type %T to int32", i, i)
}
}
// InterfaceToInt32Ptr converts interface to *int32.
func InterfaceToInt32Ptr(i interface{}, emptyAsFalse ...bool) (*int32, error) {
r, err := InterfaceToInt32(i, emptyAsFalse...)
return &r, err
}
// InterfaceToInt64 converts interface to int64.
func InterfaceToInt64(i interface{}, emptyStringAsZero ...bool) (int64, error) {
switch v := i.(type) {
case bool:
return BoolToInt64(v), nil
case nil:
return 0, nil
case int:
return IntToInt64(v), nil
case int8:
return Int8ToInt64(v), nil
case int16:
return Int16ToInt64(v), nil
case int32:
return Int32ToInt64(v), nil
case int64:
return v, nil
case uint:
return UintToInt64(v)
case uint8:
return Uint8ToInt64(v), nil
case uint16:
return Uint16ToInt64(v), nil
case uint32:
return Uint32ToInt64(v), nil
case uint64:
return Uint64ToInt64(v)
case uintptr:
return UintToInt64(uint(v))
case string:
return StringToInt64(v, emptyStringAsZero...)
default:
r := IndirectValue(reflect.ValueOf(i))
switch r.Kind() {
case reflect.Bool:
return BoolToInt64(r.Bool()), nil
case reflect.Invalid:
return 0, nil
case reflect.Float32, reflect.Float64:
return Float64ToInt64(r.Float())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return r.Int(), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return Uint64ToInt64(r.Uint())
case reflect.String:
return StringToInt64(r.String(), emptyStringAsZero...)
}
if isEmptyAsZero(emptyStringAsZero) {
return BoolToInt64(!isZero(r)), nil
}
return 0, fmt.Errorf("cannot convert %#v of type %T to int64", i, i)
}
}
// InterfaceToInt64Ptr converts interface to *int64.
func InterfaceToInt64Ptr(i interface{}, emptyAsFalse ...bool) (*int64, error) {
r, err := InterfaceToInt64(i, emptyAsFalse...)
return &r, err
}
// InterfaceToUint converts interface to uint.
func InterfaceToUint(i interface{}, emptyStringAsZero ...bool) (uint, error) {
switch v := i.(type) {
case bool:
return BoolToUint(v), nil
case nil:
return 0, nil
case int:
return IntToUint(v)
case int8:
return Int8ToUint(v)
case int16:
return Int16ToUint(v)
case int32:
return Int32ToUint(v)
case int64:
return Int64ToUint(v)
case uint:
return v, nil
case uint8:
return Uint8ToUint(v), nil
case uint16:
return Uint16ToUint(v), nil
case uint32:
return Uint32ToUint(v), nil
case uint64:
return Uint64ToUint(v)
case uintptr:
return uint(v), nil
case string:
return StringToUint(v, emptyStringAsZero...)
default:
r := IndirectValue(reflect.ValueOf(i))
switch r.Kind() {
case reflect.Bool:
return BoolToUint(r.Bool()), nil
case reflect.Invalid:
return 0, nil
case reflect.Float32, reflect.Float64:
return Float64ToUint(r.Float())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return Int64ToUint(r.Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return Uint64ToUint(r.Uint())
case reflect.String:
return StringToUint(r.String(), emptyStringAsZero...)
}
if isEmptyAsZero(emptyStringAsZero) {
return BoolToUint(!isZero(r)), nil
}
return 0, fmt.Errorf("cannot convert %#v of type %T to uint", i, i)
}
}
// InterfaceToUintPtr converts interface to *uint.
func InterfaceToUintPtr(i interface{}, emptyAsFalse ...bool) (*uint, error) {
r, err := InterfaceToUint(i, emptyAsFalse...)
return &r, err
}
// InterfaceToUint8 converts interface to uint8.
func InterfaceToUint8(i interface{}, emptyStringAsZero ...bool) (uint8, error) {
switch v := i.(type) {
case bool:
return BoolToUint8(v), nil
case nil:
return 0, nil
case int:
return IntToUint8(v)
case int8:
return Int8ToUint8(v)
case int16:
return Int16ToUint8(v)
case int32:
return Int32ToUint8(v)
case int64:
return Int64ToUint8(v)
case uint:
return UintToUint8(v)
case uint8:
return v, nil
case uint16:
return Uint16ToUint8(v)
case uint32:
return Uint32ToUint8(v)
case uint64:
return Uint64ToUint8(v)
case uintptr:
return UintToUint8(uint(v))
case string:
return StringToUint8(v, emptyStringAsZero...)
default:
r := IndirectValue(reflect.ValueOf(i))
switch r.Kind() {
case reflect.Bool:
return BoolToUint8(r.Bool()), nil
case reflect.Invalid:
return 0, nil
case reflect.Float32, reflect.Float64:
return Float64ToUint8(r.Float())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return Int64ToUint8(r.Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return Uint64ToUint8(r.Uint())
case reflect.String:
return StringToUint8(r.String(), emptyStringAsZero...)
}
if isEmptyAsZero(emptyStringAsZero) {
return BoolToUint8(!isZero(r)), nil
}
return 0, fmt.Errorf("cannot convert %#v of type %T to uint8", i, i)
}
}
// InterfaceToUint8Ptr converts interface to *uint8.
func InterfaceToUint8Ptr(i interface{}, emptyAsFalse ...bool) (*uint8, error) {
r, err := InterfaceToUint8(i, emptyAsFalse...)
return &r, err
}
// InterfaceToUint16 converts interface to uint16.
func InterfaceToUint16(i interface{}, emptyStringAsZero ...bool) (uint16, error) {
switch v := i.(type) {
case bool:
return BoolToUint16(v), nil
case nil:
return 0, nil
case int:
return IntToUint16(v)
case int8:
return Int8ToUint16(v)
case int16:
return Int16ToUint16(v)
case int32:
return Int32ToUint16(v)
case int64:
return Int64ToUint16(v)
case uint:
return UintToUint16(v)
case uint8:
return Uint8ToUint16(v), nil
case uint16:
return v, nil
case uint32:
return Uint32ToUint16(v)
case uint64:
return Uint64ToUint16(v)
case uintptr:
return UintToUint16(uint(v))
case string:
return StringToUint16(v, emptyStringAsZero...)
default:
r := IndirectValue(reflect.ValueOf(i))
switch r.Kind() {
case reflect.Bool:
return BoolToUint16(r.Bool()), nil
case reflect.Invalid:
return 0, nil
case reflect.Float32, reflect.Float64:
return Float64ToUint16(r.Float())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return Int64ToUint16(r.Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return Uint64ToUint16(r.Uint())
case reflect.String:
return StringToUint16(r.String(), emptyStringAsZero...)
}
if isEmptyAsZero(emptyStringAsZero) {
return BoolToUint16(!isZero(r)), nil
}
return 0, fmt.Errorf("cannot convert %#v of type %T to uint16", i, i)
}
}
// InterfaceToUint16Ptr converts interface to *uint16.
func InterfaceToUint16Ptr(i interface{}, emptyAsFalse ...bool) (*uint16, error) {
r, err := InterfaceToUint16(i, emptyAsFalse...)
return &r, err
}
// InterfaceToUint32 converts interface to uint32.
func InterfaceToUint32(i interface{}, emptyStringAsZero ...bool) (uint32, error) {
switch v := i.(type) {
case bool:
return BoolToUint32(v), nil
case nil:
return 0, nil
case int:
return IntToUint32(v)
case int8:
return Int8ToUint32(v)
case int16:
return Int16ToUint32(v)
case int32:
return Int32ToUint32(v)
case int64:
return Int64ToUint32(v)
case uint:
return UintToUint32(v)
case uint8:
return Uint8ToUint32(v), nil
case uint16:
return Uint16ToUint32(v), nil
case uint32:
return v, nil
case uint64:
return Uint64ToUint32(v)
case uintptr:
return UintToUint32(uint(v))
case string:
return StringToUint32(v, emptyStringAsZero...)
default:
r := IndirectValue(reflect.ValueOf(i))
switch r.Kind() {
case reflect.Bool:
return BoolToUint32(r.Bool()), nil
case reflect.Invalid:
return 0, nil
case reflect.Float32, reflect.Float64:
return Float64ToUint32(r.Float())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return Int64ToUint32(r.Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return Uint64ToUint32(r.Uint())
case reflect.String:
return StringToUint32(r.String(), emptyStringAsZero...)
}
if isEmptyAsZero(emptyStringAsZero) {
return BoolToUint32(!isZero(r)), nil
}
return 0, fmt.Errorf("cannot convert %#v of type %T to uint32", i, i)
}
}
// InterfaceToUint32Ptr converts interface to *uint32.
func InterfaceToUint32Ptr(i interface{}, emptyAsFalse ...bool) (*uint32, error) {
r, err := InterfaceToUint32(i, emptyAsFalse...)
return &r, err
}
// InterfaceToUint64 converts interface to uint64.
func InterfaceToUint64(i interface{}, emptyStringAsZero ...bool) (uint64, error) {
switch v := i.(type) {
case bool:
return BoolToUint64(v), nil
case nil:
return 0, nil
case int:
return IntToUint64(v)
case int8:
return Int8ToUint64(v)
case int16:
return Int16ToUint64(v)
case int32:
return Int32ToUint64(v)
case int64:
return Int64ToUint64(v)
case uint:
return UintToUint64(v), nil
case uint8:
return Uint8ToUint64(v), nil
case uint16:
return Uint16ToUint64(v), nil
case uint32:
return Uint32ToUint64(v), nil
case uint64:
return v, nil
case uintptr:
return UintToUint64(uint(v)), nil
case string:
return StringToUint64(v, emptyStringAsZero...)
default:
r := IndirectValue(reflect.ValueOf(i))
switch r.Kind() {
case reflect.Bool:
return BoolToUint64(r.Bool()), nil
case reflect.Invalid:
return 0, nil
case reflect.Float32, reflect.Float64:
return Float64ToUint64(r.Float())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return Int64ToUint64(r.Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return r.Uint(), nil
case reflect.String:
return StringToUint64(r.String(), emptyStringAsZero...)
}
if isEmptyAsZero(emptyStringAsZero) {
return BoolToUint64(!isZero(r)), nil
}
return 0, fmt.Errorf("cannot convert %#v of type %T to uint64", i, i)
}
}
// InterfaceToUint64Ptr converts interface to *uint64.
func InterfaceToUint64Ptr(i interface{}, emptyAsFalse ...bool) (*uint64, error) {
r, err := InterfaceToUint64(i, emptyAsFalse...)
return &r, err
}