Skip to content
Merged
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
2 changes: 2 additions & 0 deletions env.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,8 @@ func getOr(key, defaultValue string, defExists bool, envs map[string]string) (st
switch {
case (!exists || key == "") && defExists:
return defaultValue, true, true
case exists && value == "" && defExists:
return defaultValue, true, true
case !exists:
return "", false, false
}
Expand Down
12 changes: 11 additions & 1 deletion env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,16 @@ type NestedStruct struct {
NestedVar string `env:"nestedvar"`
}

func TestIssue245(t *testing.T) {
t.Setenv("NAME_NOT_SET", "")
type user struct {
Name string `env:"NAME_NOT_SET" envDefault:"abcd"`
}
cfg := user{}
isNoErr(t, Parse(&cfg))
isEqual(t, cfg.Name, "abcd")
Comment thread
caarlos0 marked this conversation as resolved.
}

func TestParsesEnv(t *testing.T) {
tos := func(v interface{}) string {
return fmt.Sprintf("%v", v)
Expand Down Expand Up @@ -671,7 +681,7 @@ func TestErrorRequiredWithDefault(t *testing.T) {

t.Setenv("IS_REQUIRED", "")
isNoErr(t, Parse(cfg))
isEqual(t, "", cfg.IsRequired)
isEqual(t, "important", cfg.IsRequired)
}

func TestErrorRequiredNotSet(t *testing.T) {
Expand Down