8000 refactor: Implement slices generic parsers (#109) · obalunenko/getenv@505b070 · GitHub
[go: up one dir, main page]

Skip to content

Commit 505b070

Browse files
authored
refactor: Implement slices generic parsers (#109)
* 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
1 parent 31d704f commit 505b070

File tree

11 files changed

+272
-481
lines changed

11 files changed

+272
-481
lines changed

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ module github.com/obalunenko/getenv
22

33
go 1.20
44

5-
require github.com/stretchr/testify v1.8.4
5+
require (
6+
github.com/stretchr/testify v1.8.4
7+
golang.org/x/exp v0.0.0-20230725093048-515e97ebf090
8+
)
69

710
require (
811
github.com/davecgh/go-spew v1.1.1 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
44
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
55
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
66
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
7+
golang.org/x/exp v0.0.0-20230725093048-515e97ebf090 h1:Di6/M8l0O2lCLc6VVRWhgCiApHV8MnQurBnFSHsQtNY=
8+
golang.org/x/exp v0.0.0-20230725093048-515e97ebf090/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
79
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
810
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
911
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

internal/constraint.go

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import (
44
"net"
55
"net/url"
66
"time"
7+
8+
"golang.org/x/exp/constraints"
79
)
810

911
type (
1012
// EnvParsable is a constraint for types that can be parsed from environment variable.
1113
EnvParsable interface {
12-
String | Int | Uint | Float | Time | Bool | URL | IP | Complex
14+
String | Int | IntSlice | Uint | UintSlice | Float | FloatSlice | Time | Bool | URL | IP | Complex | ComplexSlice
1315
}
1416

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

2022
// Int is a constraint for integer and slice of integers.
21-
Int interface {
22-
int | []int | int8 | []int8 | int16 | []int16 | int32 | []int32 | int64 | []int64
23+
Int = constraints.Signed
24+
25+
// IntSlice is a constraint for slice of integers.
26+
IntSlice interface {
27+
[]int | []int8 | []int16 | []int32 | []int64
28+
}
29+
30+
// UintSlice is a constraint for slice of unsigned integers.
31+
UintSlice interface {
32+
[]uint | []uint8 | []uint16 | []uint32 | []uint64 | []uintptr
2333
}
2434

2535
// Uint is a constraint for unsigned integer and slice of unsigned integers.
26-
Uint interface {
27-
uint | []uint | uint8 | []uint8 | uint16 | []uint16 | uint32 | []uint32 | uint64 | []uint64 | uintptr | []uintptr
36+
Uint = constraints.Unsigned
37+
38+
// FloatSlice is a constraint for slice of floats.
39+
FloatSlice interface {
40+
[]float32 | []float64
2841
}
2942

3043
// Float is a constraint for float and slice of floats.
31-
Float interface {
32-
float32 | []float32 | float64 | []float64
33-
}
44+
Float = constraints.Float
3445

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

55-
// Complex is a constraint for complex and slice of complex.
56-
Complex interface {
57-
complex64 | []complex64 | complex128 | []complex128
66+
// ComplexSlice is a constraint for slice of complex.
67+
ComplexSlice interface {
68+
[]complex64 | []complex128
5869
}
70+
71+
// Complex is a constraint for complex and slice of complex.
72+
Complex = constraints.Complex
5973
)

internal/errors.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package internal
2+
3+
import "errors"
4+
5+
var (
6+
// ErrNotSet is an error that is returned when the environment variable is not set.
7+
ErrNotSet = errors.New("not set")
8+
// ErrInvalidValue is an error that is returned when the environment variable is not valid.
9+
ErrInvalidValue = errors.New("invalid value")
10+
)

internal/iface.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ type intSliceParser []int
235235
func (i intSliceParser) ParseEnv(key string, defaltVal any, options Parameters) any {
236236
sep := options.Separator
237237

238-
val := intSliceOrDefault(key, defaltVal.([]int), sep)
238+
val := intSliceOrDefaultGen(key, defaltVal.([]int), sep)
239239

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

248-
val := float32SliceOrDefault(key, defaltVal.([]float32), sep)
248+
val := floatSliceOrDefaultGen(key, defaltVal.([]float32), sep)
249249

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

258-
val := float64SliceOrDefault(key, defaltVal.([]float64), sep)
258+
val := floatSliceOrDefaultGen(key, defaltVal.([]float64), sep)
259259

260260
return val
261261
}
@@ -297,7 +297,7 @@ type int8SliceParser []int8
297297
func (i int8SliceParser) ParseEnv(key string, defaltVal any, options Parameters) any {
298298
sep := options.Separator
299299

300-
val := int8SliceOrDefault(key, defaltVal.([]int8), sep)
300+
val := intSliceOrDefaultGen(key, defaltVal.([]int8), sep)
301301

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

310-
val := int16SliceOrDefault(key, defaltVal.([]int16), sep)
310+
val := intSliceOrDefaultGen(key, defaltVal.([]int16), sep)
311311

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

320-
val := int32SliceOrDefault(key, defaltVal.([]int32), sep)
320+
val := intSliceOrDefaultGen(key, defaltVal.([]int32), sep)
321321

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

330-
val := int64SliceOrDefault(key, defaltVal.([]int64), sep)
330+
val := intSliceOrDefaultGen(key, defaltVal.([]int64), sep)
331331

332332
return val
333333
}
@@ -408,7 +408,7 @@ type uint64SliceParser []uint64
408408
func (i uint64SliceParser) ParseEnv(key string, defaltVal any, options Parameters) any {
409409
sep := options.Separator
410410

411-
val := uint64SliceOrDefault(key, defaltVal.([]uint64), sep)
411+
val := uintSliceOrDefaultGen(key, defaltVal.([]uint64), sep)
412412

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

437-
val := uintSliceOrDefault(key, defaltVal.([]uint), sep)
437+
val := uintSliceOrDefaultGen(key, defaltVal.([]uint), sep)
438438

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

447-
val := uint8SliceOrDefault(key, defaltVal.([]uint8), sep)
447+
val := uintSliceOrDefaultGen(key, defaltVal.([]uint8), sep)
448448

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

457-
val := uint32SliceOrDefault(key, defaltVal.([]uint32), sep)
457+
val := uintSliceOrDefaultGen(key, defaltVal.([]uint32), sep)
458458

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

467-
val := uint16SliceOrDefault(key, defaltVal.([]uint16), sep)
467+
val := uintSliceOrDefaultGen(key, defaltVal.([]uint16), sep)
468468

469469
return val
470470
}
@@ -553,7 +553,7 @@ type uintptrSliceParser []uintptr
553553
func (i uintptrSliceParser) ParseEnv(key string, defaltVal any, options Parameters) any {
554554
sep := options.Separator
555555

556-
val := uintptrSliceOrDefault(key, defaltVal.([]uintptr), sep)
556+
val := uintSliceOrDefaultGen(key, defaltVal.([]uintptr), sep)
557557

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

576-
val := complex64SliceOrDefault(key, defaltVal.([]complex64), sep)
576+
val := complexSliceOrDefaultGen(key, defaltVal.([]complex64), sep)
577577

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

596-
val := complex128SliceOrDefault(key, defaltVal.([]complex128), sep)
596+
val := complexSliceOrDefaultGen(key, defaltVal.([]complex128), sep)
597597

598598
return val
599599
}

0 commit comments

Comments
 (0)
0