Skip to content

Commit 8e929e5

Browse files
authored
Merge pull request #2 from evilthiesje/master
v2: return default value if boolean is not set
2 parents cfa55d8 + 59dae55 commit 8e929e5

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

v2/bool.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ func (f *BoolField) Get() (bool, error) {
7575
switch v {
7676
case "1", trueValue, "yes":
7777
return true, nil
78+
case "":
79+
return f.defaultValue, nil
7880
default:
7981
return false, nil
8082
}

v2/bool_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626

2727
func TestBool(t *testing.T) {
2828
var (
29-
optional = env.Bool("OPTIONAL_FIELD", false)
29+
optional = env.Bool("OPTIONAL_FIELD", true)
3030
required = env.Bool("REQUIRED_FIELD", false, env.Required())
3131
)
3232

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

4747
t.Run("Value", testFn(optional, "1", true, nil))
48-
t.Run("DefaultValue", testFn(optional, "", false, nil))
48+
t.Run("DefaultValue", testFn(optional, "", true, nil))
4949
t.Run("RequiredAndSet", testFn(required, "yes", true, nil))
5050
t.Run("RequiredNotSet", testFn(required, "", false, env.ErrRequiredValueIsMissing))
5151
t.Run("UnallowedValue", testFn(optional, "okaydokay", false, env.ErrValueIsNotAllowed))

0 commit comments

Comments
 (0)