-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.go
66 lines (54 loc) · 1.31 KB
/
filter.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
package program
import (
"github.com/deweysasser/cumulus/cumulus"
"github.com/rs/zerolog/log"
"regexp"
)
type Filter struct {
Include, Exclude []*regexp.Regexp
}
var NoFilter = &Filter{
Include: []*regexp.Regexp{regexp.MustCompile(".*")},
Exclude: []*regexp.Regexp{},
}
func NewFilter(include, exclude []string) *Filter {
inc := make([]*regexp.Regexp, 0, len(include))
ex := make([]*regexp.Regexp, 0, len(exclude))
for _, i := range include {
r, e := regexp.Compile(i)
if e != nil {
log.Fatal().Err(e).Msg("Failed to parse field include regexp")
}
inc = append(inc, r)
}
for _, i := range exclude {
r, e := regexp.Compile(i)
if e != nil {
log.Fatal().Err(e).Msg("Failed to parse field exclude regexp")
}
ex = append(ex, r)
}
return &Filter{inc, ex}
}
func (f *Filter) Accept(meta cumulus.FieldMeta) bool {
log := log.With().Str("fieldname", meta.Name).Logger()
if len(f.Include) > 0 {
log.Debug().Msg("Has includes")
for _, r := range f.Include {
if r.MatchString(meta.Name) {
return true
}
}
}
if len(f.Exclude) > 0 {
log.Debug().Msg("Has excludes")
for _, r := range f.Exclude {
if r.MatchString(meta.Name) {
return false
}
}
return true
}
log.Debug().Bool("hidden", meta.DefaultHidden).Msg("returning default decision")
return !meta.DefaultHidden
}