From 5e60c3e10bcd9847ab95bf0772b8e72c96d65a2f Mon Sep 17 00:00:00 2001 From: Helmut Merz Date: Wed, 7 Jun 2023 13:41:35 +0200 Subject: [PATCH] add basic action specification --- config/config.go | 24 ++++++++++++------------ lib/action/action.go | 20 ++++++++++++++++++++ tests/etc/etc.go | 7 +++---- tests/scopes_test.go | 1 + 4 files changed, 36 insertions(+), 16 deletions(-) create mode 100644 lib/action/action.go diff --git a/config/config.go b/config/config.go index fbdc7d9..9ac9b97 100644 --- a/config/config.go +++ b/config/config.go @@ -63,13 +63,6 @@ func (cfg *Base) Add(child lib.Config) { cfg.children = append(cfg.children, child) } -func (cfg *Base) WithAction(pattern string, spec lib.ActionSpec) lib.Config { - act := ActionConfig(pattern, nil) - act.specs = append(act.specs, spec) - cfg.actions = append(cfg.actions, act) - return cfg -} - // implementation-specific methods and functions func (cfg *Base) WithStep(step lib.StepProc) *Base { @@ -82,6 +75,13 @@ func (cfg *Base) WithMessageHandler(hdlr lib.MessageHandler) *Base { return cfg } +func (cfg *Base) WithAction(pattern string, spec lib.ActionSpec) lib.Config { + act := ActionConfig(pattern, nil) + act.specs = append(act.specs, spec) + cfg.actions = append(cfg.actions, act) + return cfg +} + func MakeBase(name string, starter lib.StartProc) *Base { return &Base{ name: name, @@ -92,21 +92,21 @@ func MakeBase(name string, starter lib.StartProc) *Base { // action configuration -type ActBase struct { +type action struct { pattern string specs []lib.ActionSpec } -func (act *ActBase) Pattern() string { +func (act *action) Pattern() string { return act.pattern } -func (act *ActBase) Specs() []lib.ActionSpec { +func (act *action) Specs() []lib.ActionSpec { return act.specs } -func ActionConfig(pattern string, specs []lib.ActionSpec) *ActBase { - return &ActBase{pattern, specs} +func ActionConfig(pattern string, specs []lib.ActionSpec) *action { + return &action{pattern, specs} } // overridable settings diff --git a/lib/action/action.go b/lib/action/action.go new file mode 100644 index 0000000..008d1c7 --- /dev/null +++ b/lib/action/action.go @@ -0,0 +1,20 @@ +package action + +import "git.sr.ht/~cco/go-scopes/lib" + +type baseSpec struct { + handler lib.ActionHandler + receivers []string +} + +func (spec *baseSpec) Handler() lib.ActionHandler { + return spec.handler +} + +func (spec *baseSpec) Receivers() []string { + return spec.receivers +} + +func BaseSpec(hdlr lib.ActionHandler, rcvrs []string) *baseSpec { + return &baseSpec{hdlr, rcvrs} +} diff --git a/tests/etc/etc.go b/tests/etc/etc.go index d78e42f..12d07fb 100644 --- a/tests/etc/etc.go +++ b/tests/etc/etc.go @@ -4,6 +4,7 @@ import ( "git.sr.ht/~cco/go-scopes/app" "git.sr.ht/~cco/go-scopes/config" "git.sr.ht/~cco/go-scopes/lib" + "git.sr.ht/~cco/go-scopes/lib/action" "git.sr.ht/~cco/go-scopes/lib/core" "git.sr.ht/~cco/go-scopes/testing" ) @@ -21,10 +22,8 @@ func Config() lib.Config { Base: b("config", config.Start), ConfigFormat: "etc", }) - cfg.Add(b("test-receiver", core.Start)) - // WithAction("*", action.Base{ - // Handler: AH_Receiver, - // } + cfg.Add(b("test-receiver", core.Start). + WithAction("*", action.BaseSpec(AH_Receiver, nil))) return &cfg } diff --git a/tests/scopes_test.go b/tests/scopes_test.go index 7ef1ce8..1a83798 100644 --- a/tests/scopes_test.go +++ b/tests/scopes_test.go @@ -44,6 +44,7 @@ func SendTest(t *testing.T) { // action handlers func Receiver(act lib.Action) bool { + // t := testing.GetT(act.Context()) t := act.Context().Parent().State().(*testing.T) t.AssertEqual(act.Message().Action(), "demo") return true