services map as own type; function makeCtx; record context children
This commit is contained in:
parent
87ff5d5d93
commit
9797c3515a
1 changed files with 24 additions and 14 deletions
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -21,6 +23,7 @@ type StartFct = func(Context)
|
|||
type context struct {
|
||||
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{},
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue