diff --git a/config.go b/config.go index 70dedc1..d5e1b80 100644 --- a/config.go +++ b/config.go @@ -53,11 +53,7 @@ func (c *config) dump() ([]byte, error) { return out.Bytes(), nil } -func (c *config) load(b []byte) (err error) { - return -} - -// TODO: function draft, needs refactor +// TODO: function is too heavy, needs refactor func (c *config) meta(prefix string) []*configField { var ( fields = []*configField{} diff --git a/demo/demo.go b/demo/demo.go index 97a39c8..522760d 100644 --- a/demo/demo.go +++ b/demo/demo.go @@ -1,6 +1,7 @@ package main import ( + "encoding/json" "flag" "github.com/localhots/confection" @@ -33,6 +34,12 @@ func main() { conf := Config{ DatabaseConfig: DatabaseConfig{}, } - manager := confection.New(conf) + manager := confection.New(conf, func(b []byte) interface{} { + var newConf Config + if err := json.Unmarshal(b, &newConf); err != nil { + panic(err) + } + return newConf + }) manager.StartServer() } diff --git a/manager.go b/manager.go index afe878c..d09a277 100644 --- a/manager.go +++ b/manager.go @@ -7,9 +7,10 @@ import ( type ( Manager struct { - mux *sync.Mutex - conf *config - file *configFile + mux *sync.Mutex + conf *config + file *configFile + unmarshaller func([]byte) interface{} } ) @@ -23,7 +24,7 @@ func SetupFlags() { flag.IntVar(&serverPort, "config-port", 5050, "Config manager http port") } -func New(conf interface{}) *Manager { +func New(conf interface{}, unmarshaller func([]byte) interface{}) *Manager { mgr := &Manager{ mux: &sync.Mutex{}, conf: &config{ @@ -32,6 +33,7 @@ func New(conf interface{}) *Manager { file: &configFile{ path: configPath, }, + unmarshaller: unmarshaller, } mgr.bootstrap() @@ -57,10 +59,7 @@ func (m *Manager) bootstrap() { panic(err) } - err = m.conf.load(b) - if err != nil { - panic(err) - } + m.importJson(b) } else { b, err := m.conf.dump() if err != nil { @@ -73,3 +72,7 @@ func (m *Manager) bootstrap() { } } } + +func (m *Manager) importJson(b []byte) { + m.conf.config = m.unmarshaller(b) +}