combluotion/store/factory.go
Dan Jones 6f06adc37d ♻️ Refactor config
Make it easier to setup stores
2025-01-26 20:07:45 -06:00

43 lines
719 B
Go

package store
import (
"errors"
"fmt"
"codeberg.org/danjones000/combluotion/config"
)
var ErrNoFactory = errors.New("unknown factory")
type StoreFactory func(config.Store) (Store, error)
var factories map[string]StoreFactory
func init() {
factories = make(map[string]StoreFactory)
}
func AddFactory(name string, f StoreFactory) {
factories[name] = f
}
func GetFactory(name string) StoreFactory {
return factories[name]
}
func MakeStore(name string, conf config.Config) (Store, error) {
st, err := conf.Store(name)
if err != nil {
return nil, err
}
if name == "" {
name = st.Name()
}
f, ok := factories[name]
if !ok {
return nil, fmt.Errorf("%w: %s", ErrNoFactory, name)
}
return f(st)
}