set up config structure for specifying actions

This commit is contained in:
Helmut Merz 2023-06-07 09:59:08 +02:00
parent 15eacddeed
commit 9ecbf7d7e9
5 changed files with 51 additions and 4 deletions

View file

@ -63,6 +63,13 @@ 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 {
@ -83,6 +90,25 @@ func MakeBase(name string, starter lib.StartProc) *Base {
}
}
// action configuration
type ActBase struct {
pattern string
specs []lib.ActionSpec
}
func (act *ActBase) Pattern() string {
return act.pattern
}
func (act *ActBase) Specs() []lib.ActionSpec {
return act.specs
}
func ActionConfig(pattern string, specs []lib.ActionSpec) *ActBase {
return &ActBase{pattern, specs}
}
// overridable settings
type Settings map[string]string

View file

@ -25,6 +25,7 @@ type Config interface {
Actions() []ActionConfig
Children() []Config
Add(Config)
WithAction(string, ActionSpec) Config
}
type Services map[string]Context

View file

@ -40,7 +40,7 @@ func SetUp(tbase *testing.T) *T {
func SetUpApp(tbase *testing.T, cfg lib.Config) *T {
t := SetUp(tbase)
t.Ctx = context.AppContext(cfg)
t.Ctx = context.AppContext(cfg).WithState(t)
cfg.Starter()(t.Ctx)
time.Sleep(100 * time.Millisecond)
return t

View file

@ -21,12 +21,19 @@ func Config() lib.Config {
Base: b("config", config.Start),
ConfigFormat: "etc",
})
cfg.Add(config.Cfg{
Base: b("test-receiver", core.Start),
})
cfg.Add(b("test-receiver", core.Start))
// WithAction("*", action.Base{
// Handler: AH_Receiver,
// }
return &cfg
}
// register action handlers from ..._test.go here:
var (
AH_Receiver lib.ActionHandler
)
// collect here the names of fields that may be overridden via
// explicit Override() or SCOPES_* environment settings.
const (

View file

@ -4,6 +4,7 @@ import (
tbase "testing"
"git.sr.ht/~cco/go-scopes/app"
"git.sr.ht/~cco/go-scopes/lib"
"git.sr.ht/~cco/go-scopes/lib/message"
"git.sr.ht/~cco/go-scopes/testing"
"git.sr.ht/~cco/go-scopes/tests/etc"
@ -39,3 +40,15 @@ func SendTest(t *testing.T) {
rcvr = message.SimpleAddress("test-receiver")
rcvr.Send(ctx, msg)
}
// action handlers
func Receiver(act lib.Action) bool {
t := act.Context().Parent().State().(*testing.T)
t.AssertEqual(act.Message().Action(), "demo")
return true
}
func init() {
etc.AH_Receiver = Receiver
}