refactor logging and filters
This commit is contained in:
42
internal/config/matcher.go
Normal file
42
internal/config/matcher.go
Normal file
@ -0,0 +1,42 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type MatcherPredicateType uint8
|
||||
|
||||
const (
|
||||
MatcherPredicateTypeUnknown MatcherPredicateType = iota
|
||||
MatcherPredicateTypePattern
|
||||
MatcherPredicateTypeRegexp
|
||||
)
|
||||
|
||||
func (t *MatcherPredicateType) UnmarshalText(data []byte) error {
|
||||
switch dataStr := strings.ToLower(string(data)); dataStr {
|
||||
case "":
|
||||
*t = MatcherPredicateTypeUnknown
|
||||
case "pattern":
|
||||
*t = MatcherPredicateTypePattern
|
||||
case "regexp":
|
||||
*t = MatcherPredicateTypeRegexp
|
||||
default:
|
||||
return errors.New("unsupported type " + dataStr)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type MatcherPredicate struct {
|
||||
Value string `toml:"value"`
|
||||
Type MatcherPredicateType `toml:"type"`
|
||||
}
|
||||
|
||||
type DimensionMatcher struct {
|
||||
CaseInsensitive bool `toml:"case_insensitive"`
|
||||
|
||||
Length []MatcherPredicate `toml:"length"`
|
||||
Width []MatcherPredicate `toml:"width"`
|
||||
Height []MatcherPredicate `toml:"height"`
|
||||
}
|
||||
92
internal/dimension/matcher.go
Normal file
92
internal/dimension/matcher.go
Normal file
@ -0,0 +1,92 @@
|
||||
package dimension
|
||||
|
||||
import (
|
||||
"git.loyso.art/frx/eway/internal/config"
|
||||
"git.loyso.art/frx/eway/internal/matcher"
|
||||
)
|
||||
|
||||
type MatchResult uint8
|
||||
|
||||
const (
|
||||
MatchResultMiss MatchResult = iota
|
||||
MatchResultLength
|
||||
MatchResultHeight
|
||||
MatchResultWidth
|
||||
MatchResultDepth
|
||||
)
|
||||
|
||||
type Matcher struct {
|
||||
length matcher.Unit
|
||||
width matcher.Unit
|
||||
height matcher.Unit
|
||||
}
|
||||
|
||||
func New(cfg config.DimensionMatcher) *Matcher {
|
||||
return &Matcher{
|
||||
length: makeMatcherByConig(cfg.CaseInsensitive, cfg.Length...),
|
||||
width: makeMatcherByConig(cfg.CaseInsensitive, cfg.Width...),
|
||||
height: makeMatcherByConig(cfg.CaseInsensitive, cfg.Height...),
|
||||
}
|
||||
}
|
||||
|
||||
func makeMatcherByConig(insensitive bool, cfgs ...config.MatcherPredicate) matcher.Unit {
|
||||
opts := make([]matcher.RadixOpt, 0, 1)
|
||||
if insensitive {
|
||||
opts = append(opts, matcher.RadixCaseInsensitive())
|
||||
}
|
||||
m := matcher.NewRadix(opts...)
|
||||
|
||||
for _, cfg := range cfgs {
|
||||
switch cfg.Type {
|
||||
case config.MatcherPredicateTypePattern:
|
||||
m.Register(cfg.Value)
|
||||
case config.MatcherPredicateTypeRegexp:
|
||||
m.RegisterRegexp(cfg.Value)
|
||||
default:
|
||||
panic("unsupported matcher type")
|
||||
}
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
func (m *Matcher) Match(value string) MatchResult {
|
||||
switch {
|
||||
case value == "Высота":
|
||||
fallthrough
|
||||
case m.height.Match(value):
|
||||
return MatchResultHeight
|
||||
case value == "Глубина":
|
||||
return MatchResultDepth
|
||||
case value == "Длина":
|
||||
fallthrough
|
||||
case m.length.Match(value):
|
||||
return MatchResultLength
|
||||
case value == "Ширина":
|
||||
fallthrough
|
||||
case m.width.Match(value):
|
||||
return MatchResultWidth
|
||||
}
|
||||
|
||||
return MatchResultMiss
|
||||
}
|
||||
|
||||
func (m *Matcher) GetRegisteredPatterns() map[string][]string {
|
||||
out := map[string][]string{
|
||||
"length": nil,
|
||||
"width": nil,
|
||||
"height": nil,
|
||||
}
|
||||
|
||||
if m.height != nil {
|
||||
out["height"] = m.height.Patterns()
|
||||
}
|
||||
if m.width != nil {
|
||||
out["width"] = m.width.Patterns()
|
||||
}
|
||||
if m.length != nil {
|
||||
out["length"] = m.length.Patterns()
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
@ -115,7 +115,10 @@ func makeDomainGoodItem(builder *flatbuffers.Builder, in entity.GoodsItem) flatb
|
||||
func ParseGoodsItem(data []byte) (item entity.GoodsItem, err error) {
|
||||
itemFBS := GetRootAsGoodItem(data, 0)
|
||||
item.Articul = string(itemFBS.Sku())
|
||||
item.PhotoURLs = strings.Split(string(itemFBS.Photo()), ";")
|
||||
photoURLs := string(itemFBS.Photo())
|
||||
if len(photoURLs) > 0 {
|
||||
item.PhotoURLs = strings.Split(photoURLs, ";")
|
||||
}
|
||||
item.Name = string(itemFBS.Name())
|
||||
|
||||
description, err := base64.RawStdEncoding.DecodeString(string(itemFBS.Description()))
|
||||
|
||||
@ -12,6 +12,44 @@ type GoodsItemSize struct {
|
||||
Width Dimension
|
||||
Height Dimension
|
||||
Length Dimension
|
||||
|
||||
UnmatchedDepth Dimension
|
||||
}
|
||||
|
||||
func FixupSizes(s GoodsItemSize) (GoodsItemSize, bool) {
|
||||
// Nothing to substitute
|
||||
if s.UnmatchedDepth.IsZero() {
|
||||
return s, false
|
||||
}
|
||||
|
||||
var count int
|
||||
for _, d := range []Dimension{
|
||||
s.Width,
|
||||
s.Height,
|
||||
s.Length,
|
||||
} {
|
||||
if d.IsZero() {
|
||||
count++
|
||||
}
|
||||
}
|
||||
|
||||
// Can only replace one dimension
|
||||
if count != 1 {
|
||||
return s, false
|
||||
}
|
||||
|
||||
switch {
|
||||
case s.Width.IsZero():
|
||||
s.Width = s.UnmatchedDepth
|
||||
case s.Height.IsZero():
|
||||
s.Height = s.UnmatchedDepth
|
||||
case s.Length.IsZero():
|
||||
s.Length = s.UnmatchedDepth
|
||||
}
|
||||
|
||||
s.UnmatchedDepth = Dimension{}
|
||||
|
||||
return s, true
|
||||
}
|
||||
|
||||
func (s GoodsItemSize) GetSum(kind DimensionKind) float64 {
|
||||
|
||||
@ -24,7 +24,7 @@ type Offer struct {
|
||||
Description string `xml:"description"`
|
||||
ManufacturerWarrany bool `xml:"manufacturer_warranty"`
|
||||
Dimensions string `xml:"dimensions"`
|
||||
Params []Param `xml:"param"`
|
||||
Params []Param `xml:"param,omitempty"`
|
||||
}
|
||||
|
||||
type Currency struct {
|
||||
|
||||
@ -355,8 +355,6 @@ func (c *client) getProductInfo(ctx context.Context, cartID int64) (pi entity.Go
|
||||
return strings.Contains(err.Error(), "pipe")
|
||||
})
|
||||
|
||||
c.log.Debug().Msg("using go query")
|
||||
|
||||
pi.Parameters = map[string]string{}
|
||||
resp, err := c.do(ctx, "getProductInfo", req, resty.MethodGet, reqpath)
|
||||
if err != nil {
|
||||
|
||||
@ -71,7 +71,7 @@ func (r *radixMatcher) MatchByPattern(value string) (pattern string) {
|
||||
}
|
||||
|
||||
if !node.exact {
|
||||
return r.findByRegexp(value)
|
||||
return r.findByRegexp(originValue)
|
||||
}
|
||||
|
||||
return sb.String()
|
||||
|
||||
@ -11,6 +11,7 @@ func TestRadixMatcherWithPattern(t *testing.T) {
|
||||
m.Register("aloha")
|
||||
m.Register("hawaii")
|
||||
m.Register("te*")
|
||||
m.Register("Ширина")
|
||||
|
||||
var tt = []struct {
|
||||
name string
|
||||
@ -37,6 +38,9 @@ func TestRadixMatcherWithPattern(t *testing.T) {
|
||||
}, {
|
||||
name: "should not match 3",
|
||||
in: "alohaya",
|
||||
}, {
|
||||
name: "should match exact 3",
|
||||
in: "Ширина",
|
||||
}}
|
||||
|
||||
for _, tc := range tt {
|
||||
|
||||
Reference in New Issue
Block a user