diff --git a/common/common.go b/common/common.go index 2d5eed1..df362e4 100644 --- a/common/common.go +++ b/common/common.go @@ -1,6 +1,64 @@ package common +type Config interface { + Name() string + Starter() StartFct + Children() []Config + Add(Config) +} + type Context interface { + Config() Config + Parent() Context + Services() map[string]Context + ChildContext(Config) Context } type StartFct = func(Context) + +// Context implementation + +type context struct { + cfg Config + parent Context +} + +func (ctx *context) Config() Config { + return ctx.cfg +} + +func (ctx *context) Parent() Context { + return ctx.parent +} + +func (ctx *context) Services() map[string]Context { + return ctx.parent.Services() +} + +func (ctx *context) ChildContext(cfg Config) Context { + cctx := context{cfg, ctx} + ctx.Services()[cfg.Name()] = &cctx + return &cctx +} + +type appContext struct { + *context + services map[string]Context +} + +func (ctx *appContext) ChildContext(cfg Config) Context { + cctx := context{cfg, ctx} + ctx.services[cfg.Name()] = &cctx + return &cctx +} + +func (ctx *appContext) Services() map[string]Context { + return ctx.services +} + +func AppContext(cfg Config) Context { + return &appContext{ + context: &context{cfg, nil}, + services: map[string]Context{}, + } +} diff --git a/config/config.go b/config/config.go index b9530aa..fb01064 100644 --- a/config/config.go +++ b/config/config.go @@ -17,17 +17,10 @@ func Start(ctx common.Context) { // definitions -type Config interface { - Name() string - Starter() common.StartFct - Children() []Config - Add(Config) -} - type Base struct { name string starter common.StartFct - children []Config + children []common.Config } func (cfg *Base) Name() string { @@ -38,11 +31,11 @@ func (cfg *Base) Starter() common.StartFct { return cfg.starter } -func (cfg *Base) Children() []Config { +func (cfg *Base) Children() []common.Config { return cfg.children } -func (cfg *Base) Add(child Config) { +func (cfg *Base) Add(child common.Config) { cfg.children = append(cfg.children, child) } @@ -53,7 +46,7 @@ func MakeBase(name string, starter common.StartFct) *Base { } } -func Setup(cfg Config) Config { +func Setup(cfg common.Config) common.Config { return cfg } diff --git a/examples/demo/etc/etc.go b/examples/demo/etc/etc.go index 7d162e4..12d303b 100644 --- a/examples/demo/etc/etc.go +++ b/examples/demo/etc/etc.go @@ -2,10 +2,11 @@ package etc import ( "git.sr.ht/~cco/go-scopes" + "git.sr.ht/~cco/go-scopes/common" "git.sr.ht/~cco/go-scopes/config" ) -func Config() config.Config { +func Config() common.Config { ovr := Overrides().Use b := config.MakeBase cfg := scopes.Cfg{ diff --git a/scopes.go b/scopes.go index f75cd67..7a27e0f 100644 --- a/scopes.go +++ b/scopes.go @@ -12,10 +12,15 @@ type Cfg struct { } func Start(ctx common.Context) { + for _, cfg := range ctx.Config().Children() { + cctx := ctx.ChildContext(cfg) + cfg.Starter()(cctx) + } } // definitions -func RunApp(cfg config.Config) { - _ = cfg +func RunApp(cfg common.Config) { + ctx := common.AppContext(cfg) + cfg.Starter()(ctx) } diff --git a/tests/etc/etc.go b/tests/etc/etc.go index ab14a72..1b2685f 100644 --- a/tests/etc/etc.go +++ b/tests/etc/etc.go @@ -2,10 +2,11 @@ package etc import ( "git.sr.ht/~cco/go-scopes" + "git.sr.ht/~cco/go-scopes/common" "git.sr.ht/~cco/go-scopes/config" ) -func Config() config.Config { +func Config() common.Config { ovr := Overrides().Use b := config.MakeBase cfg := scopes.Cfg{ @@ -20,15 +21,13 @@ func Config() config.Config { // collect here the names of fields that may be overridden via // explicit Override() or SCOPES_* environment settings. const ( - NAME = "name" HOME = "home" ) -// in a production scenario this should be put in separate +// in a production scenario this should be put in a separate // file `settings.go` that should not be stored in code repository. func Overrides() config.Settings { return config.Settings{ - NAME: "overridden", HOME: "/home/scopes", } } diff --git a/tests/scopes_test.go b/tests/scopes_test.go index 8049ae0..610800d 100644 --- a/tests/scopes_test.go +++ b/tests/scopes_test.go @@ -15,7 +15,6 @@ func TestConfig(tb *tbase.T) { } func ConfigTest(t *testing.T) { - t.AssertEqual(etc.Overrides()[etc.NAME], "overridden") cfg := config.Setup(etc.Config()) t.AssertEqual(cfg.Name(), "dummy") appCfg := cfg.(*scopes.Cfg)