provide minimal default config, + some clean-up and standardization

This commit is contained in:
Helmut Merz 2023-07-24 17:58:56 +02:00
parent 0f054c36e8
commit e21499234b
10 changed files with 82 additions and 72 deletions

View file

@ -7,38 +7,75 @@ import (
lib "git.sr.ht/~cco/go-scopes"
)
type Cfg struct {
*BaseCfg
ConfigFormat string
}
// default config
func Start(ctx lib.Context) {
}
type DefaultCfg = defcfg
// definitions
type BaseCfg = base
type base struct {
type defcfg struct {
name string
starter lib.StartProc
step lib.StepProc
msgHandler lib.MessageHandler
doneHandler lib.StepProc
actions []lib.ActionConfig
children []lib.Config
}
// lib.Config implementation
func (cfg *base) Name() string {
func (cfg *defcfg) Name() string {
return cfg.name
}
func (cfg *base) SetName(n string) {
func (cfg *defcfg) SetName(n string) {
cfg.name = n
}
func (cfg *defcfg) Actions() []lib.ActionConfig {
return cfg.actions
}
func (cfg *defcfg) AddAction(pattern string, specs ...lib.ActionSpec) {
act := ActionConfig(pattern, specs)
cfg.actions = append(cfg.actions, act)
}
func (cfg *defcfg) Children() []lib.Config {
return cfg.children
}
func (cfg *defcfg) Add(c ...lib.Config) {
cfg.children = append(cfg.children, c...)
}
func (cfg *defcfg) Starter() lib.StartProc {
return DefaultStart
}
func (cfg *defcfg) Step() lib.StepProc {
return DefaultStep
}
func (cfg *defcfg) MessageHandler() lib.MessageHandler {
return DefaultMsgHandler
}
func (cfg *defcfg) DoneHandler() lib.StepProc {
return DefaultDoneHandler
}
func Default(name string) lib.Config {
return &defcfg{
name: name,
}
}
// base config
type BaseCfg = base
type base struct {
*defcfg
starter lib.StartProc
step lib.StepProc
msgHandler lib.MessageHandler
doneHandler lib.StepProc
}
func (cfg *base) Starter() lib.StartProc {
return cfg.starter
}
@ -55,19 +92,6 @@ func (cfg *base) DoneHandler() lib.StepProc {
return cfg.doneHandler
}
func (cfg *base) Actions() []lib.ActionConfig {
return cfg.actions
}
func (cfg *base) Children() []lib.Config {
return cfg.children
}
func (cfg *base) Add(c ...lib.Config) lib.Config {
cfg.children = append(cfg.children, c...)
return cfg
}
// implementation-specific methods and functions
func (cfg *base) WithStep(step lib.StepProc) *base {
@ -85,24 +109,20 @@ func (cfg *base) WithDoneHandler(hdlr lib.StepProc) *base {
return cfg
}
func (cfg *base) AddAction(pattern string, specs ...lib.ActionSpec) lib.Config {
act := ActionConfig(pattern, specs)
cfg.actions = append(cfg.actions, act)
return cfg
}
func Base(name string, starter lib.StartProc) *base {
return &base{
defcfg: &defcfg{
name: name,
},
starter: starter,
step: DefaultStep,
msgHandler: DefaultMsgHandler,
doneHandler: DefaultDoneHandler,
actions: nil,
}
}
// will be set by core.init()
var DefaultStart lib.StartProc
var DefaultStep lib.StepProc
var DefaultMsgHandler lib.MessageHandler
var DefaultDoneHandler lib.StepProc

View file

@ -46,7 +46,8 @@ func HandleDone(ctx lib.Context) bool {
}
func init() {
config.DefaultStep = Step // avoid import cycle
config.DefaultStart = Start // avoid import cycle
config.DefaultStep = Step
config.DefaultMsgHandler = HandleMessage
config.DefaultDoneHandler = HandleDone
}

View file

@ -10,6 +10,7 @@ import (
func Config() lib.Config {
ovr := Overrides().Use
b := config.Base
app_c := app.Cfg{
BaseCfg: b("dummy", app.Start),
Home: ovr(".", HOME),
@ -18,10 +19,7 @@ func Config() lib.Config {
Logfile: ovr("log/scopes.log", LOGFILE),
},
}
app_c.Add(config.Cfg{
BaseCfg: b("config", config.Start),
ConfigFormat: "etc",
})
return &app_c
}

View file

@ -3,15 +3,14 @@ package matrix
import (
lib "git.sr.ht/~cco/go-scopes"
"git.sr.ht/~cco/go-scopes/config"
"git.sr.ht/~cco/go-scopes/core"
"git.sr.ht/~cco/go-scopes/core/action"
"git.sr.ht/~cco/go-scopes/core/message"
)
func Start(ctx lib.Context) {
cfg := ctx.Config()
func DefaultConfig(name string) lib.Config {
cfg := config.Default(name)
cfg.AddAction("create", action.Base(create))
core.Start(ctx)
return cfg
}
func create(act lib.Action) bool {
@ -24,7 +23,7 @@ func create(act lib.Action) bool {
}
func createCell(ctx lib.Context) lib.Context {
cfg := config.Base("", core.Start)
cfg := DefaultConfig("")
cctx := ctx.ChildContext(cfg)
cfg.Starter()(cctx)
return cctx

View file

@ -103,9 +103,9 @@ type Config interface {
MessageHandler() MessageHandler
DoneHandler() StepProc
Actions() []ActionConfig
AddAction(string, ...ActionSpec) Config
AddAction(string, ...ActionSpec)
Children() []Config
Add(...Config) Config
Add(...Config)
}
type ActionConfig interface {

View file

@ -71,9 +71,9 @@ func ActionTest(t *testing.T) {
return true
}
}
cfg := config.Base("testing", core.None).
AddAction("start", action.Base(hdlrGen("started"))).
AddAction("scopes|doit|task", action.Base(hdlrGen("done")))
cfg := config.Base("testing", core.None)
cfg.AddAction("start", action.Base(hdlrGen("started")))
cfg.AddAction("scopes|doit|task", action.Base(hdlrGen("done")))
ctx := context.AppContext(cfg)
lib.HandleMsg(ctx, message.SimpleMessage("start"))
t.AssertEqual(result.data, "started")

View file

@ -27,11 +27,6 @@ func Config() lib.Config {
}
app_c.AddAction("demo", action.Base(action.Forward, "test-receiver"))
config_c := &config.Cfg{
BaseCfg: b("config", config.Start),
ConfigFormat: "etc",
}
server_c := &server.Cfg{
BaseCfg: b("server", server.Start),
Port: ovr("8123", SERVER_PORT),
@ -43,10 +38,10 @@ func Config() lib.Config {
}
test_client.AddAction("demo", action.Base(client.Send))
test_rcvr := b("test-receiver", core.Start).
AddAction("demo", action.Base(AH_Receiver))
test_rcvr := b("test-receiver", core.Start)
test_rcvr.AddAction("demo", action.Base(AH_Receiver))
app_c.Add(config_c, server_c, test_client, test_rcvr)
app_c.Add(server_c, test_client, test_rcvr)
return app_c
}

View file

@ -5,7 +5,6 @@ import (
"git.sr.ht/~cco/go-scopes/app"
"git.sr.ht/~cco/go-scopes/common/testing"
"git.sr.ht/~cco/go-scopes/config"
"git.sr.ht/~cco/go-scopes/core"
"git.sr.ht/~cco/go-scopes/core/action"
"git.sr.ht/~cco/go-scopes/logging"
"git.sr.ht/~cco/go-scopes/matrix"
@ -23,10 +22,10 @@ func ConfigMx() lib.Config {
}
app_c.AddAction("demo", action.Base(action.Forward, "test-receiver"))
test_rcvr := b("test-receiver", core.Start).
AddAction("demo", action.Base(AH_MxReceiver))
test_rcvr := config.Default("test-receiver")
test_rcvr.AddAction("demo", action.Base(AH_MxReceiver))
cell_0 := b("cell-0", matrix.Start)
cell_0 := matrix.DefaultConfig("cell-0")
app_c.Add(cell_0, test_rcvr)

View file

@ -22,9 +22,9 @@ func TestMatrixApp(tb *tbase.T) {
func MxTest(t *testing.T) {
ctx := t.Ctx
t.AssertEqual(len(ctx.Services()), 3)
rcvr := message.SimpleAddress("cell-0")
msg := message.SimpleMessage("create")
lib.Send(ctx, rcvr, msg)
rcvr := ctx.Services()["cell-0"]
msg := message.SimpleMessage("create").WithSender(rcvr)
rcvr.Send(msg)
}
// action handlers

View file

@ -24,7 +24,7 @@ func TestScopesApp(tb *tbase.T) {
func AppTest(t *testing.T) {
ctx := t.Ctx
t.AssertEqual(len(ctx.Services()), 5)
t.AssertEqual(len(ctx.Services()), 4)
}
func ConfigTest(t *testing.T) {
@ -32,8 +32,6 @@ func ConfigTest(t *testing.T) {
t.AssertEqual(cfg.Name(), "testing")
appCfg := cfg.(*app.Cfg)
t.AssertEqual(appCfg.Home, ".")
confCtx := t.Ctx.Services()["config"]
t.AssertEqual(confCtx.Config().Name(), "config")
}
func SendTest(t *testing.T) {