diff --git a/internal/iface.go b/internal/iface.go index 6fb30e4b..0845ea47 100644 --- a/internal/iface.go +++ b/internal/iface.go @@ -562,7 +562,7 @@ func (i uintptrSliceParser) ParseEnv(key string, defaltVal any, options Paramete type complex64Parser complex64 func (d complex64Parser) ParseEnv(key string, defaltVal any, _ Parameters) any { - val := complex64OrDefault(key, defaltVal.(complex64)) + val := complexOrDefaultGen(key, defaltVal.(complex64)) return val } @@ -582,7 +582,7 @@ func (i complex64SliceParser) ParseEnv(key string, defaltVal any, options Parame type complex128Parser complex128 func (d complex128Parser) ParseEnv(key string, defaltVal any, _ Parameters) any { - val := complex128OrDefault(key, defaltVal.(complex128)) + val := complexOrDefaultGen(key, defaltVal.(complex128)) return val } diff --git a/internal/parsers.go b/internal/parsers.go index dad48b55..c310c209 100644 --- a/internal/parsers.go +++ b/internal/parsers.go @@ -753,25 +753,38 @@ func uintptrSliceOrDefault(key string, defaultVal []uintptr, sep string) []uintp return val } -// complex64OrDefault retrieves the complex64 value of the environment variable named -// by the key. -// If variable not set or value is empty - defaultVal will be returned. -func complex64OrDefault(key string, defaultVal complex64) complex64 { +func complexOrDefaultGen[T complex64 | complex128](key string, defaultVal T) T { env := stringOrDefault(key, "") if env == "" { return defaultVal } - const ( - bitsize = 64 + var ( + bitsize int + castFn func(val complex128) T ) + switch any(defaultVal).(type) { + case complex64: + bitsize = 64 + + castFn = func(val complex128) T { + return any(complex64(val)).(T) + } + case complex128: + bitsize = 128 + + castFn = func(val complex128) T { + return any(val).(T) + } + } + val, err := strconv.ParseComplex(env, bitsize) if err != nil { return defaultVal } - return complex64(val) + return castFn(val) } // complex64SliceOrDefault retrieves the complex64 slice value of the environment variable named @@ -801,27 +814,6 @@ func complex64SliceOrDefault(key string, defaultVal []complex64, sep string) []c return val } -// complex128OrDefault retrieves the complex128 value of the environment variable named -// by the key. -// If variable not set or value is empty - defaultVal will be returned. -func complex128OrDefault(key string, defaultVal complex128) complex128 { - env := stringOrDefault(key, "") - if env == "" { - return defaultVal - } - - const ( - bitsize = 128 - ) - - val, err := strconv.ParseComplex(env, bitsize) - if err != nil { - return defaultVal - } - - return val -} - // complex128SliceOrDefault retrieves the complex128 slice value of the environment variable named // by the key and separated by sep. // If the variable is not set or the value is empty - defaultVal will be returned. diff --git a/internal/parsers_test.go b/internal/parsers_test.go index 34eee092..500a429d 100644 --- a/internal/parsers_test.go +++ b/internal/parsers_test.go @@ -3958,7 +3958,7 @@ func Test_complex64OrDefault(t *testing.T) { t.Run(tt.name, func(t *testing.T) { tt.precond.maybeSetEnv(t, tt.args.key) - got := complex64OrDefault(tt.args.key, tt.args.defaultVal) + got := complexOrDefaultGen(tt.args.key, tt.args.defaultVal) assert.Equal(t, tt.expected.val, got) }) } @@ -4150,7 +4150,7 @@ func Test_complex128OrDefault(t *testing.T) { t.Run(tt.name, func(t *testing.T) { tt.precond.maybeSetEnv(t, tt.args.key) - got := complex128OrDefault(tt.args.key, tt.args.defaultVal) + got := complexOrDefaultGen(tt.args.key, tt.args.defaultVal) assert.Equal(t, tt.expected.val, got) }) }