Skip to content

Commit

Permalink
feat: Add support of new types parsers. (#90)
Browse files Browse the repository at this point in the history
* feat: Add support of new types parsers.

- []bool
- uintptr
- []uintptr
- complex64
- []complex64
- complex128
- []complex128

* fix: Linter warnings
  • Loading branch information
obalunenko authored Mar 29, 2023
1 parent 35bea46 commit 405bfaa
Show file tree
Hide file tree
Showing 12 changed files with 2,477 additions and 145 deletions.
42 changes: 38 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
[![Go Reference](https://pkg.go.dev/badge/github.com/obalunenko/getenv.svg)](https://pkg.go.dev/github.com/obalunenko/getenv)
[![Go Report Card](https://goreportcard.com/badge/github.com/obalunenko/getenv)](https://goreportcard.com/report/github.com/obalunenko/getenv)
[![codecov](https://codecov.io/gh/obalunenko/getenv/branch/master/graph/badge.svg)](https://codecov.io/gh/obalunenko/getenv)
![coverbadger-tag-do-not-edit](https://img.shields.io/badge/coverage-98.93%25-brightgreen?longCache=true&style=flat)
![coverbadger-tag-do-not-edit](https://img.shields.io/badge/coverage-100%25-brightgreen?longCache=true&style=flat)
[![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)

# getenv
Expand Down Expand Up @@ -30,6 +30,8 @@ Types supported:
- []uint64
- uint
- []uint
- uintptr
- []uintptr
- uint32
- []uint32
- float32
Expand All @@ -41,18 +43,23 @@ Types supported:
- time.Duration
- []time.Duration
- bool
- []bool
- url.URL
- []url.URL
- net.IP
- []net.IP
- complex64
- []complex64
- complex128
- []complex128

## Examples

### EnvOrDefault

EnvOrDefault retrieves the value of the environment variable named
by the key.
If variable not set or value is empty - defaultVal will be returned.
EnvOrDefault retrieves the value of the environment variable named by the key.
If the variable is present in the environment the value will be parsed and returned.
Otherwise, the default value will be returned.

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

// []string
if err := os.Setenv(key, "a,b,c,d"); err != nil {
panic(err)
}

val = getenv.EnvOrDefault(key, []string{}, option.WithSeparator(","))
fmt.Printf("[%T]: %v\n", val, val)

// complex128
if err := os.Setenv(key, "1+2i"); err != nil {
panic(err)
}

val = getenv.EnvOrDefault(key, complex128(0))
fmt.Printf("[%T]: %v\n", val, val)

// []complex64
if err := os.Setenv(key, "1+2i,3+4i"); err != nil {
panic(err)
}

val = getenv.EnvOrDefault(key, []complex64{}, option.WithSeparator(","))
fmt.Printf("[%T]: %v\n", val, val)

}

```
Expand All @@ -154,4 +185,7 @@ Output:
[time.Duration]: 2h35m0s
[url.URL]: {https test:abcd123 golangbyexample.com:8000 /tutorials/intro false false type=advance&compact=false history }
[net.IP]: 2001:cb8::17
[[]string]: [a b c d]
[complex128]: (1+2i)
[[]complex64]: [(1+2i) (3+4i)]
```
14 changes: 11 additions & 3 deletions getenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
// - []uint64
// - uint
// - []uint
// - uintptr
// - []uintptr
// - uint32
// - []uint32
// - float32
Expand All @@ -32,20 +34,25 @@
// - time.Duration
// - []time.Duration
// - bool
// - []bool
// - url.URL
// - []url.URL
// - net.IP
// - []net.IP
// - complex64
// - []complex64
// - complex128
// - []complex128
package getenv

import (
"github.com/obalunenko/getenv/internal"
"github.com/obalunenko/getenv/option"
)

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

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

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

Expand Down
27 changes: 27 additions & 0 deletions getenv_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,30 @@ func ExampleEnvOrDefault() {
val = getenv.EnvOrDefault(key, net.IP{})
fmt.Printf("[%T]: %v\n", val, val)

// []string
if err := os.Setenv(key, "a,b,c,d"); err != nil {
panic(err)
}

val = getenv.EnvOrDefault(key, []string{}, option.WithSeparator(","))
fmt.Printf("[%T]: %v\n", val, val)

// complex128
if err := os.Setenv(key, "1+2i"); err != nil {
panic(err)
}

val = getenv.EnvOrDefault(key, complex128(0))
fmt.Printf("[%T]: %v\n", val, val)

// []complex64
if err := os.Setenv(key, "1+2i,3+4i"); err != nil {
panic(err)
}

val = getenv.EnvOrDefault(key, []complex64{}, option.WithSeparator(","))
fmt.Printf("[%T]: %v\n", val, val)

// Output:
// [string]: golly
// [int]: 123
Expand All @@ -91,4 +115,7 @@ func ExampleEnvOrDefault() {
// [time.Duration]: 2h35m0s
// [url.URL]: {https test:abcd123 golangbyexample.com:8000 /tutorials/intro false false type=advance&compact=false history }
// [net.IP]: 2001:cb8::17
// [[]string]: [a b c d]
// [complex128]: (1+2i)
// [[]complex64]: [(1+2i) (3+4i)]
}
Loading

0 comments on commit 405bfaa

Please sign in to comment.