Skip to content

Commit

Permalink
refactor: Use generic parser for float (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
obalunenko authored Jul 26, 2023
1 parent e41878c commit 9fcb283
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 46 deletions.
4 changes: 2 additions & 2 deletions internal/iface.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,15 +335,15 @@ func (i int64SliceParser) ParseEnv(key string, defaltVal any, options Parameters
type float32Parser float32

func (f float32Parser) ParseEnv(key string, defaltVal any, _ Parameters) any {
val := float32OrDefault(key, defaltVal.(float32))
val := floatOrDefaultGen(key, defaltVal.(float32))

return val
}

type float64Parser float64

func (f float64Parser) ParseEnv(key string, defaltVal any, _ Parameters) any {
val := float64OrDefault(key, defaltVal.(float64))
val := floatOrDefaultGen(key, defaltVal.(float64))

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

func floatOrDefaultGen[T float32 | float64](key string, defaultVal T) T {
env := stringOrDefault(key, "")
if env == "" {
return defaultVal
}

const (
bitsize = 64
)

var (
castFn func(val float64) T
)

switch any(defaultVal).(type) {
case float32:
castFn = func(val float64) T {
return any(float32(val)).(T)
}
case float64:
castFn = func(val float64) T {
return any(val).(T)
}
}

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

return castFn(val)
}

func intOrDefaultGen[T int | int8 | int16 | int32 | int64](key string, defaultVal T) T {
env := stringOrDefault(key, "")
if env == "" {
Expand Down Expand Up @@ -416,48 +449,6 @@ func durationSliceOrDefault(key string, defaultVal []time.Duration, separator st
return 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.
func float32OrDefault(key string, defaultVal float32) float32 {
env := stringOrDefault(key, "")
if env == "" {
return defaultVal
}

const (
bitsize = 32
)

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

return float32(val)
}

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

const (
bitsize = 64
)

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

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.
Expand Down
4 changes: 2 additions & 2 deletions internal/parsers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ func Test_float32OrDefault(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
tt.precond.maybeSetEnv(t, tt.args.key)

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

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

0 comments on commit 9fcb283

Please sign in to comment.