diff --git a/config/config.go b/config/config.go index 1b943b7..7e601a0 100644 --- a/config/config.go +++ b/config/config.go @@ -7,36 +7,73 @@ import ( lib "git.sr.ht/~cco/go-scopes" ) -type Cfg struct { - *BaseCfg - ConfigFormat string +// default config + +type DefaultCfg = defcfg + +type defcfg struct { + name string + actions []lib.ActionConfig + children []lib.Config } -func Start(ctx lib.Context) { +func (cfg *defcfg) Name() string { + return cfg.name } -// definitions +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 { - name string + *defcfg 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 { - return cfg.name -} - -func (cfg *base) SetName(n string) { - cfg.name = n } func (cfg *base) Starter() lib.StartProc { @@ -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{ - name: name, + 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 diff --git a/core/core.go b/core/core.go index ce418ad..aefbc93 100644 --- a/core/core.go +++ b/core/core.go @@ -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 } diff --git a/examples/demo/etc/etc.go b/examples/demo/etc/etc.go index 631e70b..c97897b 100644 --- a/examples/demo/etc/etc.go +++ b/examples/demo/etc/etc.go @@ -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 } diff --git a/matrix/matrix.go b/matrix/matrix.go index b1e0919..c899bfd 100644 --- a/matrix/matrix.go +++ b/matrix/matrix.go @@ -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 diff --git a/scopes.go b/scopes.go index 64a0291..63f9723 100644 --- a/scopes.go +++ b/scopes.go @@ -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 { diff --git a/tests/core_test.go b/tests/core_test.go index ce86a45..a25d2e1 100644 --- a/tests/core_test.go +++ b/tests/core_test.go @@ -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") diff --git a/tests/etc/etc.go b/tests/etc/etc.go index 3396982..ceaaa17 100644 --- a/tests/etc/etc.go +++ b/tests/etc/etc.go @@ -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 } diff --git a/tests/etc/etc_mx.go b/tests/etc/etc_mx.go index 07efd7b..70dcfba 100644 --- a/tests/etc/etc_mx.go +++ b/tests/etc/etc_mx.go @@ -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) diff --git a/tests/matrix_test.go b/tests/matrix_test.go index 855c7c1..979c8a4 100644 --- a/tests/matrix_test.go +++ b/tests/matrix_test.go @@ -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 diff --git a/tests/scopes_test.go b/tests/scopes_test.go index 2af5e6a..6a5e281 100644 --- a/tests/scopes_test.go +++ b/tests/scopes_test.go @@ -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) {