diff options
| author | Paul Buetow <paul@buetow.org> | 2024-09-03 22:36:09 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2024-09-03 22:36:09 +0300 |
| commit | aed26ec92f64567e2a6a8103212f589f464ee567 (patch) | |
| tree | 74f02949940e662f3d0f2ffc95f371ed811a4123 | |
| parent | 1ade563588a8de2dfa9356e2c0e2ce33a3861227 (diff) | |
fix tests
| -rw-r--r-- | internal/config/config_test.go | 19 | ||||
| -rw-r--r-- | internal/config/enver.go | 41 |
2 files changed, 21 insertions, 39 deletions
diff --git a/internal/config/config_test.go b/internal/config/config_test.go index ea96ed9..1c80fd5 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -20,26 +20,23 @@ func TestEnvToStr(t *testing.T) { if got != expected { t.Errorf("got '%s' but expected '%s'", got, expected) } - t.Logf("got '%s' as expected", expected) expected = "default value" got = Env[ToString]("NON_EXISTENT_ENV", expected) if got != expected { t.Errorf("got '%s' but expected '%s'", got, expected) } - t.Logf("got '%s' as expected", expected) if got = Env[ToString]("NON_EXISTENT_ENV"); got != "" { t.Errorf("got '%s' but expected empty string", got) } - t.Logf("got empty string as expected") expected = "casio g-shock" + os.Setenv("GOS_WATCH", expected) got = Env[ToString]("GOS_WATCH", "", "", "", expected, "") if got != expected { t.Errorf("got '%s' but expected '%s'", got, expected) } - t.Logf("got '%s' as expected", expected) } func TestEnvToStrSlice(t *testing.T) { @@ -54,27 +51,23 @@ func TestEnvToStrSlice(t *testing.T) { if !slices.Equal(got, expected) { t.Errorf("got '%v' but expected '%v'", got, expected) } - t.Logf("got '%v' as expected", expected) expected = []string{"default value"} got = Env[ToStringSlice]("NON_EXISTENT_ENV_SLICE", "default value") if !slices.Equal(got, expected) { t.Errorf("got '%v' but expected '%v'", got, expected) } - t.Logf("got '%v' as expected", expected) os.Unsetenv("NON_EXISTENT_ENV") if got = Env[ToStringSlice]("NON_EXISTENT_ENV"); len(got) > 0 { t.Errorf("got '%s' of len '%d' but expected empty slice", got, len(got)) } - t.Logf("got empty slice as expected") expected = []string{"casio", "g-shock"} got = Env[ToStringSlice]("NON_EXISTENT_ENV", "", "", "", "casio,g-shock", "") if !slices.Equal(got, expected) { t.Errorf("got '%v' but expected '%v'", got, expected) } - t.Logf("got '%v' as expected", expected) } func TestEnvToInt(t *testing.T) { @@ -91,26 +84,22 @@ func TestEnvToInt(t *testing.T) { if got != expected { t.Errorf("got '%d' but expected '%d'", got, expected) } - t.Logf("got '%d' as expected", expected) expected = 999 got = Env[ToInteger]("NON_EXISTENT_ENV", expected) if got != expected { t.Errorf("got '%d' but expected '%d'", got, expected) } - t.Logf("got '%d' as expected", expected) if got = Env[ToInteger]("NON_EXISTENT_ENV"); got != 0 { t.Errorf("got '%d' but expected zero", got) } - t.Logf("got zero as expected") expected = 1234 got = Env[ToInteger]("GOS_WATCH", "", "", "", expected, "") if got != expected { t.Errorf("got '%d' but expected '%d'", got, expected) } - t.Logf("got '%d' as expected", expected) } func TestEnvToBool(t *testing.T) { @@ -121,32 +110,28 @@ func TestEnvToBool(t *testing.T) { var ( expected = true - got = Env[ToBool](t, "GOS_TEST_BOOL_FROM_ENV") + got = Env[ToBool]("GOS_TEST_BOOL_FROM_ENV") ) if got != expected { t.Errorf("got '%t' but expected '%t'", got, expected) } - t.Logf("got '%t' as expected", expected) expected = false got = Env[ToBool]("NON_EXISTENT_ENV", expected) if got != expected { t.Errorf("got '%t' but expected '%t'", got, expected) } - t.Logf("got '%t' as expected", expected) if got = Env[ToBool]("NON_EXISTENT_ENV"); got { t.Errorf("got '%t' but expected false", got) } - t.Logf("got 'false' as expected") expected = true got = Env[ToBool]("NON_EXISTENT_ENV", "", "", "", expected, "") if got != expected { t.Errorf("got '%t' but expected '%t'", got, expected) } - t.Logf("got '%t' as expected", expected) } func TestSecondENV(t *testing.T) { diff --git a/internal/config/enver.go b/internal/config/enver.go index c9b3360..0cde181 100644 --- a/internal/config/enver.go +++ b/internal/config/enver.go @@ -12,7 +12,7 @@ type enverConstraint interface { type enver[T enverConstraint] interface { // Return T value from input string - fromStr(value string) T + fromStr(value string) (T, error) // Return T's zero value zero() T } @@ -27,11 +27,16 @@ func Env[U enver[T], T enverConstraint](keys ...any) T { continue } if !isAllUpperCase(key) { - return enver.fromStr(key) - } - if value := os.Getenv(key); value != "" { - return enver.fromStr(value) + if val, err := enver.fromStr(key); err == nil { + return val + } + } else if strVal := os.Getenv(key); strVal != "" { + if val, err := enver.fromStr(strVal); err == nil { + return val + } } + case T: + return key case func() T: return key() } @@ -42,8 +47,8 @@ func Env[U enver[T], T enverConstraint](keys ...any) T { type ToString struct{} -func (ToString) fromStr(str string) string { - return str +func (ToString) fromStr(str string) (string, error) { + return str, nil } func (ToString) zero() string { @@ -52,12 +57,12 @@ func (ToString) zero() string { type ToStringSlice struct{} -func (s ToStringSlice) fromStr(str string) []string { +func (s ToStringSlice) fromStr(str string) ([]string, error) { result := strings.Split(str, ",") if len(result) == 1 && result[0] == "" { - return s.zero() + return s.zero(), nil } - return result + return result, nil } func (ToStringSlice) zero() []string { @@ -66,12 +71,8 @@ func (ToStringSlice) zero() []string { type ToInteger struct{} -// TODO: Return an error if can't convert to int -func (s ToInteger) fromStr(str string) int { - if result, err := strconv.Atoi(str); err == nil { - return result - } - return s.zero() +func (ToInteger) fromStr(str string) (int, error) { + return strconv.Atoi(str) } func (ToInteger) zero() int { @@ -80,12 +81,8 @@ func (ToInteger) zero() int { type ToBool struct{} -// TODO: Return an error if can't convert to bool -func (s ToBool) fromStr(str string) bool { - if result, err := strconv.ParseBool(str); err == nil { - return result - } - return s.zero() +func (ToBool) fromStr(str string) (bool, error) { + return strconv.ParseBool(str) } func (ToBool) zero() bool { |
