basic context implementations + start of component instances

This commit is contained in:
Helmut Merz 2023-06-02 12:19:23 +02:00
parent ca37aead92
commit e30052b297
6 changed files with 74 additions and 19 deletions

View file

@ -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{},
}
}

View file

@ -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
}

View file

@ -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{

View file

@ -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)
}

View file

@ -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",
}
}

View file

@ -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)