Skip to content

Commit

Permalink
refactor: Use generic parser for integers (#105)
Browse files Browse the repository at this point in the history
* refactor: Implement generic parser for integers

* chore: Remove redundant default
  • Loading branch information
obalunenko authored Jul 26, 2023
1 parent 6fc973c commit e41878c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 98 deletions.
10 changes: 5 additions & 5 deletions internal/iface.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func (s stringSliceParser) ParseEnv(key string, defaltVal any, options Parameter
type intParser int

func (i intParser) ParseEnv(key string, defaltVal any, _ Parameters) any {
val := intOrDefault(key, defaltVal.(int))
val := intOrDefaultGen(key, defaltVal.(int))

return val
}
Expand Down Expand Up @@ -263,31 +263,31 @@ func (i float64SliceParser) ParseEnv(key string, defaltVal any, options Paramete
type int64Parser int64

func (i int64Parser) ParseEnv(key string, defaltVal any, _ Parameters) any {
val := int64OrDefault(key, defaltVal.(int64))
val := intOrDefaultGen(key, defaltVal.(int64))

return val
}

type int8Parser int8

func (i int8Parser) ParseEnv(key string, defaltVal any, _ Parameters) any {
val := int8OrDefault(key, defaltVal.(int8))
val := intOrDefaultGen(key, defaltVal.(int8))

return val
}

type int16Parser int16

func (i int16Parser) ParseEnv(key string, defaltVal any, _ Parameters) any {
val := int16OrDefault(key, defaltVal.(int16))
val := intOrDefaultGen(key, defaltVal.(int16))

return val
}

type int32Parser int32

func (i int32Parser) ParseEnv(key string, defaltVal any, _ Parameters) any {
val := int32OrDefault(key, defaltVal.(int32))
val := intOrDefaultGen(key, defaltVal.(int32))

return val
}
Expand Down
141 changes: 52 additions & 89 deletions internal/parsers.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,57 @@ func int8SliceOrDefault(key string, defaultVal []int8, sep string) []int8 {
return val
}

func intOrDefaultGen[T int | int8 | int16 | int32 | int64](key string, defaultVal T) T {
env := stringOrDefault(key, "")
if env == "" {
return defaultVal
}

const (
base = 10
)

var (
bitsize int
castFn func(val int64) T
)

switch any(defaultVal).(type) {
case int:
bitsize = 0
castFn = func(val int64) T {
return any(int(val)).(T)
}
case int8:
bitsize = 8
castFn = func(val int64) T {
return any(int8(val)).(T)
}
case int16:
bitsize = 16
castFn = func(val int64) T {
return any(int16(val)).(T)
}
case int32:
bitsize = 32
castFn = func(val int64) T {
return any(int32(val)).(T)
}
case int64:
bitsize = 64
castFn = func(val int64) T {
return any(val).(T)
}
}

val, err := strconv.ParseInt(env, base, bitsize)
if err != nil {
return defaultVal
}

return castFn(val)
}

// int32SliceOrDefault retrieves the int32 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 @@ -365,94 +416,6 @@ func durationSliceOrDefault(key string, defaultVal []time.Duration, separator st
return val
}

// int64OrDefault retrieves the int64 value of the environment variable named
// by the key.
// If variable not set or value is empty - defaultVal will be returned.
func int64OrDefault(key string, defaultVal int64) int64 {
env := stringOrDefault(key, "")
if env == "" {
return defaultVal
}

const (
base = 10
bitsize = 64
)

val, err := strconv.ParseInt(env, base, bitsize)
if err != nil {
return defaultVal
}

return val
}

// int8OrDefault retrieves the int8 value of the environment variable named
// by the key.
// If variable not set or value is empty - defaultVal will be returned.
func int8OrDefault(key string, defaultVal int8) int8 {
env := stringOrDefault(key, "")
if env == "" {
return defaultVal
}

const (
base = 10
bitsize = 8
)

val, err := strconv.ParseInt(env, base, bitsize)
if err != nil {
return defaultVal
}

return int8(val)
}

// int16OrDefault retrieves the int16 value of the environment variable named
// by the key.
// If variable not set or value is empty - defaultVal will be returned.
func int16OrDefault(key string, defaultVal int16) int16 {
env := stringOrDefault(key, "")
if env == "" {
return defaultVal
}

const (
base = 10
bitsize = 16
)

val, err := strconv.ParseInt(env, base, bitsize)
if err != nil {
return defaultVal
}

return int16(val)
}

// int32OrDefault retrieves the int32 value of the environment variable named
// by the key.
// If variable not set or value is empty - defaultVal will be returned.
func int32OrDefault(key string, defaultVal int32) int32 {
env := stringOrDefault(key, "")
if env == "" {
return defaultVal
}

const (
base = 10
bitsize = 32
)

val, err := strconv.ParseInt(env, base, bitsize)
if err != nil {
return defaultVal
}

return int32(val)
}

// float32OrDefault retrieves the float32 value of the environment variable named
// by the key.
// If variable not set or value is empty - defaultVal will be returned.
Expand Down Expand Up @@ -946,7 +909,7 @@ func complex128OrDefault(key string, defaultVal complex128) complex128 {

// complex128SliceOrDefault retrieves the complex128 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.
// If the variable is not set or the value is empty - defaultVal will be returned.
func complex128SliceOrDefault(key string, defaultVal []complex128, sep string) []complex128 {
valraw := stringSliceOrDefault(key, nil, sep)
if valraw == nil {
Expand Down
8 changes: 4 additions & 4 deletions internal/parsers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func Test_int64OrDefault(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
tt.precond.maybeSetEnv(t, tt.args.key)

got := int64OrDefault(tt.args.key, tt.args.defaultVal)
got := intOrDefaultGen(tt.args.key, tt.args.defaultVal)
assert.Equal(t, tt.expected.val, got)
})
}
Expand Down Expand Up @@ -309,7 +309,7 @@ func Test_int8OrDefault(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
tt.precond.maybeSetEnv(t, tt.args.key)

got := int8OrDefault(tt.args.key, tt.args.defaultVal)
got := intOrDefaultGen(tt.args.key, tt.args.defaultVal)
assert.Equal(t, tt.expected.val, got)
})
}
Expand Down Expand Up @@ -385,7 +385,7 @@ func Test_int16OrDefault(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
tt.precond.maybeSetEnv(t, tt.args.key)

got := int16OrDefault(tt.args.key, tt.args.defaultVal)
got := intOrDefaultGen(tt.args.key, tt.args.defaultVal)
assert.Equal(t, tt.expected.val, got)
})
}
Expand Down Expand Up @@ -461,7 +461,7 @@ func Test_int32OrDefault(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
tt.precond.maybeSetEnv(t, tt.args.key)

got := int32OrDefault(tt.args.key, tt.args.defaultVal)
got := intOrDefaultGen(tt.args.key, tt.args.defaultVal)
assert.Equal(t, tt.expected.val, got)
})
}
Expand Down

0 comments on commit e41878c

Please sign in to comment.