-
Notifications
You must be signed in to change notification settings - Fork 171
/
ddl_regex_matcher.go
52 lines (40 loc) · 1.1 KB
/
ddl_regex_matcher.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
45
46
47
48
49
50
51
52
package matchers
import (
"regexp"
"github.com/juju/errors"
"github.com/moiot/gravity/pkg/core"
"github.com/moiot/gravity/pkg/registry"
)
const DDLRegexMatcherName = "match-ddl-regex"
type DDLRegexMatcher struct {
reg *regexp.Regexp
}
func (m *DDLRegexMatcher) Configure(data interface{}) error {
if arg, ok := data.(string); !ok {
return errors.Errorf("match-ddl-regex only receives a string value, you provided: %v", data)
} else {
reg, err := regexp.Compile(arg)
if err != nil {
return errors.Annotatef(err, "fail to compile regexp %s", arg)
}
m.reg = reg
}
return nil
}
func (m *DDLRegexMatcher) Match(msg *core.Msg) bool {
if msg.Type != core.MsgDDL {
return false
}
return m.reg.Match([]byte(msg.DdlMsg.Statement))
}
type ddlRegexMatcherFactoryType struct {
}
func (f *ddlRegexMatcherFactoryType) Configure(_ string, _ map[string]interface{}) error {
return nil
}
func (f *ddlRegexMatcherFactoryType) NewMatcher() core.IMatcher {
return &DDLRegexMatcher{}
}
func init() {
registry.RegisterPlugin(registry.MatcherPlugin, DDLRegexMatcherName, &ddlRegexMatcherFactoryType{}, true)
}