Passed
Pull Request — master (#83)
by ertugrul
02:10
created

converter.go   A

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 31
dl 0
loc 114
rs 10
c 0
b 0
f 0
1
package gotil
2
3
import (
4
	"fmt"
5
	"math"
6
	"strconv"
7
8
	"github.com/gotilty/gotil/internal/errs"
9
)
10
11
//ToFloat32 returns float32(0) with an error if the parameter is unsupported type.
12
//Just works with all primitive types.
13
func ToFloat32[T any](val T) (float32, error) {
14
	var result float32
15
	convertable, def, _ := getConvertable[T, float32](val)
16
	result, _ = convertable.convert(val, def)
17
	return result, nil
18
}
19
20
//ToFloat64 returns float64(0) with an error if the parameter is unsupported type.
21
//Just works with all primitive types.
22
func ToFloat64[T any](val T) (float64, error) {
23
	var result float64
24
	convertable, def, _ := getConvertable[T, float64](val)
25
	result, _ = convertable.convert(val, def)
26
	return result, nil
27
}
28
29
//ToInt returns int(0) with an error if the parameter is unsupported type.
30
//Just works with all primitive types.
31
func ToInt[T any](val T) (int, error) {
32
	var result int
33
	convertable, def, _ := getConvertable[T, int](val)
34
	result, _ = convertable.convert(val, def)
35
	return result, nil
36
}
37
38
//ToInt8 returns int8(0) with an error if the parameter is unsupported type.
39
//Just works with all primitive types.
40
func ToInt8[T any](val T) (int8, error) {
41
	var result int8
42
	convertable, def, _ := getConvertable[T, int8](val)
43
	result, _ = convertable.convert(val, def)
44
	return result, nil
45
}
46
47
//ToInt16 returns int16(0) with an error if the parameter is unsupported type.
48
//Just works with all primitive types.
49
func ToInt16[T any](val T) (int16, error) {
50
	var result int16
51
	convertable, def, _ := getConvertable[T, int16](val)
52
	result, _ = convertable.convert(val, def)
53
	return result, nil
54
}
55
56
//ToInt32 returns int32(0) with an error if the parameter is unsupported type.
57
//Just works with all primitive types.
58
func ToInt32[T any](val T) (int32, error) {
59
	var result int32
60
	convertable, def, _ := getConvertable[T, int32](val)
61
	result, _ = convertable.convert(val, def)
62
	return result, nil
63
}
64
65
//ToInt64 returns int64(0) with an error if the parameter is unsupported type.
66
//Just works with all primitive types.
67
func ToInt64[T any](val T) (int64, error) {
68
	var result int64
69
	convertable, def, _ := getConvertable[T, int64](val)
70
	result, _ = convertable.convert(val, def)
71
	return result, nil
72
}
73
74
//ToUint returns uint(0) with an error if the parameter is unsupported type.
75
//Just works with all primitive types.
76
func ToUint[T any](val T) (uint, error) {
77
	var result uint
78
	convertable, def, _ := getConvertable[T, uint](val)
79
	result, _ = convertable.convert(val, def)
80
	return result, nil
81
}
82
83
//ToUint8 returns uint8(0) with an error if the parameter is unsupported type.
84
//Just works with all primitive types.
85
func ToUint8[T any](val T) (uint8, error) {
86
	var result uint8
87
	convertable, def, _ := getConvertable[T, uint8](val)
88
	result, _ = convertable.convert(val, def)
89
	return result, nil
90
}
91
92
//ToUint16 returns uint16(0) with an error if the parameter is unsupported type.
93
//Just works with all primitive types.
94
func ToUint16[T any](val T) (uint16, error) {
95
	var result uint16
96
	convertable, def, _ := getConvertable[T, uint16](val)
97
	result, _ = convertable.convert(val, def)
98
	return result, nil
99
}
100
101
//ToUint32 returns uint32(0) with an error if the parameter is unsupported type.
102
//Just works with all primitive types.
103
func ToUint32[T any](val T) (uint32, error) {
104
	var result uint32
105
	convertable, def, _ := getConvertable[T, uint32](val)
106
	result, _ = convertable.convert(val, def)
107
	return result, nil
108
}
109
110
//ToUint64 returns uint64(0) with an error if the parameter is unsupported type.
111
//Just works with all primitive types.
112
func ToUint64[T any](val T) (uint64, error) {
113
	var result uint64
114
	convertable, def, _ := getConvertable[T, uint64](val)
115
	result, _ = convertable.convert(val, def)
116
	return result, nil
117
}
118
119
// PRIVATE
120
121
type iconverter[T any, D any] interface {
122
	convert(a T, d D) (D, error)
123
}
124
125
type stringConverter[D any] struct {
126
	iconverter[string, D]
127
}
128
129
type intConverter[D any] struct {
130
	iconverter[int, D]
131
}
132
133
type int8Converter[D any] struct {
134
	iconverter[int8, D]
135
}
136
137
type int16Converter[D any] struct {
138
	iconverter[int16, D]
139
}
140
type int32Converter[D any] struct {
141
	iconverter[int32, D]
142
}
143
144
type int64Converter[D any] struct {
145
	iconverter[int64, D]
146
}
147
148
type uintConverter[D any] struct {
149
	iconverter[uint, D]
150
}
151
152
type uint8Converter[D any] struct {
153
	iconverter[uint8, D]
154
}
155
156
type uint16Converter[D any] struct {
157
	iconverter[uint16, D]
158
}
159
type uint32Converter[D any] struct {
160
	iconverter[uint32, D]
161
}
162
163
type uint64Converter[D any] struct {
164
	iconverter[uint64, D]
165
}
166
167
type float32Converter[D any] struct {
168
	iconverter[float32, D]
169
}
170
171
type float64Converter[D any] struct {
172
	iconverter[float64, D]
173
}
174
175
type boolConverter[D any] struct {
176
	iconverter[bool, D]
177
}
178
179
func sInt(s string) (int, error) {
180
	if s == "" {
181
		return 0, nil
182
	}
183
	if d, err := strconv.Atoi(s); err == nil {
184
		return d, nil
185
	} else {
186
		return 0, err
187
	}
188
}
189
190
func sUInt(s string) (uint64, error) {
191
	if s == "" {
192
		return 0, nil
193
	}
194
	if d, err := strconv.ParseUint(s, 10, 0); err == nil {
195
		return d, nil
196
	} else {
197
		return 0, err
198
	}
199
}
200
func sFloat(s string) (float64, error) {
201
	if s == "" {
202
		return 0, nil
203
	}
204
	if d, err := strconv.ParseFloat(s, 64); err == nil {
205
		return float64(d), nil
206
	} else {
207
		return 0, err
208
	}
209
}
210
211
func sBool(s string) (bool, error) {
212
	if s == "" {
213
		return false, nil
214
	}
215
	if d, err := strconv.ParseBool(s); err == nil {
216
		return d, nil
217
	} else {
218
		return false, err
219
	}
220
}
221
222
func (s stringConverter[D]) convert(a string, d D) (D, error) {
223
	var res interface{}
224
	var err error
225
	res = d
226
	switch any(d).(type) {
227
	case int, int8, int16, int32, int64:
228
		var maxInt64 int64 = math.MaxInt64
229
		if r, err := sInt(a); err == nil {
230
			if r > int(maxInt64) {
231
				return d, errs.CustomError("The entered number cannot be greater than max int64.")
232
			} else {
233
				switch any(d).(type) {
234
				case int:
235
					res = r
236
				case int8:
237
					res = int8(r)
238
				case int16:
239
					res = int16(r)
240
				case int32:
241
					res = int32(r)
242
				case int64:
243
					res = int64(r)
244
				}
245
				return res.(D), nil
246
			}
247
		} else {
248
			if err == nil {
249
				return res.(D), nil
250
			}
251
			return res.(D), err
252
		}
253
	case uint, uint8, uint16, uint32, uint64:
254
		var maxUInt64 uint64 = math.MaxUint64
255
		if r, err := sUInt(a); err == nil {
256
			if r > maxUInt64 {
257
				return d, errs.CustomError("The entered number cannot be greater than max float64.")
258
			} else {
259
				switch any(d).(type) {
260
				case uint:
261
					res = r
262
				case uint8:
263
					res = uint8(r)
264
				case uint16:
265
					res = uint16(r)
266
				case uint32:
267
					res = uint32(r)
268
				case uint64:
269
					res = uint64(r)
270
				}
271
				return res.(D), nil
272
			}
273
		} else {
274
			if err == nil {
275
				return res.(D), nil
276
			}
277
			return res.(D), err
278
		}
279
	case float32, float64:
280
		var maxFloat64 float64 = math.MaxFloat64
281
		if r, err := sFloat(a); err == nil {
282
			if r > maxFloat64 {
283
				return d, errs.CustomError("The entered number cannot be greater than max float64.")
284
			} else {
285
				switch any(d).(type) {
286
				case float32:
287
					res = float32(r)
288
				case float64:
289
					res = float64(r)
290
				}
291
				return res.(D), nil
292
			}
293
		} else {
294
			if err == nil {
295
				return res.(D), nil
296
			}
297
			return res.(D), err
298
		}
299
	case bool:
300
		if r, err := sBool(a); err == nil {
301
			res = r
302
		} else {
303
			return res.(D), nil
304
		}
305
		return res.(D), err
306
	case string:
307
		return d, nil
308
	}
309
	return d, nil
310
}
311
312
func (s intConverter[D]) convert(a int, d D) (D, error) {
313
	var res interface{}
314
315
	switch any(d).(type) {
316
	case int:
317
		res = a
318
	case int8:
319
		if a > math.MaxInt8 || a < math.MinInt8 {
320
			return d, errs.ErrOutOfRange
321
		}
322
		res = int8(a)
323
	case int16:
324
		if a > math.MaxInt16 || a < math.MinInt16 {
325
			return d, errs.ErrOutOfRange
326
		}
327
		res = int16(a)
328
	case int32:
329
		if a > math.MaxInt32 || a < math.MinInt32 {
330
			return d, errs.ErrOutOfRange
331
		}
332
		res = int32(a)
333
	case int64:
334
		res = int64(a)
335
	case uint:
336
		res = uint(a)
337
	case uint8:
338
		if a > math.MaxUint8 {
339
			return d, errs.ErrOutOfRange
340
		}
341
		res = uint8(a)
342
	case uint16:
343
		if a > math.MaxUint16 {
344
			return d, errs.ErrOutOfRange
345
		}
346
		res = uint16(a)
347
	case uint32:
348
		if a > math.MaxUint32 {
349
			return d, errs.ErrOutOfRange
350
		}
351
		res = uint32(a)
352
	case uint64:
353
		res = uint64(a)
354
	case float32:
355
		if float64(a) > math.MaxFloat32 {
356
			return d, errs.ErrOutOfRange
357
		}
358
		res = float32(a)
359
	case float64:
360
		res = float64(a)
361
	case string:
362
		res = fmt.Sprint(a)
363
	case bool:
364
		res = a == 1
365
	default:
366
		return d, nil
367
	}
368
	return res.(D), nil
369
370
}
371
372
func (s int8Converter[D]) convert(a int8, d D) (D, error) {
373
	var res interface{}
374
375
	switch any(d).(type) {
376
	case int:
377
		res = int(a)
378
	case int8:
379
		res = a
380
	case int16:
381
		res = int16(a)
382
	case int32:
383
		res = int32(a)
384
	case int64:
385
		res = int64(a)
386
	case uint:
387
		res = uint(a)
388
	case uint8:
389
		res = uint8(a)
390
	case uint16:
391
		res = uint16(a)
392
	case uint32:
393
		res = uint32(a)
394
	case uint64:
395
		res = uint64(a)
396
	case float32:
397
		res = float32(a)
398
	case float64:
399
		res = float64(a)
400
	case string:
401
		res = fmt.Sprint(a)
402
	case bool:
403
		res = a == 1
404
	default:
405
		return d, nil
406
	}
407
	return res.(D), nil
408
409
}
410
411
func (s int16Converter[D]) convert(a int16, d D) (D, error) {
412
	var res interface{}
413
414
	switch any(d).(type) {
415
	case int:
416
		res = int(a)
417
	case int8:
418
		if int(a) > math.MaxInt8 || int(a) < math.MinInt8 {
419
			return d, errs.ErrOutOfRange
420
		}
421
		res = int8(a)
422
	case int16:
423
		res = a
424
	case int32:
425
		res = int32(a)
426
	case int64:
427
		res = int64(a)
428
	case uint:
429
		res = uint(a)
430
	case uint8:
431
		if int(a) > math.MaxUint8 {
432
			return d, errs.ErrOutOfRange
433
		}
434
		res = uint8(a)
435
	case uint16:
436
		res = uint16(a)
437
	case uint32:
438
		res = uint32(a)
439
	case uint64:
440
		res = uint64(a)
441
	case float32:
442
		res = float32(a)
443
	case float64:
444
		res = float64(a)
445
	case string:
446
		res = fmt.Sprint(a)
447
	case bool:
448
		res = a == 1
449
	default:
450
		return d, nil
451
	}
452
	return res.(D), nil
453
}
454
455
func (s int32Converter[D]) convert(a int32, d D) (D, error) {
456
	var res interface{}
457
458
	switch any(d).(type) {
459
	case int:
460
		res = int(a)
461
	case int8:
462
		if int(a) > math.MaxInt8 || int(a) < math.MinInt8 {
463
			return d, errs.ErrOutOfRange
464
		}
465
		res = int8(a)
466
	case int16:
467
		if int(a) > math.MaxInt16 || int(a) < math.MinInt16 {
468
			return d, errs.ErrOutOfRange
469
		}
470
		res = int16(a)
471
	case int32:
472
		res = a
473
	case int64:
474
		res = int64(a)
475
	case uint:
476
		res = uint(a)
477
	case uint8:
478
		if int(a) > math.MaxUint8 {
479
			return d, errs.ErrOutOfRange
480
		}
481
		res = uint8(a)
482
	case uint16:
483
		if int(a) > math.MaxUint16 {
484
			return d, errs.ErrOutOfRange
485
		}
486
		res = uint16(a)
487
	case uint32:
488
		res = uint32(a)
489
	case uint64:
490
		res = uint64(a)
491
	case float32:
492
		res = float32(a)
493
	case float64:
494
		res = float64(a)
495
	case string:
496
		res = fmt.Sprint(a)
497
	case bool:
498
		res = a == 1
499
	default:
500
		return d, nil
501
	}
502
	return res.(D), nil
503
}
504
505
func (s int64Converter[D]) convert(a int64, d D) (D, error) {
506
	var res interface{}
507
508
	switch any(d).(type) {
509
	case int:
510
		res = int(a)
511
	case int8:
512
		if int(a) > math.MaxInt8 || int(a) < math.MinInt8 {
513
			return d, errs.ErrOutOfRange
514
		}
515
		res = int8(a)
516
	case int16:
517
		if int(a) > math.MaxInt16 || int(a) < math.MinInt16 {
518
			return d, errs.ErrOutOfRange
519
		}
520
		res = int16(a)
521
	case int32:
522
		if int(a) > math.MaxInt32 || int(a) < math.MinInt32 {
523
			return d, errs.ErrOutOfRange
524
		}
525
		res = a
526
	case int64:
527
		res = int64(a)
528
	case uint:
529
		res = uint(a)
530
	case uint8:
531
		if int(a) > math.MaxUint8 {
532
			return d, errs.ErrOutOfRange
533
		}
534
		res = uint8(a)
535
	case uint16:
536
		if int(a) > math.MaxUint16 {
537
			return d, errs.ErrOutOfRange
538
		}
539
		res = uint16(a)
540
	case uint32:
541
		if int(a) > math.MaxUint32 {
542
			return d, errs.ErrOutOfRange
543
		}
544
		res = uint32(a)
545
	case uint64:
546
		res = uint64(a)
547
	case float32:
548
		res = float32(a)
549
	case float64:
550
		res = float64(a)
551
	case string:
552
		res = fmt.Sprint(a)
553
	case bool:
554
		res = a == 1
555
	default:
556
		return d, nil
557
	}
558
	return res.(D), nil
559
}
560
561
func (s uintConverter[D]) convert(a uint, d D) (D, error) {
562
	var res interface{}
563
	switch any(d).(type) {
564
	case int:
565
		res = a
566
	case int8:
567
		if a > math.MaxInt8 {
568
			return d, errs.ErrOutOfRange
569
		}
570
		res = int8(a)
571
	case int16:
572
		if a > math.MaxInt16 {
573
			return d, errs.ErrOutOfRange
574
		}
575
		res = int16(a)
576
	case int32:
577
		if a > math.MaxInt32 {
578
			return d, errs.ErrOutOfRange
579
		}
580
		res = int32(a)
581
	case int64:
582
		res = int64(a)
583
	case uint:
584
		res = uint(a)
585
	case uint8:
586
		if a > math.MaxUint8 {
587
			return d, errs.ErrOutOfRange
588
		}
589
		res = uint8(a)
590
	case uint16:
591
		if a > math.MaxUint16 {
592
			return d, errs.ErrOutOfRange
593
		}
594
		res = uint16(a)
595
	case uint32:
596
		if a > math.MaxUint32 {
597
			return d, errs.ErrOutOfRange
598
		}
599
		res = uint32(a)
600
	case uint64:
601
		res = uint64(a)
602
	case float32:
603
		if float64(a) > math.MaxFloat32 {
604
			return d, errs.ErrOutOfRange
605
		}
606
		res = float32(a)
607
	case float64:
608
		res = float64(a)
609
	case string:
610
		res = fmt.Sprint(a)
611
	case bool:
612
		res = a == 1
613
	default:
614
		return d, nil
615
	}
616
	return res.(D), nil
617
618
}
619
620
func (s uint8Converter[D]) convert(a uint8, d D) (D, error) {
621
	var res interface{}
622
623
	switch any(d).(type) {
624
	case int:
625
		res = int(a)
626
	case int8:
627
		res = int8(a)
628
	case int16:
629
		res = int16(a)
630
	case int32:
631
		res = int32(a)
632
	case int64:
633
		res = int64(a)
634
	case uint:
635
		res = uint(a)
636
	case uint8:
637
		res = a
638
	case uint16:
639
		res = uint16(a)
640
	case uint32:
641
		res = uint32(a)
642
	case uint64:
643
		res = uint64(a)
644
	case float32:
645
		res = float32(a)
646
	case float64:
647
		res = float64(a)
648
	case string:
649
		res = fmt.Sprint(a)
650
	case bool:
651
		res = a == 1
652
	default:
653
		return d, nil
654
	}
655
	return res.(D), nil
656
657
}
658
659
func (s uint16Converter[D]) convert(a uint16, d D) (D, error) {
660
	var res interface{}
661
662
	switch any(d).(type) {
663
	case int:
664
		res = int(a)
665
	case int8:
666
		if a > math.MaxInt8 {
667
			return d, errs.ErrOutOfRange
668
		}
669
		res = int8(a)
670
	case int16:
671
		res = int16(a)
672
	case int32:
673
		res = int32(a)
674
	case int64:
675
		res = int64(a)
676
	case uint:
677
		res = uint(a)
678
	case uint8:
679
		if a > math.MaxUint8 {
680
			return d, errs.ErrOutOfRange
681
		}
682
		res = uint8(a)
683
	case uint16:
684
		res = uint16(a)
685
	case uint32:
686
		res = uint32(a)
687
	case uint64:
688
		res = uint64(a)
689
	case float32:
690
		res = float32(a)
691
	case float64:
692
		res = float64(a)
693
	case string:
694
		res = fmt.Sprint(a)
695
	case bool:
696
		res = a == 1
697
	default:
698
		return d, nil
699
	}
700
	return res.(D), nil
701
}
702
703
func (s uint32Converter[D]) convert(a uint32, d D) (D, error) {
704
	var res interface{}
705
706
	switch any(d).(type) {
707
	case int:
708
		res = int(a)
709
	case int8:
710
		if a > math.MaxInt8 {
711
			return d, errs.ErrOutOfRange
712
		}
713
		res = int8(a)
714
	case int16:
715
		if a > math.MaxInt16 {
716
			return d, errs.ErrOutOfRange
717
		}
718
		res = int16(a)
719
	case int32:
720
		res = uint32(a)
721
	case int64:
722
		res = int64(a)
723
	case uint:
724
		res = uint(a)
725
	case uint8:
726
		if a > math.MaxUint8 {
727
			return d, errs.ErrOutOfRange
728
		}
729
		res = uint8(a)
730
	case uint16:
731
		if a > math.MaxUint16 {
732
			return d, errs.ErrOutOfRange
733
		}
734
		res = uint16(a)
735
	case uint32:
736
		res = a
737
	case uint64:
738
		res = uint64(a)
739
	case float32:
740
		res = float32(a)
741
	case float64:
742
		res = float64(a)
743
	case string:
744
		res = fmt.Sprint(a)
745
	case bool:
746
		res = a == 1
747
	default:
748
		return d, nil
749
	}
750
	return res.(D), nil
751
}
752
753
func (s uint64Converter[D]) convert(a uint64, d D) (D, error) {
754
	var res interface{}
755
756
	switch any(d).(type) {
757
	case int:
758
		res = int(a)
759
	case int8:
760
		if a > math.MaxInt8 {
761
			return d, errs.ErrOutOfRange
762
		}
763
		res = int8(a)
764
	case int16:
765
		if a > math.MaxInt16 {
766
			return d, errs.ErrOutOfRange
767
		}
768
		res = int16(a)
769
	case int32:
770
		if a > math.MaxInt32 {
771
			return d, errs.ErrOutOfRange
772
		}
773
		res = a
774
	case int64:
775
		res = int64(a)
776
	case uint:
777
		res = uint(a)
778
	case uint8:
779
		if a > math.MaxUint8 {
780
			return d, errs.ErrOutOfRange
781
		}
782
		res = uint8(a)
783
	case uint16:
784
		if a > math.MaxUint16 {
785
			return d, errs.ErrOutOfRange
786
		}
787
		res = uint16(a)
788
	case uint32:
789
		if a > math.MaxUint32 {
790
			return d, errs.ErrOutOfRange
791
		}
792
		res = uint32(a)
793
	case uint64:
794
		res = uint64(a)
795
	case float32:
796
		res = float32(a)
797
	case float64:
798
		res = float64(a)
799
	case string:
800
		res = fmt.Sprint(a)
801
	case bool:
802
		res = a == 1
803
	default:
804
		return d, nil
805
	}
806
	return res.(D), nil
807
}
808
809
func (s float32Converter[D]) convert(a float32, d D) (D, error) {
810
	var res interface{}
811
	switch any(d).(type) {
812
	case int:
813
		res = int(a)
814
	case int8:
815
		if a > math.MaxInt8 || a < math.MinInt8 {
816
			return d, errs.ErrOutOfRange
817
		}
818
		res = int8(a)
819
	case int16:
820
		if a > math.MaxInt16 || a < math.MinInt16 {
821
			return d, errs.ErrOutOfRange
822
		}
823
		res = int16(a)
824
	case int32:
825
		if a > math.MaxInt32 || a < math.MinInt32 {
826
			return d, errs.ErrOutOfRange
827
		}
828
		res = int32(a)
829
	case int64:
830
		if a > math.MaxInt64 || a < math.MinInt64 {
831
			return d, errs.ErrOutOfRange
832
		}
833
		res = int64(a)
834
	case uint:
835
		res = uint(a)
836
	case uint8:
837
		if a > math.MaxUint8 {
838
			return d, errs.ErrOutOfRange
839
		}
840
		res = uint8(a)
841
	case uint16:
842
		if a > math.MaxUint16 {
843
			return d, errs.ErrOutOfRange
844
		}
845
		res = uint16(a)
846
	case uint32:
847
		if a > math.MaxUint32 {
848
			return d, errs.ErrOutOfRange
849
		}
850
		res = uint32(a)
851
	case uint64:
852
		if a > math.MaxUint64 {
853
			return d, errs.ErrOutOfRange
854
		}
855
		res = uint64(a)
856
	case float32:
857
		res = a
858
	case float64:
859
		res = float64(a)
860
	case string:
861
		res = fmt.Sprint(a)
862
	case bool:
863
		res = a == 1
864
	default:
865
		return d, nil
866
	}
867
	return res.(D), nil
868
869
}
870
871
func (s float64Converter[D]) convert(a float64, d D) (D, error) {
872
	var res interface{}
873
	switch any(d).(type) {
874
	case int:
875
		res = int(a)
876
	case int8:
877
		if a > math.MaxInt8 || a < math.MinInt8 {
878
			return d, errs.ErrOutOfRange
879
		}
880
		res = int8(a)
881
	case int16:
882
		if a > math.MaxInt16 || a < math.MinInt16 {
883
			return d, errs.ErrOutOfRange
884
		}
885
		res = int16(a)
886
	case int32:
887
		if a > math.MaxInt32 || a < math.MinInt32 {
888
			return d, errs.ErrOutOfRange
889
		}
890
		res = int32(a)
891
	case int64:
892
		if a > math.MaxInt64 || a < math.MinInt64 {
893
			return d, errs.ErrOutOfRange
894
		}
895
		res = int64(a)
896
	case uint:
897
		res = uint(a)
898
	case uint8:
899
		if a > math.MaxUint8 {
900
			return d, errs.ErrOutOfRange
901
		}
902
		res = uint8(a)
903
	case uint16:
904
		if a > math.MaxUint16 {
905
			return d, errs.ErrOutOfRange
906
		}
907
		res = uint16(a)
908
	case uint32:
909
		if a > math.MaxUint32 {
910
			return d, errs.ErrOutOfRange
911
		}
912
		res = uint32(a)
913
	case uint64:
914
		if a > math.MaxUint64 {
915
			return d, errs.ErrOutOfRange
916
		}
917
		res = uint64(a)
918
	case float32:
919
		if a > math.MaxFloat32 {
920
			return d, errs.ErrOutOfRange
921
		}
922
		res = a
923
	case float64:
924
		res = float64(a)
925
	case string:
926
		res = fmt.Sprint(a)
927
	case bool:
928
		res = a == 1
929
	default:
930
		return d, nil
931
	}
932
	return res.(D), nil
933
934
}
935
936
func (s boolConverter[D]) convert(a bool, d D) (D, error) {
937
	var res interface{}
938
	switch any(d).(type) {
939
	case int:
940
		if a {
941
			res = 1
942
		} else {
943
			res = 0
944
		}
945
	case int8:
946
		if a {
947
			res = int8(1)
948
		} else {
949
			res = int8(0)
950
		}
951
	case int16:
952
		if a {
953
			res = int16(1)
954
		} else {
955
			res = int16(0)
956
		}
957
	case int32:
958
		if a {
959
			res = int32(1)
960
		} else {
961
			res = int32(0)
962
		}
963
	case int64:
964
		if a {
965
			res = int64(1)
966
		} else {
967
			res = int64(0)
968
		}
969
	case uint:
970
		if a {
971
			res = uint(1)
972
		} else {
973
			res = uint(0)
974
		}
975
	case uint8:
976
		if a {
977
			res = uint8(1)
978
		} else {
979
			res = uint8(0)
980
		}
981
	case uint16:
982
		if a {
983
			res = uint16(1)
984
		} else {
985
			res = uint16(0)
986
		}
987
	case uint32:
988
		if a {
989
			res = uint32(1)
990
		} else {
991
			res = uint32(0)
992
		}
993
	case uint64:
994
		if a {
995
			res = uint64(1)
996
		} else {
997
			res = uint64(0)
998
		}
999
	case float32:
1000
		if a {
1001
			res = float32(1)
1002
		} else {
1003
			res = float32(0)
1004
		}
1005
	case float64:
1006
		if a {
1007
			res = float64(1)
1008
		} else {
1009
			res = float64(0)
1010
		}
1011
	case string:
1012
		res = fmt.Sprint(a)
1013
	case bool:
1014
		res = a
1015
	default:
1016
		return d, nil
1017
	}
1018
	return res.(D), nil
1019
1020
}
1021
1022
func getConvertable[T, D any](t T) (iconverter[T, D], D, error) {
1023
	var def D
1024
	var convertable interface{}
1025
	switch any(t).(type) {
1026
	case string:
1027
		convertable = stringConverter[D]{}
1028
		return convertable.(iconverter[T, D]), def, nil
1029
	case float32:
1030
		convertable = float32Converter[D]{}
1031
		return convertable.(iconverter[T, D]), def, nil
1032
	case float64:
1033
		convertable = float64Converter[D]{}
1034
		return convertable.(iconverter[T, D]), def, nil
1035
	case int:
1036
		convertable = intConverter[D]{}
1037
		return convertable.(iconverter[T, D]), def, nil
1038
	case int8:
1039
		convertable = int8Converter[D]{}
1040
		return convertable.(iconverter[T, D]), def, nil
1041
	case int16:
1042
		convertable = int16Converter[D]{}
1043
		return convertable.(iconverter[T, D]), def, nil
1044
	case int32:
1045
		convertable = int32Converter[D]{}
1046
		return convertable.(iconverter[T, D]), def, nil
1047
	case int64:
1048
		convertable = int64Converter[D]{}
1049
		return convertable.(iconverter[T, D]), def, nil
1050
	case uint:
1051
		convertable = uint8Converter[D]{}
1052
		return convertable.(iconverter[T, D]), def, nil
1053
	case uint8:
1054
		convertable = uint8Converter[D]{}
1055
		return convertable.(iconverter[T, D]), def, nil
1056
	case uint16:
1057
		convertable = uint16Converter[D]{}
1058
		return convertable.(iconverter[T, D]), def, nil
1059
	case uint32:
1060
		convertable = uint32Converter[D]{}
1061
		return convertable.(iconverter[T, D]), def, nil
1062
	case uint64:
1063
		convertable = uint64Converter[D]{}
1064
		return convertable.(iconverter[T, D]), def, nil
1065
	case bool:
1066
		convertable = boolConverter[D]{}
1067
		return convertable.(iconverter[T, D]), def, nil
1068
	default:
1069
		return nil, def, errs.ErrUnsupportedType
1070
	}
1071
1072
}
1073