summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2024-09-03 22:54:04 +0300
committerPaul Buetow <paul@buetow.org>2024-09-03 22:54:04 +0300
commit74de487c57f22c4c7f9b04404a21b3a69f252f70 (patch)
tree19733a34d77c928ea51c85f1aebd78bb245d1143
parent1fcd3eca65b9339fac1348cd2a4ab81110882f11 (diff)
hiding generics as types cant be inferred
-rw-r--r--internal/config/client/client.go12
-rw-r--r--internal/config/config_test.go38
-rw-r--r--internal/config/enver.go42
-rw-r--r--internal/config/server/server.go20
4 files changed, 64 insertions, 48 deletions
diff --git a/internal/config/client/client.go b/internal/config/client/client.go
index 94f0d98..f551686 100644
--- a/internal/config/client/client.go
+++ b/internal/config/client/client.go
@@ -26,16 +26,16 @@ func New(configFile string) (ClientConfig, error) {
log.Println("Skipping config file:", err)
}
- conf.Servers = config.Env[config.StrSlice]("GOS_SERVERS", conf.Servers)
- conf.APIKey = config.Env[config.Str]("GOS_API_KEY", conf.APIKey)
- conf.Editor = config.Env[config.Str]("GOS_EDITOR", "EDITOR", conf.Editor, "vi")
+ conf.Servers = config.StrSlice("GOS_SERVERS", conf.Servers)
+ conf.APIKey = config.Str("GOS_API_KEY", conf.APIKey)
+ conf.Editor = config.Str("GOS_EDITOR", "EDITOR", conf.Editor, "vi")
defaultDataDir := fmt.Sprintf("%s/.gos/data", os.Getenv("HOME"))
- conf.DataDir = config.Env[config.Str]("GOS_DATA_DIR", conf.DataDir, defaultDataDir)
- conf.ComposeFile = config.Env[config.Str]("GOS_COMPOSE_FILE", conf.ComposeFile, "compose.txt")
+ conf.DataDir = config.Str("GOS_DATA_DIR", conf.DataDir, defaultDataDir)
+ conf.ComposeFile = config.Str("GOS_COMPOSE_FILE", conf.ComposeFile, "compose.txt")
defaultLogFile := fmt.Sprintf("%s/.gos/gos.log", os.Getenv("HOME"))
- conf.LogFile = config.Env[config.Str]("GOS_LOG_FILE", conf.LogFile, defaultLogFile)
+ conf.LogFile = config.Str("GOS_LOG_FILE", conf.LogFile, defaultLogFile)
return conf, nil
}
diff --git a/internal/config/config_test.go b/internal/config/config_test.go
index 588bd2f..eca163f 100644
--- a/internal/config/config_test.go
+++ b/internal/config/config_test.go
@@ -14,7 +14,7 @@ func TestEnvToStr(t *testing.T) {
var (
expected = "foobarbaz"
- got = Env[Str]("GOS_TEST_FROM_ENV")
+ got = Str("GOS_TEST_FROM_ENV")
)
if got != expected {
@@ -22,18 +22,18 @@ func TestEnvToStr(t *testing.T) {
}
expected = "default value"
- got = Env[Str]("NON_EXISTENT_ENV", expected)
+ got = Str("NON_EXISTENT_ENV", expected)
if got != expected {
t.Errorf("got '%s' but expected '%s'", got, expected)
}
- if got = Env[Str]("NON_EXISTENT_ENV"); got != "" {
+ if got = Str("NON_EXISTENT_ENV"); got != "" {
t.Errorf("got '%s' but expected empty string", got)
}
expected = "casio g-shock"
os.Setenv("GOS_WATCH", expected)
- got = Env[Str]("GOS_WATCH", "", "", "", expected, "")
+ got = Str("GOS_WATCH", "", "", "", expected, "")
if got != expected {
t.Errorf("got '%s' but expected '%s'", got, expected)
}
@@ -46,25 +46,25 @@ func TestEnvToStrSlice(t *testing.T) {
var (
expected = []string{"foo", "bar", "baz"}
- got = Env[StrSlice]("GOS_TEST_SLICE_FROM_ENV")
+ got = StrSlice("GOS_TEST_SLICE_FROM_ENV")
)
if !slices.Equal(got, expected) {
t.Errorf("got '%v' but expected '%v'", got, expected)
}
expected = []string{"default value"}
- got = Env[StrSlice]("NON_EXISTENT_ENV_SLICE", "default value")
+ got = StrSlice("NON_EXISTENT_ENV_SLICE", "default value")
if !slices.Equal(got, expected) {
t.Errorf("got '%v' but expected '%v'", got, expected)
}
os.Unsetenv("NON_EXISTENT_ENV")
- if got = Env[StrSlice]("NON_EXISTENT_ENV"); len(got) > 0 {
+ if got = StrSlice("NON_EXISTENT_ENV"); len(got) > 0 {
t.Errorf("got '%s' of len '%d' but expected empty slice", got, len(got))
}
expected = []string{"casio", "g-shock"}
- got = Env[StrSlice]("NON_EXISTENT_ENV", "", "", "", "casio,g-shock", "")
+ got = StrSlice("NON_EXISTENT_ENV", "", "", "", "casio,g-shock", "")
if !slices.Equal(got, expected) {
t.Errorf("got '%v' but expected '%v'", got, expected)
}
@@ -78,7 +78,7 @@ func TestEnvToInt(t *testing.T) {
var (
expected = 1
- got = Env[Int](t, "GOS_TEST_INT_FROM_ENV")
+ got = Int(t, "GOS_TEST_INT_FROM_ENV")
)
if got != expected {
@@ -86,17 +86,17 @@ func TestEnvToInt(t *testing.T) {
}
expected = 999
- got = Env[Int]("NON_EXISTENT_ENV", expected)
+ got = Int("NON_EXISTENT_ENV", expected)
if got != expected {
t.Errorf("got '%d' but expected '%d'", got, expected)
}
- if got = Env[Int]("NON_EXISTENT_ENV"); got != 0 {
+ if got = Int("NON_EXISTENT_ENV"); got != 0 {
t.Errorf("got '%d' but expected zero", got)
}
expected = 1234
- got = Env[Int]("GOS_WATCH", "", "", "", expected, "")
+ got = Int("GOS_WATCH", "", "", "", expected, "")
if got != expected {
t.Errorf("got '%d' but expected '%d'", got, expected)
}
@@ -110,7 +110,7 @@ func TestEnvToBool(t *testing.T) {
var (
expected = true
- got = Env[Bool]("GOS_TEST_BOOL_FROM_ENV")
+ got = Bool("GOS_TEST_BOOL_FROM_ENV")
)
if got != expected {
@@ -118,17 +118,17 @@ func TestEnvToBool(t *testing.T) {
}
expected = false
- got = Env[Bool]("NON_EXISTENT_ENV", expected)
+ got = Bool("NON_EXISTENT_ENV", expected)
if got != expected {
t.Errorf("got '%t' but expected '%t'", got, expected)
}
- if got = Env[Bool]("NON_EXISTENT_ENV"); got {
+ if got = Bool("NON_EXISTENT_ENV"); got {
t.Errorf("got '%t' but expected false", got)
}
expected = true
- got = Env[Bool]("NON_EXISTENT_ENV", "", "", "", expected, "")
+ got = Bool("NON_EXISTENT_ENV", "", "", "", expected, "")
if got != expected {
t.Errorf("got '%t' but expected '%t'", got, expected)
}
@@ -142,7 +142,7 @@ func TestSecondENV(t *testing.T) {
var (
expected = "hx"
- got = Env[Str]("GOS_NONEXISTANT", "EDITOR", "notepad.exe")
+ got = Str("GOS_NONEXISTANT", "EDITOR", "notepad.exe")
)
if expected != got {
@@ -168,7 +168,7 @@ func TestDefaultStrCB(t *testing.T) {
var (
expected = "hello"
- got = Env[Str]("GOS_NONEXISTANT", func() string {
+ got = Str("GOS_NONEXISTANT", func() string {
return "hello"
})
)
@@ -184,7 +184,7 @@ func TestDefaultIntCB(t *testing.T) {
var (
expected = 666
- got = Env[Int]("GOS_NONEXISTANT", func() int {
+ got = Int("GOS_NONEXISTANT", func() int {
return 666
})
)
diff --git a/internal/config/enver.go b/internal/config/enver.go
index 308398f..f1aa1d7 100644
--- a/internal/config/enver.go
+++ b/internal/config/enver.go
@@ -17,7 +17,23 @@ type enver[T enverConstraint] interface {
zero() T
}
-func Env[U enver[T], T enverConstraint](keys ...any) T {
+func Str(keys ...any) string {
+ return fromEnv[ToStr](keys...)
+}
+
+func StrSlice(keys ...any) []string {
+ return fromEnv[ToStrSlice](keys...)
+}
+
+func Int(keys ...any) int {
+ return fromEnv[ToInt](keys...)
+}
+
+func Bool(keys ...any) bool {
+ return fromEnv[ToBool](keys...)
+}
+
+func fromEnv[U enver[T], T enverConstraint](keys ...any) T {
var enver U
for _, key := range keys {
@@ -45,19 +61,19 @@ func Env[U enver[T], T enverConstraint](keys ...any) T {
return enver.zero()
}
-type Str struct{}
+type ToStr struct{}
-func (Str) fromStr(str string) (string, error) {
+func (ToStr) fromStr(str string) (string, error) {
return str, nil
}
-func (Str) zero() string {
+func (ToStr) zero() string {
return ""
}
-type StrSlice struct{}
+type ToStrSlice struct{}
-func (s StrSlice) fromStr(str string) ([]string, error) {
+func (s ToStrSlice) fromStr(str string) ([]string, error) {
result := strings.Split(str, ",")
if len(result) == 1 && result[0] == "" {
return s.zero(), nil
@@ -65,26 +81,26 @@ func (s StrSlice) fromStr(str string) ([]string, error) {
return result, nil
}
-func (StrSlice) zero() []string {
+func (ToStrSlice) zero() []string {
return []string{}
}
-type Int struct{}
+type ToInt struct{}
-func (Int) fromStr(str string) (int, error) {
+func (ToInt) fromStr(str string) (int, error) {
return strconv.Atoi(str)
}
-func (Int) zero() int {
+func (ToInt) zero() int {
return 0
}
-type Bool struct{}
+type ToBool struct{}
-func (Bool) fromStr(str string) (bool, error) {
+func (ToBool) fromStr(str string) (bool, error) {
return strconv.ParseBool(str)
}
-func (Bool) zero() bool {
+func (ToBool) zero() bool {
return false
}
diff --git a/internal/config/server/server.go b/internal/config/server/server.go
index 1a5d40f..8454c34 100644
--- a/internal/config/server/server.go
+++ b/internal/config/server/server.go
@@ -35,14 +35,14 @@ func New(configFile, secretsFile string) (ServerConfig, error) {
return conf, err
}
- conf.ListenAddr = config.Env[config.Str]("GOS_LISTEN_ADDR", conf.ListenAddr, "localhost:8080")
- conf.Partners = config.Env[config.StrSlice]("GOS_PARTNERS", conf.Partners)
- conf.APIKey = config.Env[config.Str]("GOS_API_KEY", conf.APIKey)
- conf.DataDir = config.Env[config.Str]("GOS_DATA_DIR", conf.DataDir, "data")
- conf.EmailTo = config.Env[config.Str]("GOS_EMAIL_TO", conf.EmailTo)
- conf.EmailFrom = config.Env[config.Str]("GOS_EMAIL_FROM", conf.EmailFrom)
-
- conf.SMTPServer = config.Env[config.Str]("GOS_SMTP_SERVER", conf.SMTPServer, func() string {
+ conf.ListenAddr = config.Str("GOS_LISTEN_ADDR", conf.ListenAddr, "localhost:8080")
+ conf.Partners = config.StrSlice("GOS_PARTNERS", conf.Partners)
+ conf.APIKey = config.Str("GOS_API_KEY", conf.APIKey)
+ conf.DataDir = config.Str("GOS_DATA_DIR", conf.DataDir, "data")
+ conf.EmailTo = config.Str("GOS_EMAIL_TO", conf.EmailTo)
+ conf.EmailFrom = config.Str("GOS_EMAIL_FROM", conf.EmailFrom)
+
+ conf.SMTPServer = config.Str("GOS_SMTP_SERVER", conf.SMTPServer, func() string {
hostname, err := os.Hostname()
if err != nil {
log.Fatal(err)
@@ -51,8 +51,8 @@ func New(configFile, secretsFile string) (ServerConfig, error) {
})
const oneHour = 3600
- conf.MergeIntervalS = config.Env[config.Int]("GOS_MERGE_INTERVAL", oneHour)
- conf.ScheduleIntervalS = config.Env[config.Int]("GOS_SCHEDULER_INTERVAL", oneHour*6)
+ conf.MergeIntervalS = config.Int("GOS_MERGE_INTERVAL", oneHour)
+ conf.ScheduleIntervalS = config.Int("GOS_SCHEDULER_INTERVAL", oneHour*6)
return conf, nil
}