@@ -449,28 +449,6 @@ func durationSliceOrDefault(key string, defaultVal []time.Duration, separator st
449449 return val
450450}
451451
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-
474452// uint64SliceOrDefault retrieves the uint64 slice value of the environment variable named
475453// by the key and separated by sep.
476454// 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
499477 return val
500478}
501479
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 {
506481 env := stringOrDefault (key , "" )
507482 if env == "" {
508483 return defaultVal
509484 }
510485
511486 const (
512- base = 10
513- bitsize = 8
487+ base = 10
514488 )
515489
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+ )
532494
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 :
535512 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+ }
537527
538528 val , err := strconv .ParseUint (env , base , bitsize )
539529 if err != nil {
540530 return defaultVal
541531 }
542532
543- return uint (val )
533+ return castFn (val )
544534}
545535
546536// uintSliceOrDefault retrieves the uint slice value of the environment variable named
@@ -655,50 +645,6 @@ func uint32SliceOrDefault(key string, defaultVal []uint32, sep string) []uint32
655645 return val
656646}
657647
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-
702648// urlOrDefault retrieves the url.URL value of the environment variable named
703649// by the key represented by layout.
704650// 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 {
779725 return val
780726}
781727
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-
804728// uintptrSliceOrDefault retrieves the uintptr slice value of the environment variable named
805729// by the key and separated by sep.
806730// If variable not set or value is empty - defaultVal will be returned.
0 commit comments