v0.0.127
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"os"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -172,6 +173,20 @@ func parseEnvToValue(envval string, fullEnvKey string, rvtype reflect.Type) (ref
|
||||
|
||||
return envcvl, nil
|
||||
|
||||
} else if rvtype.ConvertibleTo(reflect.TypeOf(false)) {
|
||||
|
||||
if strings.TrimSpace(strings.ToLower(envval)) == "true" {
|
||||
return reflect.ValueOf(true).Convert(rvtype), nil
|
||||
} else if strings.TrimSpace(strings.ToLower(envval)) == "false" {
|
||||
return reflect.ValueOf(true).Convert(rvtype), nil
|
||||
} else if strings.TrimSpace(strings.ToLower(envval)) == "1" {
|
||||
return reflect.ValueOf(false).Convert(rvtype), nil
|
||||
} else if strings.TrimSpace(strings.ToLower(envval)) == "0" {
|
||||
return reflect.ValueOf(false).Convert(rvtype), nil
|
||||
} else {
|
||||
return reflect.Value{}, errors.New(fmt.Sprintf("Failed to parse env-config variable '%s' to <%s, ,bool> (value := '%s')", rvtype.Name(), fullEnvKey, envval))
|
||||
}
|
||||
|
||||
} else if rvtype.ConvertibleTo(reflect.TypeOf("")) {
|
||||
|
||||
envcvl := reflect.ValueOf(envval).Convert(rvtype)
|
||||
|
@@ -68,6 +68,7 @@ func TestApplyEnvOverridesSimple(t *testing.T) {
|
||||
V7 aliasstring `env:"TEST_V7"`
|
||||
V8 time.Duration `env:"TEST_V8"`
|
||||
V9 time.Time `env:"TEST_V9"`
|
||||
VA bool `env:"TEST_VA"`
|
||||
}
|
||||
|
||||
data := testdata{
|
||||
@@ -82,6 +83,7 @@ func TestApplyEnvOverridesSimple(t *testing.T) {
|
||||
V7: "7",
|
||||
V8: 9,
|
||||
V9: time.Unix(1671102873, 0),
|
||||
VA: false,
|
||||
}
|
||||
|
||||
t.Setenv("TEST_V1", "846")
|
||||
@@ -93,6 +95,7 @@ func TestApplyEnvOverridesSimple(t *testing.T) {
|
||||
t.Setenv("TEST_V7", "AAAAAA")
|
||||
t.Setenv("TEST_V8", "1min4s")
|
||||
t.Setenv("TEST_V9", "2009-11-10T23:00:00Z")
|
||||
t.Setenv("TEST_VA", "true")
|
||||
|
||||
err := ApplyEnvOverrides("", &data, ".")
|
||||
if err != nil {
|
||||
@@ -109,6 +112,7 @@ func TestApplyEnvOverridesSimple(t *testing.T) {
|
||||
tst.AssertEqual(t, data.V7, "AAAAAA")
|
||||
tst.AssertEqual(t, data.V8, time.Second*64)
|
||||
tst.AssertEqual(t, data.V9, time.Unix(1257894000, 0).UTC())
|
||||
tst.AssertEqual(t, data.VA, true)
|
||||
}
|
||||
|
||||
func TestApplyEnvOverridesRecursive(t *testing.T) {
|
||||
|
Reference in New Issue
Block a user