add basic action specification

This commit is contained in:
Helmut Merz 2023-06-07 13:41:35 +02:00
parent 9ecbf7d7e9
commit 5e60c3e10b
4 changed files with 36 additions and 16 deletions

View file

@ -63,13 +63,6 @@ func (cfg *Base) Add(child lib.Config) {
cfg.children = append(cfg.children, child) 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 // implementation-specific methods and functions
func (cfg *Base) WithStep(step lib.StepProc) *Base { func (cfg *Base) WithStep(step lib.StepProc) *Base {
@ -82,6 +75,13 @@ func (cfg *Base) WithMessageHandler(hdlr lib.MessageHandler) *Base {
return cfg 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 { func MakeBase(name string, starter lib.StartProc) *Base {
return &Base{ return &Base{
name: name, name: name,
@ -92,21 +92,21 @@ func MakeBase(name string, starter lib.StartProc) *Base {
// action configuration // action configuration
type ActBase struct { type action struct {
pattern string pattern string
specs []lib.ActionSpec specs []lib.ActionSpec
} }
func (act *ActBase) Pattern() string { func (act *action) Pattern() string {
return act.pattern return act.pattern
} }
func (act *ActBase) Specs() []lib.ActionSpec { func (act *action) Specs() []lib.ActionSpec {
return act.specs return act.specs
} }
func ActionConfig(pattern string, specs []lib.ActionSpec) *ActBase { func ActionConfig(pattern string, specs []lib.ActionSpec) *action {
return &ActBase{pattern, specs} return &action{pattern, specs}
} }
// overridable settings // overridable settings

20
lib/action/action.go Normal file
View file

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

View file

@ -4,6 +4,7 @@ import (
"git.sr.ht/~cco/go-scopes/app" "git.sr.ht/~cco/go-scopes/app"
"git.sr.ht/~cco/go-scopes/config" "git.sr.ht/~cco/go-scopes/config"
"git.sr.ht/~cco/go-scopes/lib" "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/lib/core"
"git.sr.ht/~cco/go-scopes/testing" "git.sr.ht/~cco/go-scopes/testing"
) )
@ -21,10 +22,8 @@ func Config() lib.Config {
Base: b("config", config.Start), Base: b("config", config.Start),
ConfigFormat: "etc", ConfigFormat: "etc",
}) })
cfg.Add(b("test-receiver", core.Start)) cfg.Add(b("test-receiver", core.Start).
// WithAction("*", action.Base{ WithAction("*", action.BaseSpec(AH_Receiver, nil)))
// Handler: AH_Receiver,
// }
return &cfg return &cfg
} }

View file

@ -44,6 +44,7 @@ func SendTest(t *testing.T) {
// action handlers // action handlers
func Receiver(act lib.Action) bool { func Receiver(act lib.Action) bool {
// t := testing.GetT(act.Context())
t := act.Context().Parent().State().(*testing.T) t := act.Context().Parent().State().(*testing.T)
t.AssertEqual(act.Message().Action(), "demo") t.AssertEqual(act.Message().Action(), "demo")
return true return true