Archived
1
0
Fork 0
This repository has been archived on 2024-12-18. You can view files and clone it, but cannot push or open issues or pull requests.
restockbot/filter_include.go
Julien Riou 27d9f4976e
style: fix typo in filter_include.go
Signed-off-by: Julien Riou <julien@riou.xyz>
2021-05-20 17:11:12 +02:00

42 lines
1,011 B
Go

package main
import (
"regexp"
log "github.com/sirupsen/logrus"
)
// IncludeFilter struct to store the compiled regex used for the inclusion
type IncludeFilter struct {
regex *regexp.Regexp
}
// NewIncludeFilter to create an IncludeFilter
func NewIncludeFilter(regex string) (*IncludeFilter, error) {
var err error
var compiledRegex *regexp.Regexp
log.Debugf("compiling include filter regex")
if regex != "" {
compiledRegex, err = regexp.Compile(regex)
if err != nil {
return nil, err
}
}
return &IncludeFilter{regex: compiledRegex}, nil
}
// Include returns true when the product name matches the regex
// implements the Filter interface
func (f *IncludeFilter) Include(product *Product) bool {
if f.regex == nil {
return true
}
if f.regex.MatchString(product.Name) {
log.Debugf("product %s included because it matches the include regex", product.Name)
return true
}
log.Debugf("product %s excluded because it doesn't match the include regex", product.Name)
return false
}