Skip to content

Commit

Permalink
refactor: Implement uint generic parser (#107)
Browse files Browse the repository at this point in the history
* ci: Disable linter depguard cause of false-positive results

* refactor: Implement uint generic parser
  • Loading branch information
obalunenko authored Jul 26, 2023
1 parent 9fcb283 commit f05063b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 135 deletions.
9 changes: 0 additions & 9 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -56,7 +48,6 @@ linters:
disable-all: true
enable:
- bodyclose
- depguard
- dogsled
- dupl
- errcheck
Expand Down
12 changes: 6 additions & 6 deletions internal/iface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -416,15 +416,15 @@ 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
}

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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
152 changes: 38 additions & 114 deletions internal/parsers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
12 changes: 6 additions & 6 deletions internal/parsers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
Expand Down Expand Up @@ -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)
})
}
Expand Down Expand Up @@ -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)
})
}
Expand Down Expand Up @@ -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)
})
}
Expand Down Expand Up @@ -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)
})
}
Expand Down Expand Up @@ -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)
})
}
Expand Down

0 comments on commit f05063b

Please sign in to comment.