From 9797c3515a474efdea2c4fdf9a9de5f8a9e590f9 Mon Sep 17 00:00:00 2001 From: Helmut Merz Date: Sat, 3 Jun 2023 08:41:55 +0200 Subject: [PATCH] services map as own type; function makeCtx; record context children --- common/common.go | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/common/common.go b/common/common.go index 70aeaab..52d8daf 100644 --- a/common/common.go +++ b/common/common.go @@ -7,10 +7,12 @@ type Config interface { Add(Config) } +type Services map[string]Context + type Context interface { Config() Config Parent() Context - Services() map[string]Context + Services() Services ChildContext(Config) Context } @@ -19,8 +21,9 @@ type StartFct = func(Context) // Context implementation type context struct { - cfg Config - parent Context + cfg Config + parent Context + children []Context } func (ctx *context) Config() Config { @@ -31,36 +34,43 @@ func (ctx *context) Parent() Context { return ctx.parent } -func (ctx *context) Services() map[string]Context { +func (ctx *context) Services() Services { return ctx.parent.Services() } func (ctx *context) ChildContext(cfg Config) Context { - cctx := context{cfg, ctx} - ctx.Services()[cfg.Name()] = &cctx - return &cctx + cctx := makeCtx(cfg, ctx) + ctx.Services()[cfg.Name()] = cctx + ctx.children = append(ctx.children, cctx) + return cctx +} + +func makeCtx(cfg Config, parent Context) *context { + ctx := context{cfg, parent, nil} + return &ctx } // top-level application context type appContext struct { *context - services map[string]Context + services Services } func (ctx *appContext) ChildContext(cfg Config) Context { - cctx := context{cfg, ctx} - ctx.services[cfg.Name()] = &cctx - return &cctx + cctx := makeCtx(cfg, ctx) + ctx.services[cfg.Name()] = cctx + ctx.children = append(ctx.children, cctx) + return cctx } -func (ctx *appContext) Services() map[string]Context { +func (ctx *appContext) Services() Services { return ctx.services } func AppContext(cfg Config) Context { return &appContext{ - context: &context{cfg, nil}, - services: map[string]Context{}, + context: makeCtx(cfg, nil), + services: Services{}, } }