73 lines
1.6 KiB
Go
73 lines
1.6 KiB
Go
package commands
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"git.loyso.art/frx/eway/internal/dimension"
|
|
"git.loyso.art/frx/eway/internal/entity"
|
|
|
|
"github.com/rs/zerolog"
|
|
)
|
|
|
|
type dimensionDispatcher struct {
|
|
m *dimension.Matcher
|
|
}
|
|
|
|
func pickFirst[T, V any](t T, v V) T {
|
|
return t
|
|
}
|
|
|
|
func (d dimensionDispatcher) isDimensionParam(value string) bool {
|
|
return pickFirst(d.m.Match(value)) != dimension.MatchResultMiss
|
|
}
|
|
|
|
func (d dimensionDispatcher) applyDimensionValue(ctx context.Context, key, value string, in *entity.GoodsItemSize) (updated bool) {
|
|
matchResult, priority := d.m.Match(key)
|
|
if matchResult == dimension.MatchResultMiss {
|
|
return false
|
|
}
|
|
|
|
log := zerolog.Ctx(ctx).With().Str("key", key).Str("value", value).Logger()
|
|
|
|
if strings.Contains(value, "/") {
|
|
dimensionValues := strings.Split(value, "/")
|
|
for _, dv := range dimensionValues {
|
|
updated = updated || d.applyDimensionValue(ctx, key, dv, in)
|
|
}
|
|
} else {
|
|
out, err := entity.ParseDimention(value, entity.DimensionLocalRU)
|
|
if err != nil {
|
|
log.Warn().Err(err).Msg("unable to parse key, skipping")
|
|
return false
|
|
}
|
|
|
|
out = out.AdjustTo(entity.DimensionKindCentimeter)
|
|
|
|
switch matchResult {
|
|
case dimension.MatchResultHeight:
|
|
if !priority && in.Height.Value != 0 {
|
|
return false
|
|
}
|
|
|
|
in.Height = out
|
|
case dimension.MatchResultLength:
|
|
if !priority && in.Length.Value != 0 {
|
|
return false
|
|
}
|
|
|
|
in.Length = out
|
|
case dimension.MatchResultWidth:
|
|
if !priority && in.Width.Value != 0 {
|
|
return false
|
|
}
|
|
|
|
in.Width = out
|
|
case dimension.MatchResultDepth:
|
|
in.UnmatchedDepth = out
|
|
}
|
|
}
|
|
|
|
return true
|
|
}
|