-
Notifications
You must be signed in to change notification settings - Fork 1
/
matchers.go
68 lines (59 loc) · 1.36 KB
/
matchers.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package server
import (
"fmt"
"regexp"
"strings"
ics "github.com/arran4/golang-ical"
)
var (
defaultMatches = matchGroup{
{
property: ics.ComponentPropertySummary,
expression: regexp.MustCompile(".*"),
},
}
)
type Matcher struct {
Property, Regex string
}
type matcher struct {
property ics.ComponentProperty
expression *regexp.Regexp
}
func (m matcher) Matcher() Matcher {
return Matcher{
Property: string(m.property),
Regex: m.expression.String(),
}
}
type matchGroup []matcher
func parseMatchers(m []string) (matchGroup, error) {
matches := make(matchGroup, 0, len(m))
for i, matchOpt := range m {
parts := strings.Split(matchOpt, "=")
if len(parts) != 2 {
return nil, fmt.Errorf("invalid match parameter %q at index %d, should be <FIELD>=<regexp>", matchOpt, i)
}
expression, err := regexp.Compile(parts[1])
if err != nil {
return nil, fmt.Errorf("bad regexp in match parameter %s at index %d: %w", matchOpt, i, err)
}
matches = append(matches, matcher{
property: ics.ComponentProperty(parts[0]),
expression: expression,
})
}
return matches, nil
}
func (m matchGroup) matches(event *ics.VEvent) bool {
for _, matcher := range m {
property := event.GetProperty(matcher.property)
if property == nil {
return false
}
if matcher.expression.Match([]byte(property.Value)) {
return true
}
}
return false
}