This repository has been archived by the owner on Nov 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
filter_range.go
86 lines (72 loc) · 2.59 KB
/
filter_range.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
package main
import (
"regexp"
log "github.com/sirupsen/logrus"
)
// DefaultCurrency to fallback when no currency is provided
const DefaultCurrency = "USD"
// RangeFilter to store the pattern to match product model and price limits
type RangeFilter struct {
model *regexp.Regexp
min float64
max float64
currency string
converter *CurrencyConverter
}
// NewRangeFilter to create a RangeFilter
func NewRangeFilter(regex string, min float64, max float64, currency string, converter *CurrencyConverter) (*RangeFilter, error) {
var err error
var compiledRegex *regexp.Regexp
log.Debugf("compiling model filter regex")
if regex != "" {
compiledRegex, err = regexp.Compile(regex)
if err != nil {
return nil, err
}
}
var detectedCurrency string
if currency != "" {
detectedCurrency = currency
} else {
detectedCurrency = DefaultCurrency
}
return &RangeFilter{
model: compiledRegex,
min: min,
max: max,
converter: converter,
currency: detectedCurrency,
}, nil
}
// Include returns false when a product name matches the model regex and price is outside of the range
// implements the Filter interface
func (f *RangeFilter) Include(product *Product) bool {
// include products with a missing model regex
if f.model == nil {
log.Debugf("product %s included because range filter model is missing", product.Name)
return true
}
// include products with a different model
if !f.model.MatchString(product.Name) {
log.Debugf("product %s included because range filter model hasn't been detected in the product name", product.Name)
return true
}
// convert price to the filter currency
convertedPrice, err := f.converter.Convert(product.Price, product.PriceCurrency, f.currency)
if err != nil {
log.Warnf("could not convert price %.2f %s to %s for range filter: %s", product.Price, product.PriceCurrency, f.currency, err)
return true
}
// include prices with unlimited maximum if min is respected
if f.max == 0 && convertedPrice > f.max && f.min <= convertedPrice {
log.Debugf("product %s included because max value is unlimited and converted price of %.2f%s is higher than lower limit of %.2f%s", product.Name, convertedPrice, f.currency, f.min, f.currency)
return true
}
// include prices inside the range
if f.min <= convertedPrice && convertedPrice <= f.max {
log.Debugf("product %s included because range filter model matches and converted price is inside of the range", product.Name)
return true
}
log.Debugf("product %s excluded because range filter model matches and converted price is outside of the range", product.Name)
return false
}