Skip to content

Commit

Permalink
refactor: Implement complex generic parser (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
obalunenko authored Jul 26, 2023
1 parent f05063b commit 31d704f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 32 deletions.
4 changes: 2 additions & 2 deletions internal/iface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
48 changes: 20 additions & 28 deletions internal/parsers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions internal/parsers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
Expand Down Expand Up @@ -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)
})
}
Expand Down

0 comments on commit 31d704f

Please sign in to comment.