♻️ 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
|
|
@ -1,18 +1,76 @@
|
|||
package config
|
||||
|
||||
type Config struct {
|
||||
Name string `toml:"name"`
|
||||
Env Env `toml:"env"`
|
||||
BaseURL string `toml:"base_url"`
|
||||
Conn ConnSettings `toml:"conn"`
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
)
|
||||
|
||||
type config struct {
|
||||
name string
|
||||
env Env
|
||||
baseURL string
|
||||
stores stores
|
||||
md toml.MetaData
|
||||
}
|
||||
|
||||
type ConnSettings struct {
|
||||
Store string `toml:"store"`
|
||||
DSN string `toml:"dsn"`
|
||||
Settings map[string]any `toml:"settings"`
|
||||
var _ Config = config{}
|
||||
|
||||
func (c config) Name() string {
|
||||
return c.name
|
||||
}
|
||||
|
||||
func (c Config) Environment() Env {
|
||||
return ValidEnvOrDev(c.Env)
|
||||
func (c config) Env() Env {
|
||||
return ValidEnvOrDev(c.env)
|
||||
}
|
||||
|
||||
func (c config) BaseURL() string {
|
||||
return c.baseURL
|
||||
}
|
||||
|
||||
func (c config) Store(name string) (Store, error) {
|
||||
if name == "" {
|
||||
name = c.stores.store
|
||||
}
|
||||
return c.stores.GetStore(name)
|
||||
}
|
||||
|
||||
func (c config) StoreName() string {
|
||||
return c.stores.store
|
||||
}
|
||||
|
||||
type stores struct {
|
||||
store string
|
||||
settings map[string]toml.Primitive
|
||||
conf *config
|
||||
}
|
||||
|
||||
var ErrMissingStore = errors.New("unknown store")
|
||||
|
||||
func (ss stores) GetStore(name string) (Store, error) {
|
||||
if tp, ok := ss.settings[name]; ok {
|
||||
return store{name, tp, ss.conf.md}, nil
|
||||
}
|
||||
return nil, ErrMissingStore
|
||||
}
|
||||
|
||||
type store struct {
|
||||
name string
|
||||
settings toml.Primitive
|
||||
md toml.MetaData
|
||||
}
|
||||
|
||||
var _ Store = store{}
|
||||
|
||||
func (s store) Name() string {
|
||||
return s.name
|
||||
}
|
||||
|
||||
func (s store) Map() (m map[string]any, err error) {
|
||||
err = s.Decode(&m)
|
||||
return
|
||||
}
|
||||
|
||||
func (s store) Decode(v any) error {
|
||||
return s.md.PrimitiveDecode(s.settings, v)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue