/
match.go
202 lines (178 loc) · 4 KB
/
match.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package node
import (
"regexp"
"strings"
"github.com/beevik/etree"
"github.com/tvarney/sdtdmod/pkg/node/key"
)
// Match defines an interface for matching elements.
type Match interface {
Check(*etree.Element) bool
Serialize() map[string]interface{}
}
// TagMatch is a match which checks the tag against a set of constraints.
type TagMatch struct {
Value string
Regex *regexp.Regexp
Suffix string
Prefix string
}
func (t *TagMatch) Check(element *etree.Element) bool {
if t.Value != "" && element.Tag != t.Value {
return false
}
if t.Regex != nil && !t.Regex.MatchString(element.Tag) {
return false
}
if t.Suffix != "" && !strings.HasSuffix(element.Tag, t.Suffix) {
return false
}
if t.Prefix != "" && !strings.HasPrefix(element.Tag, t.Prefix) {
return false
}
return true
}
func (t *TagMatch) Serialize() map[string]interface{} {
m := map[string]interface{}{
key.Type: key.MatchTag,
}
if t.Value != "" {
m["value"] = t.Value
}
if t.Regex != nil {
m["regex"] = t.Regex.String()
}
if t.Prefix != "" {
m["prefix"] = t.Prefix
}
if t.Suffix != "" {
m["suffix"] = t.Suffix
}
return m
}
// AttrMatch is a match which checks an attribute against a set of constraints.
type AttrMatch struct {
Attribute string
Value string
Prefix string
Suffix string
Regex *regexp.Regexp
}
func (a *AttrMatch) Check(element *etree.Element) bool {
attr := element.SelectAttr(a.Attribute)
if attr == nil {
return false
}
if a.Value != "" && attr.Value != a.Value {
return false
}
if a.Regex != nil && !a.Regex.MatchString(attr.Value) {
return false
}
if a.Suffix != "" && !strings.HasSuffix(attr.Value, a.Suffix) {
return false
}
if a.Prefix != "" && !strings.HasPrefix(attr.Value, a.Prefix) {
return false
}
return true
}
func (a *AttrMatch) Serialize() map[string]interface{} {
m := map[string]interface{}{
"type": key.MatchAttr,
"name": a.Attribute,
}
if a.Value != "" {
m["value"] = a.Value
}
if a.Regex != nil {
m["regex"] = a.Regex.String()
}
if a.Prefix != "" {
m["prefix"] = a.Prefix
}
if a.Suffix != "" {
m["suffix"] = a.Suffix
}
return m
}
// AnyOf is a Match which checks that an element matches at least one of the
// sub-matches.
type AnyOf []Match
func (a AnyOf) Check(element *etree.Element) bool {
for _, m := range a {
if m.Check(element) {
return true
}
}
return false
}
func (a AnyOf) Serialize() map[string]interface{} {
matches := make([]map[string]interface{}, 0, len(a))
for _, m := range a {
matches = append(matches, m.Serialize())
}
return map[string]interface{}{
"type": key.MatchAnyOf,
"matches": matches,
}
}
// OneOf is a Match which checks that an element matches exactly one of the
// sub-matches.
type OneOf []Match
func (o OneOf) Check(element *etree.Element) bool {
matches := 0
for _, m := range o {
if m.Check(element) {
matches++
if matches > 1 {
return false
}
}
}
return matches == 1
}
func (o OneOf) Serialize() map[string]interface{} {
matches := make([]map[string]interface{}, 0, len(o))
for _, m := range o {
matches = append(matches, m.Serialize())
}
return map[string]interface{}{
"type": key.MatchOneOf,
"matches": matches,
}
}
// AllOf is a Match which checks that the element matches all of the
// sub-matches.
type AllOf []Match
func (a AllOf) Check(element *etree.Element) bool {
for _, m := range a {
if !m.Check(element) {
return false
}
}
return true
}
func (a AllOf) Serialize() map[string]interface{} {
matches := make([]map[string]interface{}, 0, len(a))
for _, m := range a {
matches = append(matches, m.Serialize())
}
return map[string]interface{}{
"type": key.MatchAllOf,
"matches": matches,
}
}
// Not is a Match which checks that the element does not match the child match.
type Not struct {
Child Match
}
func (n Not) Check(element *etree.Element) bool {
return !n.Child.Check(element)
}
func (n Not) Serialize() map[string]interface{} {
return map[string]interface{}{
"type": key.MatchNot,
"match": n.Child.Serialize(),
}
}