♻️ Refactor config
Make it easier to setup stores
This commit is contained in:
parent
ecae0d5f83
commit
6f06adc37d
12 changed files with 623 additions and 81 deletions
|
|
@ -9,7 +9,7 @@ import (
|
|||
|
||||
var ErrNoFactory = errors.New("unknown factory")
|
||||
|
||||
type StoreFactory func(config.Config) (Store, error)
|
||||
type StoreFactory func(config.Store) (Store, error)
|
||||
|
||||
var factories map[string]StoreFactory
|
||||
|
||||
|
|
@ -26,13 +26,18 @@ func GetFactory(name string) StoreFactory {
|
|||
}
|
||||
|
||||
func MakeStore(name string, conf config.Config) (Store, error) {
|
||||
st, err := conf.Store(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if name == "" {
|
||||
name = conf.Conn.Store
|
||||
name = st.Name()
|
||||
}
|
||||
|
||||
f, ok := factories[name]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("%w: %s", ErrNoFactory, name)
|
||||
}
|
||||
return f(conf)
|
||||
return f(st)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,20 +7,31 @@ import (
|
|||
"go.uber.org/mock/gomock"
|
||||
|
||||
"codeberg.org/danjones000/combluotion/config"
|
||||
confMock "codeberg.org/danjones000/combluotion/internal/testmocks/config"
|
||||
storeMock "codeberg.org/danjones000/combluotion/internal/testmocks/store"
|
||||
)
|
||||
|
||||
func getStoreFactory(t *testing.T) (*gomock.Controller, StoreFactory) {
|
||||
type testHelp struct {
|
||||
ctrl *gomock.Controller
|
||||
store *storeMock.MockStore
|
||||
fact StoreFactory
|
||||
conf *confMock.MockConfig
|
||||
}
|
||||
|
||||
func setupFactoryTest(t *testing.T) testHelp {
|
||||
t.Helper()
|
||||
ctrl := gomock.NewController(t)
|
||||
return ctrl, func(config.Config) (Store, error) {
|
||||
return storeMock.NewMockStore(ctrl), nil
|
||||
store := storeMock.NewMockStore(ctrl)
|
||||
fact := func(config.Store) (Store, error) {
|
||||
return store, nil
|
||||
}
|
||||
|
||||
return testHelp{ctrl, store, fact, confMock.NewMockConfig(ctrl)}
|
||||
}
|
||||
|
||||
func TestAddFactory(t *testing.T) {
|
||||
_, f := getStoreFactory(t)
|
||||
AddFactory("mock", f)
|
||||
th := setupFactoryTest(t)
|
||||
AddFactory("mock", th.fact)
|
||||
defer delete(factories, "mock")
|
||||
_, ok := factories["mock"]
|
||||
assert.True(t, ok)
|
||||
|
|
@ -32,36 +43,52 @@ func TestGetFactoryNil(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestGetFactoryNotNil(t *testing.T) {
|
||||
_, f := getStoreFactory(t)
|
||||
AddFactory("mock", f)
|
||||
th := setupFactoryTest(t)
|
||||
AddFactory("mock", th.fact)
|
||||
defer delete(factories, "mock")
|
||||
f = GetFactory("mock")
|
||||
f := GetFactory("mock")
|
||||
assert.NotNil(t, f)
|
||||
}
|
||||
|
||||
func TestMakeStoreError(t *testing.T) {
|
||||
s, e := MakeStore("mock", config.Config{})
|
||||
th := setupFactoryTest(t)
|
||||
th.conf.
|
||||
EXPECT().
|
||||
Store("mock").
|
||||
Return(nil, nil)
|
||||
s, e := MakeStore("mock", th.conf)
|
||||
assert.Nil(t, s)
|
||||
assert.ErrorIs(t, e, ErrNoFactory)
|
||||
assert.ErrorContains(t, e, ErrNoFactory.Error()+": mock")
|
||||
}
|
||||
|
||||
func TestMakeStoreNoError(t *testing.T) {
|
||||
_, f := getStoreFactory(t)
|
||||
AddFactory("mock", f)
|
||||
th := setupFactoryTest(t)
|
||||
th.conf.
|
||||
EXPECT().
|
||||
Store("mock").
|
||||
Return(nil, nil)
|
||||
AddFactory("mock", th.fact)
|
||||
defer delete(factories, "mock")
|
||||
s, e := MakeStore("mock", config.Config{})
|
||||
s, e := MakeStore("mock", th.conf)
|
||||
assert.NotNil(t, s)
|
||||
assert.NoError(t, e)
|
||||
}
|
||||
|
||||
func TestMakeStoreNoName(t *testing.T) {
|
||||
_, f := getStoreFactory(t)
|
||||
AddFactory("mock", f)
|
||||
th := setupFactoryTest(t)
|
||||
confStore := confMock.NewMockStore(th.ctrl)
|
||||
th.conf.
|
||||
EXPECT().
|
||||
Store("").
|
||||
Return(confStore, nil)
|
||||
confStore.
|
||||
EXPECT().
|
||||
Name().
|
||||
Return("mock")
|
||||
AddFactory("mock", th.fact)
|
||||
defer delete(factories, "mock")
|
||||
s, e := MakeStore("", config.Config{
|
||||
Conn: config.ConnSettings{Store: "mock"},
|
||||
})
|
||||
s, e := MakeStore("", th.conf)
|
||||
assert.NotNil(t, s)
|
||||
assert.NoError(t, e)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,8 +10,17 @@ func init() {
|
|||
store.AddFactory("sqlite", MakeStore)
|
||||
}
|
||||
|
||||
func MakeStore(conf config.Config) (store.Store, error) {
|
||||
sqlConf := sqlite.Config{Path: conf.Conn.DSN}
|
||||
type settings struct {
|
||||
Path string
|
||||
}
|
||||
|
||||
func MakeStore(conf config.Store) (store.Store, error) {
|
||||
var s settings
|
||||
err := conf.Decode(&s)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sqlConf := sqlite.Config{Path: s.Path}
|
||||
db, err := sqlite.New(sqlConf)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue