-
Notifications
You must be signed in to change notification settings - Fork 0
/
filters.go
250 lines (201 loc) · 4.54 KB
/
filters.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package main
import (
"fmt"
"regexp"
"strings"
)
type filterer interface {
filter(line string) bool
}
type anyFilter struct{}
func (f *anyFilter) filter(_ string) bool {
return true
}
type blankFilter struct{}
func (f *blankFilter) filter(line string) bool {
return line == ""
}
type matchFilter struct {
Line string `yaml:"line"`
}
func (f *matchFilter) filter(line string) bool {
return line == f.Line
}
type containsFilter struct {
Line string `yaml:"line"`
}
func (f *containsFilter) filter(line string) bool {
return strings.Contains(line, f.Line)
}
type prefixFilter struct {
Prefix string `yaml:"prefix"`
}
func (p *prefixFilter) filter(line string) bool {
return strings.HasPrefix(line, p.Prefix)
}
type suffixFilter struct {
Suffix string `yaml:"suffix"`
}
func (s *suffixFilter) filter(line string) bool {
return strings.HasSuffix(line, s.Suffix)
}
type lengthFilter struct {
Length int `yaml:"length"`
}
func (f *lengthFilter) filter(line string) bool {
return len(line) == f.Length
}
type regexpFilter struct {
Reg *regexp.Regexp `yaml:"pattern"`
}
func (f *regexpFilter) UnmarshalYAML(unmarshal func(interface{}) error) error {
type fu struct {
Pattern string `yaml:"pattern"`
}
u := fu{}
err := unmarshal(&u)
if err != nil {
return err
}
f.Reg, err = regexp.Compile(u.Pattern)
return err
}
func (f *regexpFilter) filter(line string) bool {
return f.Reg.MatchString(line)
}
type multilineFilter struct {
StartFilter filterer `yaml:"start-filter"`
EndFilter filterer `yaml:"end-filter"`
matching bool `yaml:"-"`
}
func (f *multilineFilter) filter(line string) bool {
if f.StartFilter.filter(line) {
f.matching = true
return true
}
if f.EndFilter.filter(line) {
f.matching = false
return true
}
return f.matching
}
func (f *multilineFilter) UnmarshalYAML(unmarshal func(interface{}) error) error {
type fu struct {
StartFilter imap `yaml:"start-filter"`
EndFilter imap `yaml:"end-filter"`
}
u := fu{}
err := unmarshal(&u)
if err != nil {
return err
}
kind, ok := u.StartFilter["kind"]
if !ok {
return fmt.Errorf("no kind for multiline filert start")
}
f.StartFilter, err = unmarshalYAMLFilter(kind, u.StartFilter)
if err != nil {
return err
}
kind, ok = u.EndFilter["kind"]
if !ok {
return fmt.Errorf("no kind for multiline filert end")
}
f.EndFilter, err = unmarshalYAMLFilter(kind, u.EndFilter)
if err != nil {
return err
}
return nil
}
type unionFilter struct {
FilterA filterer `yaml:"filter-a"`
FilterB filterer `yaml:"filter-b"`
}
func (f *unionFilter) filter(line string) bool {
return f.FilterA.filter(line) || f.FilterB.filter(line)
}
func (f *unionFilter) UnmarshalYAML(unmarshal func(interface{}) error) error {
type fu struct {
FilterA imap `yaml:"filter-a"`
FilterB imap `yaml:"filter-b"`
}
u := fu{}
err := unmarshal(&u)
if err != nil {
return err
}
kind, ok := u.FilterA["kind"]
if !ok {
return fmt.Errorf("no kind for union filert a")
}
f.FilterA, err = unmarshalYAMLFilter(kind, u.FilterA)
if err != nil {
return err
}
kind, ok = u.FilterB["kind"]
if !ok {
return fmt.Errorf("no kind for union filert b")
}
f.FilterB, err = unmarshalYAMLFilter(kind, u.FilterB)
if err != nil {
return err
}
return nil
}
type intersectionFilter struct {
FilterA filterer `yaml:"filter-a"`
FilterB filterer `yaml:"filter-b"`
}
func (f *intersectionFilter) filter(line string) bool {
return f.FilterA.filter(line) && f.FilterB.filter(line)
}
func (f *intersectionFilter) UnmarshalYAML(unmarshal func(interface{}) error) error {
type fu struct {
FilterA imap `yaml:"filter-a"`
FilterB imap `yaml:"filter-b"`
}
u := fu{}
err := unmarshal(&u)
if err != nil {
return err
}
kind, ok := u.FilterA["kind"]
if !ok {
return fmt.Errorf("no kind for intersection filert a")
}
f.FilterA, err = unmarshalYAMLFilter(kind, u.FilterA)
if err != nil {
return err
}
kind, ok = u.FilterB["kind"]
if !ok {
return fmt.Errorf("no kind for intersection filert b")
}
f.FilterB, err = unmarshalYAMLFilter(kind, u.FilterB)
if err != nil {
return err
}
return nil
}
type notFilter struct {
Filter filterer `yaml:"filter"`
}
func (f *notFilter) filter(line string) bool {
return !f.Filter.filter(line)
}
func (f *notFilter) UnmarshalYAML(unmarshal func(interface{}) error) error {
type fu struct {
Filter imap `yaml:"filter"`
}
u := fu{}
err := unmarshal(&u)
if err != nil {
return err
}
kind, ok := u.Filter["kind"]
if !ok {
return fmt.Errorf("no kind for not filter")
}
f.Filter, err = unmarshalYAMLFilter(kind, u.Filter)
return err
}