store pattern as a slice of strings

This commit is contained in:
Helmut Merz 2023-07-07 12:59:35 +02:00
parent 4c174217d1
commit a2a47293a8
5 changed files with 29 additions and 9 deletions

View file

@ -106,11 +106,11 @@ var DefaultDoneHandler lib.StepProc
// action configuration
type action struct {
pattern string
pattern lib.Pattern
specs []lib.ActionSpec
}
func (act *action) Pattern() string {
func (act *action) Pattern() lib.Pattern {
return act.pattern
}
@ -118,8 +118,21 @@ func (act *action) Specs() []lib.ActionSpec {
return act.specs
}
func ActionConfig(pattern string, specs []lib.ActionSpec) *action {
return &action{pattern, specs}
func ActionConfig(pat string, specs []lib.ActionSpec) *action {
return &action{
pattern: pattern(strings.Split(pat, "|")),
specs: specs,
}
}
type pattern []string
func (p pattern) String() string {
return strings.Join(p, "|")
}
func (p pattern) Slice() []string {
return []string(p)
}
// overridable settings

View file

@ -33,7 +33,9 @@ func Select(ctx lib.Context, msg lib.Message) []lib.Action {
if match(ac, msg) {
for _, spec := range ac.Specs() {
act := action{ctx, spec, msg}
logging.DebugA(&act).Str("pattern", ac.Pattern()).Msg("action.Select")
logging.DebugA(&act).
Str("pattern", ac.Pattern().String()).
Msg("action.Select")
acts = append(acts, &act)
}
}
@ -42,7 +44,7 @@ func Select(ctx lib.Context, msg lib.Message) []lib.Action {
}
func match(ac lib.ActionConfig, msg lib.Message) bool {
return ac.Pattern() == msg.Action()
return ac.Pattern().Slice()[0] == msg.Action()
//return false
}

View file

@ -15,8 +15,13 @@ type ActionSpec interface {
Receivers() []string
}
type Pattern interface {
fmt.Stringer
Slice() []string
}
type ActionConfig interface {
Pattern() string
Pattern() Pattern
Specs() []ActionSpec
}

View file

@ -81,7 +81,7 @@ func New(args ...string) lib.Message {
}
func SimpleMessage(action string) lib.Message {
return New("standard", action)
return New("scopes", action)
}
var Quit = SimpleMessage("quit")

View file

@ -45,7 +45,7 @@ func MessageTest(t *testing.T) {
var msg lib.Message = message.SimpleMessage("doit")
t.AssertEqual(msg.Action(), "doit")
t.AssertEqual(msg.Head().Action(), "doit")
t.AssertEqual(fmt.Sprint(msg), "standard/doit//")
t.AssertEqual(fmt.Sprint(msg), "scopes/doit//")
msg = message.New("taskman", "doit", "task", "tsk001")
t.AssertEqual(fmt.Sprint(msg), "taskman/doit/task/tsk001")
}