Archived
1
0
Fork 0

refactor: move filters out of parser

Filters are now separate structures to include a product or not based
on their own set of properties. For now, include and exclude filters
are supported. They take a regex as an argument and include a product
if the regex matches (or doesn't match) the product name. This commit
will allow us to create new filters on product like on a price range.

Signed-off-by: Julien Riou <julien@riou.xyz>
This commit is contained in:
Julien Riou 2021-05-19 17:43:31 +02:00
commit 244c9f68e7
No known key found for this signature in database
GPG key ID: FF42D23B580C89F7
10 changed files with 212 additions and 206 deletions

40
filter_exclude.go Normal file
View file

@ -0,0 +1,40 @@
package main
import (
"regexp"
log "github.com/sirupsen/logrus"
)
type ExcludeFilter struct {
regex *regexp.Regexp
}
func NewExcludeFilter(regex string) (*ExcludeFilter, error) {
var err error
var compiledRegex *regexp.Regexp
log.Debugf("compiling exclude filter regex")
if regex != "" {
compiledRegex, err = regexp.Compile(regex)
if err != nil {
return nil, err
}
}
return &ExcludeFilter{regex: compiledRegex}, nil
}
// Filter excludes product with name matching the regex
// implements the Filter interface
func (f *ExcludeFilter) Include(product *Product) bool {
if f.regex == nil {
return true
}
if f.regex.MatchString(product.Name) {
log.Debugf("product %s excluded because it matches the exclude regex", product.Name)
return false
}
log.Debugf("product %s included because it doesn't match the exclude regex", product.Name)
return true
}