minor improvements; matrix: use message parts for setting up action pattern

This commit is contained in:
Helmut Merz 2023-07-25 14:51:42 +02:00
parent c5f1fe6e81
commit d0f2fbc346
5 changed files with 18 additions and 7 deletions

View file

@ -30,7 +30,7 @@ func (cfg *defcfg) Actions() []lib.ActionConfig {
} }
func (cfg *defcfg) AddAction(pattern string, specs ...lib.ActionSpec) { func (cfg *defcfg) AddAction(pattern string, specs ...lib.ActionSpec) {
act := ActionConfig(pattern, specs) act := actionConfig(pattern, specs)
cfg.actions = append(cfg.actions, act) cfg.actions = append(cfg.actions, act)
} }
@ -142,7 +142,7 @@ func (act *action) Specs() []lib.ActionSpec {
return act.specs return act.specs
} }
func ActionConfig(pat string, specs []lib.ActionSpec) *action { func actionConfig(pat string, specs []lib.ActionSpec) *action {
return &action{ return &action{
pattern: pattern(strings.Split(pat, "|")), pattern: pattern(strings.Split(pat, "|")),
specs: specs, specs: specs,
@ -159,6 +159,10 @@ func (p pattern) Slice() []string {
return []string(p) return []string(p)
} }
func Pattern(p ...string) lib.Pattern {
return pattern(p)
}
// overridable settings // overridable settings
type Settings map[string]string type Settings map[string]string

View file

@ -22,6 +22,11 @@ func (spec *baseSpec) Receivers() []lib.Addressable {
} }
func (spec *baseSpec) AddReceiver(rcv lib.Addressable) { func (spec *baseSpec) AddReceiver(rcv lib.Addressable) {
for _, r := range spec.receivers {
if r == rcv {
return
}
}
spec.receivers = append(spec.receivers, rcv) spec.receivers = append(spec.receivers, rcv)
} }

View file

@ -31,7 +31,7 @@ func createCell(ctx lib.Context) lib.Context {
func connect(src, tgt lib.Context, msg lib.Message) { func connect(src, tgt lib.Context, msg lib.Message) {
cfg := src.Config() cfg := src.Config()
pattern := "matrix|data" pattern := config.Pattern(msg.Domain(), "data", msg.Class(), msg.Item()).String()
for _, act := range cfg.Actions() { for _, act := range cfg.Actions() {
if act.Pattern().String() == pattern { if act.Pattern().String() == pattern {
for _, spec := range act.Specs() { for _, spec := range act.Specs() {

View file

@ -23,7 +23,7 @@ func ConfigMx() lib.Config {
app_c.AddAction("demo", action.Base(action.Forward, "test-receiver")) app_c.AddAction("demo", action.Base(action.Forward, "test-receiver"))
test_rcvr := config.Default("test-receiver") test_rcvr := config.Default("test-receiver")
test_rcvr.AddAction("demo", action.Base(AH_MxReceiver)) test_rcvr.AddAction("data", action.Base(AH_MxReceiver))
cell_0 := matrix.DefaultConfig("cell-0") cell_0 := matrix.DefaultConfig("cell-0")

View file

@ -1,4 +1,4 @@
package scopes package scopes_test
import ( import (
"os" "os"
@ -16,7 +16,7 @@ func TestMatrixApp(tb *tbase.T) {
t := testing.SetUpApp(tb, etc.ConfigMx()) t := testing.SetUpApp(tb, etc.ConfigMx())
t.Run("app-matrix", MxTest) t.Run("app-matrix", MxTest)
t.TearDownApp("matrix") t.TearDownApp("matrix")
t.AssertEqual(t.LogCheck(logfile, true), 7) t.AssertEqual(t.LogCheck(logfile, true), 13)
} }
func MxTest(t *testing.T) { func MxTest(t *testing.T) {
@ -26,13 +26,15 @@ func MxTest(t *testing.T) {
rcvr := ctx.Services()["test-receiver"] rcvr := ctx.Services()["test-receiver"]
msg := message.New("matrix", "create").WithSender(rcvr) msg := message.New("matrix", "create").WithSender(rcvr)
c0.Send(msg) c0.Send(msg)
msg = message.New("matrix", "data")
c0.Send(msg)
} }
// action handlers // action handlers
func MxReceiver(act lib.Action) bool { func MxReceiver(act lib.Action) bool {
t := testing.GetT(act.Context()) t := testing.GetT(act.Context())
t.AssertEqual(act.Message().Action(), "demo") t.AssertEqual(act.Message().Action(), "data")
return true return true
} }