diff --git a/.golangci.yml b/.golangci.yml index 70bd447f..ec1d0574 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,12 +1,4 @@ linters-settings: - depguard: - list-type: blacklist - packages: - # logging is allowed only by logutils.Log, logrus - # is allowed to use only in logutils package - - github.com/sirupsen/logrus - packages-with-error-message: - - github.com/sirupsen/logrus: "logging is allowed only by github.com/obalunenko/logger" dupl: threshold: 100 funlen: @@ -56,7 +48,6 @@ linters: disable-all: true enable: - bodyclose - - depguard - dogsled - dupl - errcheck diff --git a/internal/iface.go b/internal/iface.go index a1139ce0..6fb30e4b 100644 --- a/internal/iface.go +++ b/internal/iface.go @@ -398,7 +398,7 @@ func (d durationParser) ParseEnv(key string, defaltVal any, _ Parameters) any { type uint64Parser uint64 func (d uint64Parser) ParseEnv(key string, defaltVal any, _ Parameters) any { - val := uint64OrDefault(key, defaltVal.(uint64)) + val := uintOrDefaultGen(key, defaltVal.(uint64)) return val } @@ -416,7 +416,7 @@ func (i uint64SliceParser) ParseEnv(key string, defaltVal any, options Parameter type uint8Parser uint func (d uint8Parser) ParseEnv(key string, defaltVal any, _ Parameters) any { - val := uint8OrDefault(key, defaltVal.(uint8)) + val := uintOrDefaultGen(key, defaltVal.(uint8)) return val } @@ -424,7 +424,7 @@ func (d uint8Parser) ParseEnv(key string, defaltVal any, _ Parameters) any { type uintParser uint func (d uintParser) ParseEnv(key string, defaltVal any, _ Parameters) any { - val := uintOrDefault(key, defaltVal.(uint)) + val := uintOrDefaultGen(key, defaltVal.(uint)) return val } @@ -473,7 +473,7 @@ func (i uint16SliceParser) ParseEnv(key string, defaltVal any, options Parameter type uint16Parser uint func (d uint16Parser) ParseEnv(key string, defaltVal any, _ Parameters) any { - val := uint16OrDefault(key, defaltVal.(uint16)) + val := uintOrDefaultGen(key, defaltVal.(uint16)) return val } @@ -482,7 +482,7 @@ func (d uint16Parser) ParseEnv(key string, defaltVal any, _ Parameters) any { type uint32Parser uint func (d uint32Parser) ParseEnv(key string, defaltVal any, _ Parameters) any { - val := uint32OrDefault(key, defaltVal.(uint32)) + val := uintOrDefaultGen(key, defaltVal.(uint32)) return val } @@ -542,7 +542,7 @@ func (b boolSliceParser) ParseEnv(key string, defaltVal any, options Parameters) type uintptrParser uintptr func (d uintptrParser) ParseEnv(key string, defaltVal any, _ Parameters) any { - val := uintptrOrDefault(key, defaltVal.(uintptr)) + val := uintOrDefaultGen(key, defaltVal.(uintptr)) return val } diff --git a/internal/parsers.go b/internal/parsers.go index eed4566d..dad48b55 100644 --- a/internal/parsers.go +++ b/internal/parsers.go @@ -449,28 +449,6 @@ func durationSliceOrDefault(key string, defaultVal []time.Duration, separator st return val } -// uint64OrDefault retrieves the unt64 value of the environment variable named -// by the key. -// If variable not set or value is empty - defaultVal will be returned. -func uint64OrDefault(key string, defaultVal uint64) uint64 { - env := stringOrDefault(key, "") - if env == "" { - return defaultVal - } - - const ( - base = 10 - bitsize = 64 - ) - - val, err := strconv.ParseUint(env, base, bitsize) - if err != nil { - return defaultVal - } - - return val -} - // uint64SliceOrDefault retrieves the uint64 slice value of the environment variable named // by the key and separated by sep. // 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 return val } -// uint8OrDefault retrieves the unt8 value of the environment variable named -// by the key. -// If variable not set or value is empty - defaultVal will be returned. -func uint8OrDefault(key string, defaultVal uint8) uint8 { +func uintOrDefaultGen[T uint | uint8 | uint16 | uint32 | uint64 | uintptr](key string, defaultVal T) T { env := stringOrDefault(key, "") if env == "" { return defaultVal } const ( - base = 10 - bitsize = 8 + base = 10 ) - val, err := strconv.ParseUint(env, base, bitsize) - if err != nil { - return defaultVal - } - - return uint8(val) -} - -// uintOrDefault retrieves the unt value of the environment variable named -// by the key. -// If variable not set or value is empty - defaultVal will be returned. -func uintOrDefault(key string, defaultVal uint) uint { - env := stringOrDefault(key, "") - if env == "" { - return defaultVal - } + var ( + bitsize int + castFn func(val uint64) T + ) - const ( - base = 10 + switch any(defaultVal).(type) { + case uint: + bitsize = 0 + castFn = func(val uint64) T { + return any(uint(val)).(T) + } + case uint8: + bitsize = 8 + castFn = func(val uint64) T { + return any(uint8(val)).(T) + } + case uint16: + bitsize = 16 + castFn = func(val uint64) T { + return any(uint16(val)).(T) + } + case uint32: bitsize = 32 - ) + castFn = func(val uint64) T { + return any(uint32(val)).(T) + } + case uint64: + bitsize = 64 + castFn = func(val uint64) T { + return any(val).(T) + } + case uintptr: + bitsize = 0 + castFn = func(val uint64) T { + return any(uintptr(val)).(T) + } + } val, err := strconv.ParseUint(env, base, bitsize) if err != nil { return defaultVal } - return uint(val) + return castFn(val) } // uintSliceOrDefault retrieves the uint slice value of the environment variable named @@ -655,50 +645,6 @@ func uint32SliceOrDefault(key string, defaultVal []uint32, sep string) []uint32 return val } -// uint16OrDefault retrieves the unt16 value of the environment variable named -// by the key. -// If variable not set or value is empty - defaultVal will be returned. -func uint16OrDefault(key string, defaultVal uint16) uint16 { - env := stringOrDefault(key, "") - if env == "" { - return defaultVal - } - - const ( - base = 10 - bitsize = 16 - ) - - val, err := strconv.ParseUint(env, base, bitsize) - if err != nil { - return defaultVal - } - - return uint16(val) -} - -// uint32OrDefault retrieves the unt32 value of the environment variable named -// by the key. -// If variable not set or value is empty - defaultVal will be returned. -func uint32OrDefault(key string, defaultVal uint32) uint32 { - env := stringOrDefault(key, "") - if env == "" { - return defaultVal - } - - const ( - base = 10 - bitsize = 32 - ) - - val, err := strconv.ParseUint(env, base, bitsize) - if err != nil { - return defaultVal - } - - return uint32(val) -} - // urlOrDefault retrieves the url.URL value of the environment variable named // by the key represented by layout. // 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 { return val } -// uintptrOrDefault retrieves the uintptr value of the environment variable named -// by the key. -// If variable not set or value is empty - defaultVal will be returned. -func uintptrOrDefault(key string, defaultVal uintptr) uintptr { - env := stringOrDefault(key, "") - if env == "" { - return defaultVal - } - - const ( - base = 10 - bitsize = 0 - ) - - val, err := strconv.ParseUint(env, base, bitsize) - if err != nil { - return defaultVal - } - - return uintptr(val) -} - // uintptrSliceOrDefault retrieves the uintptr slice value of the environment variable named // by the key and separated by sep. // If variable not set or value is empty - defaultVal will be returned. diff --git a/internal/parsers_test.go b/internal/parsers_test.go index 867f432a..34eee092 100644 --- a/internal/parsers_test.go +++ b/internal/parsers_test.go @@ -2798,7 +2798,7 @@ func Test_uint64OrDefault(t *testing.T) { t.Run(tt.name, func(t *testing.T) { tt.precond.maybeSetEnv(t, tt.args.key) - got := uint64OrDefault(tt.args.key, tt.args.defaultVal) + got := uintOrDefaultGen(tt.args.key, tt.args.defaultVal) assert.Equal(t, tt.expected.val, got) }) } @@ -3003,7 +3003,7 @@ func Test_uint8OrDefault(t *testing.T) { t.Run(tt.name, func(t *testing.T) { tt.precond.maybeSetEnv(t, tt.args.key) - got := uint8OrDefault(tt.args.key, tt.args.defaultVal) + got := uintOrDefaultGen(tt.args.key, tt.args.defaultVal) assert.Equal(t, tt.expected.val, got) }) } @@ -3095,7 +3095,7 @@ func Test_uintOrDefault(t *testing.T) { t.Run(tt.name, func(t *testing.T) { tt.precond.maybeSetEnv(t, tt.args.key) - got := uintOrDefault(tt.args.key, tt.args.defaultVal) + got := uintOrDefaultGen(tt.args.key, tt.args.defaultVal) assert.Equal(t, tt.expected.val, got) }) } @@ -3187,7 +3187,7 @@ func Test_uint16OrDefault(t *testing.T) { t.Run(tt.name, func(t *testing.T) { tt.precond.maybeSetEnv(t, tt.args.key) - got := uint16OrDefault(tt.args.key, tt.args.defaultVal) + got := uintOrDefaultGen(tt.args.key, tt.args.defaultVal) assert.Equal(t, tt.expected.val, got) }) } @@ -3279,7 +3279,7 @@ func Test_uint32OrDefault(t *testing.T) { t.Run(tt.name, func(t *testing.T) { tt.precond.maybeSetEnv(t, tt.args.key) - got := uint32OrDefault(tt.args.key, tt.args.defaultVal) + got := uintOrDefaultGen(tt.args.key, tt.args.defaultVal) assert.Equal(t, tt.expected.val, got) }) } @@ -3766,7 +3766,7 @@ func Test_uintptrOrDefault(t *testing.T) { t.Run(tt.name, func(t *testing.T) { tt.precond.maybeSetEnv(t, tt.args.key) - got := uintptrOrDefault(tt.args.key, tt.args.defaultVal) + got := uintOrDefaultGen(tt.args.key, tt.args.defaultVal) assert.Equal(t, tt.expected.val, got) }) }