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

76 lines
1.2 KiB
Go

package config
import (
"errors"
"github.com/BurntSushi/toml"
)
type config struct {
name string
env Env
baseURL string
stores stores
md toml.MetaData
}
var _ Config = config{}
func (c config) Name() string {
return c.name
}
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)
}