Files
eway/cmd/cli/dimensiondispatcher.go
2024-02-11 15:50:43 +03:00

72 lines
1.7 KiB
Go

package main
import (
"context"
"strings"
"git.loyso.art/frx/eway/internal/entity"
"git.loyso.art/frx/eway/internal/matcher"
"github.com/rs/zerolog"
)
type dimensionDispatcher struct {
heigth matcher.Unit
width matcher.Unit
length matcher.Unit
}
func (d dimensionDispatcher) isDimensionParam(value string) bool {
return d.heigth.Match(value) || d.width.Match(value) || d.length.Match(value)
}
func (d dimensionDispatcher) dispatch(ctx context.Context, value, key string, in *entity.GoodsItemSize) {
if !d.isDimensionParam(value) {
return
}
if strings.Contains(value, "/") {
dimensionValues := strings.Split(value, "/")
for _, dv := range dimensionValues {
d.dispatch(ctx, dv, key, in)
}
} else {
out, err := entity.ParseDimention(key, entity.DimensionLocalRU)
if err != nil {
zerolog.Ctx(ctx).Warn().Err(err).Msg("unable to parse key, skipping")
return
}
out = out.AdjustTo(entity.DimensionKindCentimeter)
switch {
case d.heigth.Match(value):
in.Height = out
case d.width.Match(value):
in.Width = out
case d.width.Match(value):
in.Length = out
default:
zerolog.Ctx(ctx).Error().Str("key", key).Msg("unable to find proper matcher")
}
}
}
func makeDefaultDimensionDispatcher() dimensionDispatcher {
h := matcher.NewRadix(matcher.RadixCaseInsensitive())
h.Register("Высота")
h.Register("Высота/*")
w := matcher.NewRadix(matcher.RadixCaseInsensitive())
w.Register("Ширина")
w.Register("Ширина/*")
l := matcher.NewRadix(matcher.RadixCaseInsensitive())
l.Register("Длина")
l.Register("Длина/*")
l.Register("Общ. длина")
return dimensionDispatcher{
heigth: h,
width: w,
length: l,
}
}