From 9fcb2835b6a8a5ee6bf66ad9bb04c9f65901a20b Mon Sep 17 00:00:00 2001 From: Oleg Balunenko Date: Wed, 26 Jul 2023 15:12:45 +0400 Subject: [PATCH] refactor: Use generic parser for float (#106) --- internal/iface.go | 4 +-- internal/parsers.go | 75 ++++++++++++++++++---------------------- internal/parsers_test.go | 4 +-- 3 files changed, 37 insertions(+), 46 deletions(-) diff --git a/internal/iface.go b/internal/iface.go index 7287fb0e..a1139ce0 100644 --- a/internal/iface.go +++ b/internal/iface.go @@ -335,7 +335,7 @@ 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 } @@ -343,7 +343,7 @@ func (f float32Parser) ParseEnv(key string, defaltVal any, _ Parameters) any { 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 } diff --git a/internal/parsers.go b/internal/parsers.go index c0b74a4f..eed4566d 100644 --- a/internal/parsers.go +++ b/internal/parsers.go @@ -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 == "" { @@ -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. diff --git a/internal/parsers_test.go b/internal/parsers_test.go index 1a528470..867f432a 100644 --- a/internal/parsers_test.go +++ b/internal/parsers_test.go @@ -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) }) } @@ -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) }) }