Skip to content

Commit

Permalink
refactor: Implement slices generic parsers (#109)
Browse files Browse the repository at this point in the history
* refactor: Implement int/float slice generic parsers

* refactor: Implement complex slice generic parser

* refactor: Implement uint slice generic parser

* style: Fix linter warnings

* style: Fix linter warnings

* refactor: Reduce cognitive complexity

* refactor: Use generic slices parsers

* refactor: Use generic uint slice parcer

* refactor: Use golang.org/x/exp/constraints

* refactor: Simplify code

* refactor: Simplify code
  • Loading branch information
obalunenko authored Jul 26, 2023
1 parent 31d704f commit 505b070
Show file tree
Hide file tree
Showing 11 changed files with 272 additions and 481 deletions.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ module github.com/obalunenko/getenv

go 1.20

require github.com/stretchr/testify v1.8.4
require (
github.com/stretchr/testify v1.8.4
golang.org/x/exp v0.0.0-20230725093048-515e97ebf090
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/exp v0.0.0-20230725093048-515e97ebf090 h1:Di6/M8l0O2lCLc6VVRWhgCiApHV8MnQurBnFSHsQtNY=
golang.org/x/exp v0.0.0-20230725093048-515e97ebf090/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
36 changes: 25 additions & 11 deletions internal/constraint.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"net"
"net/url"
"time"

"golang.org/x/exp/constraints"
)

type (
// EnvParsable is a constraint for types that can be parsed from environment variable.
EnvParsable interface {
String | Int | Uint | Float | Time | Bool | URL | IP | Complex
String | Int | IntSlice | Uint | UintSlice | Float | FloatSlice | Time | Bool | URL | IP | Complex | ComplexSlice
}

// String is a constraint for string and slice of strings.
Expand All @@ -18,19 +20,28 @@ type (
}

// Int is a constraint for integer and slice of integers.
Int interface {
int | []int | int8 | []int8 | int16 | []int16 | int32 | []int32 | int64 | []int64
Int = constraints.Signed

// IntSlice is a constraint for slice of integers.
IntSlice interface {
[]int | []int8 | []int16 | []int32 | []int64
}

// UintSlice is a constraint for slice of unsigned integers.
UintSlice interface {
[]uint | []uint8 | []uint16 | []uint32 | []uint64 | []uintptr
}

// Uint is a constraint for unsigned integer and slice of unsigned integers.
Uint interface {
uint | []uint | uint8 | []uint8 | uint16 | []uint16 | uint32 | []uint32 | uint64 | []uint64 | uintptr | []uintptr
Uint = constraints.Unsigned

// FloatSlice is a constraint for slice of floats.
FloatSlice interface {
[]float32 | []float64
}

// Float is a constraint for float and slice of floats.
Float interface {
float32 | []float32 | float64 | []float64
}
Float = constraints.Float

// Time is a constraint for time.Time and slice of time.Time.
Time interface {
Expand All @@ -52,8 +63,11 @@ type (
net.IP | []net.IP
}

// Complex is a constraint for complex and slice of complex.
Complex interface {
complex64 | []complex64 | complex128 | []complex128
// ComplexSlice is a constraint for slice of complex.
ComplexSlice interface {
[]complex64 | []complex128
}

// Complex is a constraint for complex and slice of complex.
Complex = constraints.Complex
)
10 changes: 10 additions & 0 deletions internal/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package internal

import "errors"

var (
// ErrNotSet is an error that is returned when the environment variable is not set.
ErrNotSet = errors.New("not set")
// ErrInvalidValue is an error that is returned when the environment variable is not valid.
ErrInvalidValue = errors.New("invalid value")
)
30 changes: 15 additions & 15 deletions internal/iface.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ type intSliceParser []int
func (i intSliceParser) ParseEnv(key string, defaltVal any, options Parameters) any {
sep := options.Separator

val := intSliceOrDefault(key, defaltVal.([]int), sep)
val := intSliceOrDefaultGen(key, defaltVal.([]int), sep)

return val
}
Expand All @@ -245,7 +245,7 @@ type float32SliceParser []float32
func (i float32SliceParser) ParseEnv(key string, defaltVal any, options Parameters) any {
sep := options.Separator

val := float32SliceOrDefault(key, defaltVal.([]float32), sep)
val := floatSliceOrDefaultGen(key, defaltVal.([]float32), sep)

return val
}
Expand All @@ -255,7 +255,7 @@ type float64SliceParser []float64
func (i float64SliceParser) ParseEnv(key string, defaltVal any, options Parameters) any {
sep := options.Separator

val := float64SliceOrDefault(key, defaltVal.([]float64), sep)
val := floatSliceOrDefaultGen(key, defaltVal.([]float64), sep)

return val
}
Expand Down Expand Up @@ -297,7 +297,7 @@ type int8SliceParser []int8
func (i int8SliceParser) ParseEnv(key string, defaltVal any, options Parameters) any {
sep := options.Separator

val := int8SliceOrDefault(key, defaltVal.([]int8), sep)
val := intSliceOrDefaultGen(key, defaltVal.([]int8), sep)

return val
}
Expand All @@ -307,7 +307,7 @@ type int16SliceParser []int16
func (i int16SliceParser) ParseEnv(key string, defaltVal any, options Parameters) any {
sep := options.Separator

val := int16SliceOrDefault(key, defaltVal.([]int16), sep)
val := intSliceOrDefaultGen(key, defaltVal.([]int16), sep)

return val
}
Expand All @@ -317,7 +317,7 @@ type int32SliceParser []int32
func (i int32SliceParser) ParseEnv(key string, defaltVal any, options Parameters) any {
sep := options.Separator

val := int32SliceOrDefault(key, defaltVal.([]int32), sep)
val := intSliceOrDefaultGen(key, defaltVal.([]int32), sep)

return val
}
Expand All @@ -327,7 +327,7 @@ type int64SliceParser []int64
func (i int64SliceParser) ParseEnv(key string, defaltVal any, options Parameters) any {
sep := options.Separator

val := int64SliceOrDefault(key, defaltVal.([]int64), sep)
val := intSliceOrDefaultGen(key, defaltVal.([]int64), sep)

return val
}
Expand Down Expand Up @@ -408,7 +408,7 @@ type uint64SliceParser []uint64
func (i uint64SliceParser) ParseEnv(key string, defaltVal any, options Parameters) any {
sep := options.Separator

val := uint64SliceOrDefault(key, defaltVal.([]uint64), sep)
val := uintSliceOrDefaultGen(key, defaltVal.([]uint64), sep)

return val
}
Expand All @@ -434,7 +434,7 @@ type uintSliceParser []uint
func (i uintSliceParser) ParseEnv(key string, defaltVal any, options Parameters) any {
sep := options.Separator

val := uintSliceOrDefault(key, defaltVal.([]uint), sep)
val := uintSliceOrDefaultGen(key, defaltVal.([]uint), sep)

return val
}
Expand All @@ -444,7 +444,7 @@ type uint8SliceParser []uint8
func (i uint8SliceParser) ParseEnv(key string, defaltVal any, options Parameters) any {
sep := options.Separator

val := uint8SliceOrDefault(key, defaltVal.([]uint8), sep)
val := uintSliceOrDefaultGen(key, defaltVal.([]uint8), sep)

return val
}
Expand All @@ -454,7 +454,7 @@ type uint32SliceParser []uint32
func (i uint32SliceParser) ParseEnv(key string, defaltVal any, options Parameters) any {
sep := options.Separator

val := uint32SliceOrDefault(key, defaltVal.([]uint32), sep)
val := uintSliceOrDefaultGen(key, defaltVal.([]uint32), sep)

return val
}
Expand All @@ -464,7 +464,7 @@ type uint16SliceParser []uint16
func (i uint16SliceParser) ParseEnv(key string, defaltVal any, options Parameters) any {
sep := options.Separator

val := uint16SliceOrDefault(key, defaltVal.([]uint16), sep)
val := uintSliceOrDefaultGen(key, defaltVal.([]uint16), sep)

return val
}
Expand Down Expand Up @@ -553,7 +553,7 @@ type uintptrSliceParser []uintptr
func (i uintptrSliceParser) ParseEnv(key string, defaltVal any, options Parameters) any {
sep := options.Separator

val := uintptrSliceOrDefault(key, defaltVal.([]uintptr), sep)
val := uintSliceOrDefaultGen(key, defaltVal.([]uintptr), sep)

return val
}
Expand All @@ -573,7 +573,7 @@ type complex64SliceParser []complex64
func (i complex64SliceParser) ParseEnv(key string, defaltVal any, options Parameters) any {
sep := options.Separator

val := complex64SliceOrDefault(key, defaltVal.([]complex64), sep)
val := complexSliceOrDefaultGen(key, defaltVal.([]complex64), sep)

return val
}
Expand All @@ -593,7 +593,7 @@ type complex128SliceParser []complex128
func (i complex128SliceParser) ParseEnv(key string, defaltVal any, options Parameters) any {
sep := options.Separator

val := complex128SliceOrDefault(key, defaltVal.([]complex128), sep)
val := complexSliceOrDefaultGen(key, defaltVal.([]complex128), sep)

return val
}
Loading

0 comments on commit 505b070

Please sign in to comment.