Skip to content

Commit

Permalink
Merge pull request #2 from evilthiesje/master
Browse files Browse the repository at this point in the history
v2: return default value if boolean is not set
  • Loading branch information
phifty authored Feb 22, 2022
2 parents cfa55d8 + 59dae55 commit 8e929e5
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
2 changes: 2 additions & 0 deletions v2/bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ func (f *BoolField) Get() (bool, error) {
switch v {
case "1", trueValue, "yes":
return true, nil
case "":
return f.defaultValue, nil
default:
return false, nil
}
Expand Down
4 changes: 2 additions & 2 deletions v2/bool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (

func TestBool(t *testing.T) {
var (
optional = env.Bool("OPTIONAL_FIELD", false)
optional = env.Bool("OPTIONAL_FIELD", true)
required = env.Bool("REQUIRED_FIELD", false, env.Required())
)

Expand All @@ -45,7 +45,7 @@ func TestBool(t *testing.T) {
}

t.Run("Value", testFn(optional, "1", true, nil))
t.Run("DefaultValue", testFn(optional, "", false, nil))
t.Run("DefaultValue", testFn(optional, "", true, nil))
t.Run("RequiredAndSet", testFn(required, "yes", true, nil))
t.Run("RequiredNotSet", testFn(required, "", false, env.ErrRequiredValueIsMissing))
t.Run("UnallowedValue", testFn(optional, "okaydokay", false, env.ErrValueIsNotAllowed))
Expand Down

0 comments on commit 8e929e5

Please sign in to comment.