Files
eway/cmd/cli/commands/dimensiondispatcher.go
2024-02-19 18:11:59 +03:00

57 lines
1.3 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 (d dimensionDispatcher) isDimensionParam(value string) bool {
return d.m.Match(value) != dimension.MatchResultMiss
}
func (d dimensionDispatcher) dispatch(ctx context.Context, key, value string, in *entity.GoodsItemSize) (updated bool) {
matchResult := 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.dispatch(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:
in.Height = out
case dimension.MatchResultLength:
in.Length = out
case dimension.MatchResultWidth:
in.Width = out
case dimension.MatchResultDepth:
in.UnmatchedDepth = out
}
}
return true
}