forked from yosisa/fluxion
/
tag.go
44 lines (37 loc) · 827 Bytes
/
tag.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package engine
import (
"regexp"
"strings"
"github.com/yosisa/fluxion/message"
)
type Emitter interface {
Emit(*message.Event) error
}
type TagRouter struct {
patterns []*regexp.Regexp
values []Emitter
}
func (t *TagRouter) Add(re *regexp.Regexp, e Emitter) {
t.patterns = append(t.patterns, re)
t.values = append(t.values, e)
}
func (t *TagRouter) Route(tag string) Emitter {
for i, re := range t.patterns {
if re.MatchString(tag) {
return t.values[i]
}
}
return nil
}
func compileTag(s string) (*regexp.Regexp, error) {
if s == "**" || s == "*" {
s = `.*`
} else {
s = strings.Replace(s, `.`, `\.`, -1)
s = strings.Replace(s, `**`, `(\..+|)`, -1)
s = strings.Replace(s, `\.(\..+|)`, `(\..+|)`, -1)
s = strings.Replace(s, `*`, `.*`, -1)
}
s = `^` + s + `$`
return regexp.Compile(s)
}