-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrule.go
73 lines (65 loc) · 1.26 KB
/
rule.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
69
70
71
72
73
package main
import (
"regexp"
"strings"
)
type Rule struct {
Exact map[string]string
Suffix map[string]string
Pattern map[string]string
pattern map[*regexp.Regexp]string
}
func (r *Rule) Compile() error {
if r.Exact == nil {
r.Exact = make(map[string]string)
}
if r.Suffix == nil {
r.Suffix = make(map[string]string)
}
if r.Pattern == nil {
r.Pattern = make(map[string]string)
}
if r.pattern == nil {
r.pattern = make(map[*regexp.Regexp]string)
}
for pattern, target := range r.Pattern {
if re, err := regexp.Compile(pattern); err != nil {
return err
} else {
r.pattern[re] = target
}
}
return nil
}
func (r *Rule) MatchExact(name string) *string {
if v, ok := r.Exact[name]; ok {
return &v
}
return nil
}
func (r *Rule) MatchSuffix(name string) *string {
for suffix, v := range r.Suffix {
if strings.HasSuffix(name, suffix) {
return &v
}
}
return nil
}
func (r *Rule) MatchPattern(name string) *string {
for re, v := range r.pattern {
if re.MatchString(name) {
return &v
}
}
return nil
}
func (r *Rule) Match(name string) *string {
if v := r.MatchExact(name); v != nil {
return v
} else if v = r.MatchSuffix(name); v != nil {
return v
} else if v = r.MatchPattern(name); v != nil {
return v
}
return nil
}