@@ -449,28 +449,6 @@ func durationSliceOrDefault(key string, defaultVal []time.Duration, separator st
449
449
return val
450
450
}
451
451
452
- // uint64OrDefault retrieves the unt64 value of the environment variable named
453
- // by the key.
454
- // If variable not set or value is empty - defaultVal will be returned.
455
- func uint64OrDefault (key string , defaultVal uint64 ) uint64 {
456
- env := stringOrDefault (key , "" )
457
- if env == "" {
458
- return defaultVal
459
- }
460
-
461
- const (
462
- base = 10
463
- bitsize = 64
464
- )
465
-
466
- val , err := strconv .ParseUint (env , base , bitsize )
467
- if err != nil {
468
- return defaultVal
469
- }
470
-
471
- return val
472
- }
473
-
474
452
// uint64SliceOrDefault retrieves the uint64 slice value of the environment variable named
475
453
// by the key and separated by sep.
476
454
// If variable not set or value is empty - defaultVal will be returned.
@@ -499,48 +477,60 @@ func uint64SliceOrDefault(key string, defaultVal []uint64, sep string) []uint64
499
477
return val
500
478
}
501
479
502
- // uint8OrDefault retrieves the unt8 value of the environment variable named
503
- // by the key.
504
- // If variable not set or value is empty - defaultVal will be returned.
505
- func uint8OrDefault (key string , defaultVal uint8 ) uint8 {
480
+ func uintOrDefaultGen [T uint | uint8 | uint16 | uint32 | uint64 | uintptr ](key string , defaultVal T ) T {
506
481
env := stringOrDefault (key , "" )
507
482
if env == "" {
508
483
return defaultVal
509
484
}
510
485
511
486
const (
512
- base = 10
513
- bitsize = 8
487
+ base = 10
514
488
)
515
489
516
- val , err := strconv .ParseUint (env , base , bitsize )
517
- if err != nil {
518
- return defaultVal
519
- }
520
-
521
- return uint8 (val )
522
- }
523
-
524
- // uintOrDefault retrieves the unt value of the environment variable named
525
- // by the key.
526
- // If variable not set or value is empty - defaultVal will be returned.
527
- func uintOrDefault (key string , defaultVal uint ) uint {
528
- env := stringOrDefault (key , "" )
529
- if env == "" {
530
- return defaultVal
531
- }
490
+ var (
491
+ bitsize int
492
+ castFn func (val uint64 ) T
493
+ )
532
494
533
- const (
534
- base = 10
495
+ switch any (defaultVal ).(type ) {
496
+ case uint :
497
+ bitsize = 0
498
+ castFn = func (val uint64 ) T {
499
+ return any (uint (val )).(T )
500
+ }
501
+ case uint8 :
502
+ bitsize = 8
503
+ castFn = func (val uint64 ) T {
504
+ return any (uint8 (val )).(T )
505
+ }
506
+ case uint16 :
507
+ bitsize = 16
508
+ castFn = func (val uint64 ) T {
509
+ return any (uint16 (val )).(T )
510
+ }
511
+ case uint32 :
535
512
bitsize = 32
536
- )
513
+ castFn = func (val uint64 ) T {
514
+ return any (uint32 (val )).(T )
515
+ }
516
+ case uint64 :
517
+ bitsize = 64
518
+ castFn = func (val uint64 ) T {
519
+ return any (val ).(T )
520
+ }
521
+ case uintptr :
522
+ bitsize = 0
523
+ castFn = func (val uint64 ) T {
524
+ return any (uintptr (val )).(T )
525
+ }
526
+ }
537
527
538
528
val , err := strconv .ParseUint (env , base , bitsize )
539
529
if err != nil {
540
530
return defaultVal
541
531
}
542
532
543
- return uint (val )
533
+ return castFn (val )
544
534
}
545
535
546
536
// uintSliceOrDefault retrieves the uint slice value of the environment variable named
@@ -655,50 +645,6 @@ func uint32SliceOrDefault(key string, defaultVal []uint32, sep string) []uint32
655
645
return val
656
646
}
657
647
658
- // uint16OrDefault retrieves the unt16 value of the environment variable named
659
- // by the key.
660
- // If variable not set or value is empty - defaultVal will be returned.
661
- func uint16OrDefault (key string , defaultVal uint16 ) uint16 {
662
- env := stringOrDefault (key , "" )
663
- if env == "" {
664
- return defaultVal
665
- }
666
-
667
- const (
668
- base = 10
669
- bitsize = 16
670
- )
671
-
672
- val , err := strconv .ParseUint (env , base , bitsize )
673
- if err != nil {
674
- return defaultVal
675
- }
676
-
677
- return uint16 (val )
678
- }
679
-
680
- // uint32OrDefault retrieves the unt32 value of the environment variable named
681
- // by the key.
682
- // If variable not set or value is empty - defaultVal will be returned.
683
- func uint32OrDefault (key string , defaultVal uint32 ) uint32 {
684
- env := stringOrDefault (key , "" )
685
- if env == "" {
686
- return defaultVal
687
- }
688
-
689
- const (
690
- base = 10
691
- bitsize = 32
692
- )
693
-
694
- val , err := strconv .ParseUint (env , base , bitsize )
695
- if err != nil {
696
- return defaultVal
697
- }
698
-
699
- return uint32 (val )
700
- }
701
-
702
648
// urlOrDefault retrieves the url.URL value of the environment variable named
703
649
// by the key represented by layout.
704
650
// If variable not set or value is empty - defaultVal will be returned.
@@ -779,28 +725,6 @@ func ipSliceOrDefault(key string, defaultVal []net.IP, sep string) []net.IP {
779
725
return val
780
726
}
781
727
782
- // uintptrOrDefault retrieves the uintptr value of the environment variable named
783
- // by the key.
784
- // If variable not set or value is empty - defaultVal will be returned.
785
- func uintptrOrDefault (key string , defaultVal uintptr ) uintptr {
786
- env := stringOrDefault (key , "" )
787
- if env == "" {
788
- return defaultVal
789
- }
790
-
791
- const (
792
- base = 10
793
- bitsize = 0
794
- )
795
-
796
- val , err := strconv .ParseUint (env , base , bitsize )
797
- if err != nil {
798
- return defaultVal
799
- }
800
-
801
- return uintptr (val )
802
- }
803
-
804
728
// uintptrSliceOrDefault retrieves the uintptr slice value of the environment variable named
805
729
// by the key and separated by sep.
806
730
// If variable not set or value is empty - defaultVal will be returned.
0 commit comments