Skip to content

Commit 405bfaa

Browse files
authored
feat: Add support of new types parsers. (#90)
* feat: Add support of new types parsers. - []bool - uintptr - []uintptr - complex64 - []complex64 - complex128 - []complex128 * fix: Linter warnings
1 parent 35bea46 commit 405bfaa

12 files changed

Lines changed: 2477 additions & 145 deletions

README.md

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
[![Go Reference](https://pkg.go.dev/badge/github.com/obalunenko/getenv.svg)](https://pkg.go.dev/github.com/obalunenko/getenv)
33
[![Go Report Card](https://goreportcard.com/badge/github.com/obalunenko/getenv)](https://goreportcard.com/report/github.com/obalunenko/getenv)
44
[![codecov](https://codecov.io/gh/obalunenko/getenv/branch/master/graph/badge.svg)](https://codecov.io/gh/obalunenko/getenv)
5-
![coverbadger-tag-do-not-edit](https://img.shields.io/badge/coverage-98.93%25-brightgreen?longCache=true&style=flat)
5+
![coverbadger-tag-do-not-edit](https://img.shields.io/badge/coverage-100%25-brightgreen?longCache=true&style=flat)
66
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=obalunenko_getenv&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=obalunenko_getenv)
77

88
# getenv
@@ -30,6 +30,8 @@ Types supported:
3030
- []uint64
3131
- uint
3232
- []uint
33+
- uintptr
34+
- []uintptr
3335
- uint32
3436
- []uint32
3537
- float32
@@ -41,18 +43,23 @@ Types supported:
4143
- time.Duration
4244
- []time.Duration
4345
- bool
46+
- []bool
4447
- url.URL
4548
- []url.URL
4649
- net.IP
4750
- []net.IP
51+
- complex64
52+
- []complex64
53+
- complex128
54+
- []complex128
4855

4956
## Examples
5057

5158
### EnvOrDefault
5259

53-
EnvOrDefault retrieves the value of the environment variable named
54-
by the key.
55-
If variable not set or value is empty - defaultVal will be returned.
60+
EnvOrDefault retrieves the value of the environment variable named by the key.
61+
If the variable is present in the environment the value will be parsed and returned.
62+
Otherwise, the default value will be returned.
5663

5764
```golang
5865
package main
@@ -140,6 +147,30 @@ func main() {
140147
val = getenv.EnvOrDefault(key, net.IP{})
141148
fmt.Printf("[%T]: %v\n", val, val)
142149

150+
// []string
151+
if err := os.Setenv(key, "a,b,c,d"); err != nil {
152+
panic(err)
153+
}
154+
155+
val = getenv.EnvOrDefault(key, []string{}, option.WithSeparator(","))
156+
fmt.Printf("[%T]: %v\n", val, val)
157+
158+
// complex128
159+
if err := os.Setenv(key, "1+2i"); err != nil {
160+
panic(err)
161+
}
162+
163+
val = getenv.EnvOrDefault(key, complex128(0))
164+
fmt.Printf("[%T]: %v\n", val, val)
165+
166+
// []complex64
167+
if err := os.Setenv(key, "1+2i,3+4i"); err != nil {
168+
panic(err)
169+
}
170+
171+
val = getenv.EnvOrDefault(key, []complex64{}, option.WithSeparator(","))
172+
fmt.Printf("[%T]: %v\n", val, val)
173+
143174
}
144175

145176
```
@@ -154,4 +185,7 @@ Output:
154185
[time.Duration]: 2h35m0s
155186
[url.URL]: {https test:abcd123 golangbyexample.com:8000 /tutorials/intro false false type=advance&compact=false history }
156187
[net.IP]: 2001:cb8::17
188+
[[]string]: [a b c d]
189+
[complex128]: (1+2i)
190+
[[]complex64]: [(1+2i) (3+4i)]
157191
```

getenv.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
// - []uint64
2222
// - uint
2323
// - []uint
24+
// - uintptr
25+
// - []uintptr
2426
// - uint32
2527
// - []uint32
2628
// - float32
@@ -32,20 +34,25 @@
3234
// - time.Duration
3335
// - []time.Duration
3436
// - bool
37+
// - []bool
3538
// - url.URL
3639
// - []url.URL
3740
// - net.IP
3841
// - []net.IP
42+
// - complex64
43+
// - []complex64
44+
// - complex128
45+
// - []complex128
3946
package getenv
4047

4148
import (
4249
"github.com/obalunenko/getenv/internal"
4350
"github.com/obalunenko/getenv/option"
4451
)
4552

46-
// EnvOrDefault retrieves the value of the environment variable named
47-
// by the key.
48-
// If variable not set or value is empty - defaultVal will be returned.
53+
// EnvOrDefault retrieves the value of the environment variable named by the key.
54+
// If the variable is present in the environment the value will be parsed and returned.
55+
// Otherwise, the default value will be returned.
4956
func EnvOrDefault[T internal.EnvParsable](key string, defaultVal T, options ...option.Option) T {
5057
w := internal.NewEnvParser(defaultVal)
5158

@@ -56,6 +63,7 @@ func EnvOrDefault[T internal.EnvParsable](key string, defaultVal T, options ...o
5663
return val.(T)
5764
}
5865

66+
// newParseParams creates new parameters from options.
5967
func newParseParams(opts []option.Option) internal.Parameters {
6068
var p internal.Parameters
6169

getenv_example_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,30 @@ func ExampleEnvOrDefault() {
8383
val = getenv.EnvOrDefault(key, net.IP{})
8484
fmt.Printf("[%T]: %v\n", val, val)
8585

86+
// []string
87+
if err := os.Setenv(key, "a,b,c,d"); err != nil {
88+
panic(err)
89+
}
90+
91+
val = getenv.EnvOrDefault(key, []string{}, option.WithSeparator(","))
92+
fmt.Printf("[%T]: %v\n", val, val)
93+
94+
// complex128
95+
if err := os.Setenv(key, "1+2i"); err != nil {
96+
panic(err)
97+
}
98+
99+
val = getenv.EnvOrDefault(key, complex128(0))
100+
fmt.Printf("[%T]: %v\n", val, val)
101+
102+
// []complex64
103+
if err := os.Setenv(key, "1+2i,3+4i"); err != nil {
104+
panic(err)
105+
}
106+
107+
val = getenv.EnvOrDefault(key, []complex64{}, option.WithSeparator(","))
108+
fmt.Printf("[%T]: %v\n", val, val)
109+
86110
// Output:
87111
// [string]: golly
88112
// [int]: 123
@@ -91,4 +115,7 @@ func ExampleEnvOrDefault() {
91115
// [time.Duration]: 2h35m0s
92116
// [url.URL]: {https test:abcd123 golangbyexample.com:8000 /tutorials/intro false false type=advance&compact=false history }
93117
// [net.IP]: 2001:cb8::17
118+
// [[]string]: [a b c d]
119+
// [complex128]: (1+2i)
120+
// [[]complex64]: [(1+2i) (3+4i)]
94121
}

0 commit comments

Comments
 (0)