Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ParseAs, ParseAsWithOptions, Must
closes #299 Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
- Loading branch information
Showing
3 changed files
with
76 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//go:build go1.18 | ||
// +build go1.18 | ||
|
||
package env | ||
|
||
// ParseAs parses the given struct type containing `env` tags and loads its | ||
// values from environment variables. | ||
func ParseAs[T any]() (T, error) { | ||
Check failure on line 8 in env_generic.go GitHub Actions / lint
|
||
var t T | ||
return t, Parse(&t) | ||
} | ||
|
||
// ParseWithOptions parses the given struct type containing `env` tags and | ||
// loads its values from environment variables. | ||
func ParseAsWithOptions[T any](opts Options) (T, error) { | ||
Check failure on line 15 in env_generic.go GitHub Actions / lint
|
||
var t T | ||
return t, ParseWithOptions(&t, opts) | ||
} | ||
|
||
// Must panic is if err is not nil, and returns t otherwise. | ||
func Must[T any](t T, err error) T { | ||
Check failure on line 21 in env_generic.go GitHub Actions / lint
|
||
if err != nil { | ||
panic(err) | ||
} | ||
return t | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
//go:build go1.18 | ||
// +build go1.18 | ||
|
||
package env | ||
|
||
import "testing" | ||
|
||
type Conf struct { | ||
Foo string `env:"FOO" envDefault:"bar"` | ||
} | ||
|
||
func TestParseAs(t *testing.T) { | ||
config, err := ParseAs[Conf]() | ||
isNoErr(t, err) | ||
isEqual(t, "bar", config.Foo) | ||
} | ||
|
||
func TestParseAsWithOptions(t *testing.T) { | ||
config, err := ParseAsWithOptions[Conf](Options{ | ||
Environment: map[string]string{ | ||
"FOO": "not bar", | ||
}, | ||
}) | ||
isNoErr(t, err) | ||
isEqual(t, "not bar", config.Foo) | ||
} | ||
|
||
type ConfRequired struct { | ||
Foo string `env:"FOO,required"` | ||
} | ||
|
||
func TestMust(t *testing.T) { | ||
t.Run("error", func(t *testing.T) { | ||
defer func() { | ||
err := recover() | ||
isErrorWithMessage(t, err.(error), `env: required environment variable "FOO" is not set`) | ||
}() | ||
conf := Must(ParseAs[ConfRequired]()) | ||
isEqual(t, "", conf.Foo) | ||
}) | ||
t.Run("success", func(t *testing.T) { | ||
t.Setenv("FOO", "bar") | ||
conf := Must(ParseAs[ConfRequired]()) | ||
isEqual(t, "bar", conf.Foo) | ||
}) | ||
} |