From 99bc92794f1244f8f2e5cbb7af6bfa595e9d2dd1 Mon Sep 17 00:00:00 2001 From: Maxim Lebedev Date: Sat, 25 Sep 2021 20:21:47 +0500 Subject: [PATCH] :recycle: Refactored config package --- internal/config/repository.go | 1 + .../config/repository/viper/viper_config.go | 30 +++------ .../repository/viper/viper_config_test.go | 36 +++++++++++ internal/config/usecase.go | 10 +-- internal/config/usecase/config_ucase.go | 37 +++++++++++ internal/config/usecase/config_ucase_test.go | 62 +++++++++++++++++++ internal/config/usecase/config_usecase.go | 37 ----------- 7 files changed, 148 insertions(+), 65 deletions(-) create mode 100644 internal/config/repository/viper/viper_config_test.go create mode 100644 internal/config/usecase/config_ucase.go create mode 100644 internal/config/usecase/config_ucase_test.go delete mode 100644 internal/config/usecase/config_usecase.go diff --git a/internal/config/repository.go b/internal/config/repository.go index 6080258..7eed865 100644 --- a/internal/config/repository.go +++ b/internal/config/repository.go @@ -1,5 +1,6 @@ package config type Repository interface { + GetInt(key string) int GetString(key string) string } diff --git a/internal/config/repository/viper/viper_config.go b/internal/config/repository/viper/viper_config.go index fc305a0..9339d85 100644 --- a/internal/config/repository/viper/viper_config.go +++ b/internal/config/repository/viper/viper_config.go @@ -1,40 +1,24 @@ package viper import ( - "path/filepath" - "github.com/spf13/viper" - "gitlab.com/toby3d/indieauth/internal/config" + "source.toby3d.me/website/oauth/internal/config" ) type viperConfigRepository struct { viper *viper.Viper } -func NewViperConfigRepository(v *viper.Viper) (config.Repository, error) { - v.AddConfigPath(filepath.Join(".", "configs")) - v.AddConfigPath(".") - v.SetConfigName("config") - v.SetConfigType("yaml") - - for key, value := range map[string]interface{}{ - "database.client": "bolt", - "database.connection.filename": "data/development.db", - "server.port": 3000, - "url": "http://127.0.0.1:3000/", - } { - v.SetDefault(key, value) - } - - if err := v.ReadInConfig(); err != nil { - return nil, err - } - +func NewViperConfigRepository(v *viper.Viper) config.Repository { return &viperConfigRepository{ viper: v, - }, nil + } } func (v *viperConfigRepository) GetString(key string) string { return v.viper.GetString(key) } + +func (v *viperConfigRepository) GetInt(key string) int { + return v.viper.GetInt(key) +} diff --git a/internal/config/repository/viper/viper_config_test.go b/internal/config/repository/viper/viper_config_test.go new file mode 100644 index 0000000..c52d306 --- /dev/null +++ b/internal/config/repository/viper/viper_config_test.go @@ -0,0 +1,36 @@ +package viper_test + +import ( + "testing" + + "github.com/spf13/viper" + "github.com/stretchr/testify/assert" + repository "source.toby3d.me/website/oauth/internal/config/repository/viper" +) + +func TestGetString(t *testing.T) { + v := viper.New() + + v.MergeConfigMap(map[string]interface{}{ + "testing": map[string]interface{}{ + "sample": "text", + "answer": 42, + }, + }) + + repo := repository.NewViperConfigRepository(v) + assert.Equal(t, "text", repo.GetString("testing.sample")) + assert.Equal(t, "42", repo.GetString("testing.answer")) +} + +func TestGetInt(t *testing.T) { + v := viper.New() + + v.MergeConfigMap(map[string]interface{}{ + "testing": map[string]interface{}{ + "answer": 42, + }, + }) + + assert.Equal(t, 42, repository.NewViperConfigRepository(v).GetInt("testing.answer")) +} diff --git a/internal/config/usecase.go b/internal/config/usecase.go index a97954b..67855c0 100644 --- a/internal/config/usecase.go +++ b/internal/config/usecase.go @@ -1,9 +1,9 @@ package config type UseCase interface { - GetURL() string - GetHost() string - GetPort() string - GetAddr() string - GetDatabaseFileName() string + Addr() string + DBFileName() string + Host() string + Port() int + URL() string } diff --git a/internal/config/usecase/config_ucase.go b/internal/config/usecase/config_ucase.go new file mode 100644 index 0000000..d18f8c4 --- /dev/null +++ b/internal/config/usecase/config_ucase.go @@ -0,0 +1,37 @@ +package usecase + +import ( + "net" + + "source.toby3d.me/website/oauth/internal/config" +) + +type configUseCase struct { + repo config.Repository +} + +func NewConfigUseCase(repo config.Repository) config.UseCase { + return &configUseCase{ + repo: repo, + } +} + +func (useCase *configUseCase) URL() string { + return useCase.repo.GetString("url") +} + +func (useCase *configUseCase) Host() string { + return useCase.repo.GetString("server.host") +} + +func (useCase *configUseCase) Port() int { + return useCase.repo.GetInt("server.port") +} + +func (useCase *configUseCase) Addr() string { + return net.JoinHostPort(useCase.repo.GetString("server.host"), useCase.repo.GetString("server.port")) +} + +func (useCase *configUseCase) DBFileName() string { + return useCase.repo.GetString("database.connection.filename") +} diff --git a/internal/config/usecase/config_ucase_test.go b/internal/config/usecase/config_ucase_test.go new file mode 100644 index 0000000..d2c83b0 --- /dev/null +++ b/internal/config/usecase/config_ucase_test.go @@ -0,0 +1,62 @@ +package usecase_test + +import ( + "os" + "testing" + + "github.com/spf13/viper" + "github.com/stretchr/testify/assert" + "source.toby3d.me/website/oauth/internal/config" + repository "source.toby3d.me/website/oauth/internal/config/repository/viper" + "source.toby3d.me/website/oauth/internal/config/usecase" +) + +var ucase config.UseCase + +func TestMain(m *testing.M) { + v := viper.New() + + for key, val := range map[string]interface{}{ + "database.client": "bolt", + "database.connection.filename": "./data/development.db", + "server.host": "127.0.0.1", + "server.port": 3000, + "url": "http://127.0.0.1:3000/", + } { + v.Set(key, val) + } + + ucase = usecase.NewConfigUseCase(repository.NewViperConfigRepository(v)) + + os.Exit(m.Run()) +} + +func TestAddr(t *testing.T) { + t.Parallel() + + assert.Equal(t, "127.0.0.1:3000", ucase.Addr()) +} + +func TestDBFileName(t *testing.T) { + t.Parallel() + + assert.Equal(t, "./data/development.db", ucase.DBFileName()) +} + +func TestHost(t *testing.T) { + t.Parallel() + + assert.Equal(t, "127.0.0.1", ucase.Host()) +} + +func TestPort(t *testing.T) { + t.Parallel() + + assert.Equal(t, 3000, ucase.Port()) +} + +func TestURL(t *testing.T) { + t.Parallel() + + assert.Equal(t, "http://127.0.0.1:3000/", ucase.URL()) +} diff --git a/internal/config/usecase/config_usecase.go b/internal/config/usecase/config_usecase.go deleted file mode 100644 index b71a7de..0000000 --- a/internal/config/usecase/config_usecase.go +++ /dev/null @@ -1,37 +0,0 @@ -package usecase - -import ( - "net" - - "gitlab.com/toby3d/indieauth/internal/config" -) - -type configUseCase struct { - repo config.Repository -} - -func NewConfigUseCase(repo config.Repository) config.UseCase { - return &configUseCase{ - repo: repo, - } -} - -func (useCase *configUseCase) GetURL() string { - return useCase.repo.GetString("url") -} - -func (useCase *configUseCase) GetHost() string { - return useCase.repo.GetString("server.host") -} - -func (useCase *configUseCase) GetPort() string { - return useCase.repo.GetString("server.port") -} - -func (useCase *configUseCase) GetAddr() string { - return net.JoinHostPort(useCase.GetHost(), useCase.GetPort()) -} - -func (useCase *configUseCase) GetDatabaseFileName() string { - return useCase.repo.GetString("database.connection.filename") -}