combluotion/config/config.go

77 lines
1.2 KiB
Go
Raw Normal View History

2024-09-12 16:40:31 -05:00
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
2024-09-12 16:40:31 -05:00
}
2024-09-13 13:38:30 -05:00
func (s store) Decode(v any) error {
return s.md.PrimitiveDecode(s.settings, v)
2024-09-13 13:38:30 -05:00
}