Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Proposal] add struct tag for fallback value in case of error #193

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion envconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,10 @@ func Process(prefix string, spec interface{}) error {
continue
}

err = processField(value, info.Field)
if err = processField(value, info.Field); err != nil && info.Tags.Get("on_err") != "" {
err = processField(info.Tags.Get("on_err"), info.Field)
}

if err != nil {
return &ParseError{
KeyName: info.Key,
Expand Down
8 changes: 7 additions & 1 deletion envconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type Specification struct {
NoPrefixDefault string `envconfig:"BROKER" default:"127.0.0.1"`
RequiredDefault string `required:"true" default:"foo2bar"`
Ignored string `ignored:"true"`
OnErr *bool `on_err:"false"`
NestedSpecification struct {
Property string `envconfig:"inner"`
PropertyWithDefault string `default:"fuzzybydefault"`
Expand Down Expand Up @@ -103,6 +104,7 @@ func TestProcess(t *testing.T) {
os.Setenv("ENV_CONFIG_TTL", "30")
os.Setenv("ENV_CONFIG_REQUIREDVAR", "foo")
os.Setenv("ENV_CONFIG_IGNORED", "was-not-ignored")
os.Setenv("ENV_CONFIG_ONERR", "invalid-value")
os.Setenv("ENV_CONFIG_OUTER_INNER", "iamnested")
os.Setenv("ENV_CONFIG_AFTERNESTED", "after")
os.Setenv("ENV_CONFIG_HONOR", "honor")
Expand Down Expand Up @@ -177,6 +179,10 @@ func TestProcess(t *testing.T) {
)
}

if *s.OnErr != false {
t.Errorf("expected '%v' bool, got %v", false, *s.OnErr)
}

if s.NestedSpecification.Property != "iamnested" {
t.Errorf("expected '%s' string, got %#v", "iamnested", s.NestedSpecification.Property)
}
Expand Down Expand Up @@ -794,7 +800,7 @@ func TestCheckDisallowedIgnored(t *testing.T) {

func TestErrorMessageForRequiredAltVar(t *testing.T) {
var s struct {
Foo string `envconfig:"BAR" required:"true"`
Foo string `envconfig:"BAR" required:"true"`
}

os.Clearenv()
Expand Down
1 change: 1 addition & 0 deletions testdata/custom.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ ENV_CONFIG_DEFAULTVAR=
ENV_CONFIG_REQUIREDVAR=
ENV_CONFIG_BROKER=
ENV_CONFIG_REQUIREDDEFAULT=
ENV_CONFIG_ONERR=
ENV_CONFIG_OUTER_INNER=
ENV_CONFIG_OUTER_PROPERTYWITHDEFAULT=
ENV_CONFIG_AFTERNESTED=
Expand Down
5 changes: 5 additions & 0 deletions testdata/default_list.txt
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ ENV_CONFIG_REQUIREDDEFAULT
..[type]........String
..[default].....foo2bar
..[required]....true
ENV_CONFIG_ONERR
..[description].
..[type]........True.or.False
..[default].....
..[required]....
ENV_CONFIG_OUTER_INNER
..[description].
..[type]........String
Expand Down
1 change: 1 addition & 0 deletions testdata/default_table.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ ENV_CONFIG_DEFAULTVAR............................String.........................
ENV_CONFIG_REQUIREDVAR...........................String................................................................true........
ENV_CONFIG_BROKER................................String..........................................127.0.0.1.........................
ENV_CONFIG_REQUIREDDEFAULT.......................String..........................................foo2bar...............true........
ENV_CONFIG_ONERR.................................True.or.False.....................................................................
ENV_CONFIG_OUTER_INNER...........................String............................................................................
ENV_CONFIG_OUTER_PROPERTYWITHDEFAULT.............String..........................................fuzzybydefault....................
ENV_CONFIG_AFTERNESTED...........................String............................................................................
Expand Down
1 change: 1 addition & 0 deletions testdata/fault.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@
{.Key}
{.Key}
{.Key}
{.Key}