From 2fa8872603268fc4ca45885389b619dfa158708d Mon Sep 17 00:00:00 2001 From: BXY0414 <1079823339@qq.com> Date: Fri, 10 Jul 2026 19:04:45 +0800 Subject: [PATCH] fix(product): tighten SKU attributes parsing for product query --- internal/repository/product_repository.go | 63 ++++++++++++++++++++--- internal/server/product_server.go | 3 ++ 2 files changed, 59 insertions(+), 7 deletions(-) diff --git a/internal/repository/product_repository.go b/internal/repository/product_repository.go index 59e44c4..08e99d4 100644 --- a/internal/repository/product_repository.go +++ b/internal/repository/product_repository.go @@ -10,6 +10,8 @@ import ( "strings" "time" + "github.com/jackc/pgx/v5/pgconn" + productv1 "llms.idsbuybuybuy.com/ecommerce/ids-proto/gen/go/ids/product/v1" ) @@ -19,6 +21,9 @@ var ErrProductNotFound = errors.New("product not found") // ErrSKUNotFound is returned when a SKU does not exist. var ErrSKUNotFound = errors.New("sku not found") +// ErrDuplicateSPUNo is returned when a non-deleted product already uses the SPU number. +var ErrDuplicateSPUNo = errors.New("spu_no already exists") + // ProductRecord is the persistence model for product.products (SPU). type ProductRecord struct { ID string @@ -678,11 +683,22 @@ RETURNING id, createdProduct, err := scanProduct(row) if err != nil { + if isPostgresConstraintError(err, "23505", "uq_products_spu_no") { + return nil, fmt.Errorf("%w: %s", ErrDuplicateSPUNo, product.SPUNo) + } return nil, fmt.Errorf("insert product: %w", err) } return createdProduct, nil } +func isPostgresConstraintError(err error, code, constraint string) bool { + var pgErr *pgconn.PgError + if !errors.As(err, &pgErr) { + return false + } + return pgErr.Code == code && pgErr.ConstraintName == constraint +} + func insertSKU(ctx context.Context, tx *sql.Tx, productID string, sku SKURecord) (*SKURecord, error) { attrs, err := normalizeAttributesJSON(sku.AttributesJSON) if err != nil { @@ -1198,15 +1214,12 @@ func productCrossBorderJSON(product ProductRecord) ([]byte, error) { } func applySKUAttributes(rec *SKURecord) error { - if rec == nil || strings.TrimSpace(rec.AttributesJSON) == "" { + if rec == nil { return nil } - if strings.TrimSpace(rec.AttributesJSON) == "{}" { - return nil - } - var attrs []map[string]string - if err := json.Unmarshal([]byte(rec.AttributesJSON), &attrs); err != nil { - return fmt.Errorf("unmarshal sku attributes: %w", err) + attrs, err := parseSKUAttributes(rec.AttributesJSON) + if err != nil { + return err } for _, item := range attrs { if value := item["k1"]; value != "" { @@ -1231,6 +1244,42 @@ func applySKUAttributes(rec *SKURecord) error { return nil } +func parseSKUAttributes(raw string) ([]map[string]string, error) { + raw = strings.TrimSpace(raw) + if raw == "" || raw == "{}" || raw == "null" { + return nil, nil + } + var attrs []map[string]string + if err := json.Unmarshal([]byte(raw), &attrs); err == nil { + return attrs, nil + } + + var attr map[string]any + if err := json.Unmarshal([]byte(raw), &attr); err != nil { + return nil, fmt.Errorf("unmarshal sku attributes: %w", err) + } + if isEmptySKUAttributeObject(attr) { + return nil, nil + } + return nil, fmt.Errorf("unmarshal sku attributes: expected array, got object") +} + +func isEmptySKUAttributeObject(attr map[string]any) bool { + if len(attr) == 0 { + return true + } + for _, value := range attr { + if value == nil { + continue + } + if s, ok := value.(string); ok && strings.TrimSpace(s) == "" { + continue + } + return false + } + return true +} + func parseStringArrayJSON(raw string) ([]string, error) { if strings.TrimSpace(raw) == "" { return []string{}, nil diff --git a/internal/server/product_server.go b/internal/server/product_server.go index 6887b5e..29716c1 100644 --- a/internal/server/product_server.go +++ b/internal/server/product_server.go @@ -88,6 +88,9 @@ func (s *ProductServer) CreateProduct(ctx context.Context, req *productv1.Create dbSpan.RecordError(err) dbSpan.SetStatus(otelcodes.Error, err.Error()) s.observer.Logger(ctx).Error("create product failed", zap.String("spu_no", product.SPUNo), zap.Error(err)) + if errors.Is(err, repository.ErrDuplicateSPUNo) { + return nil, status.Errorf(codes.AlreadyExists, "spu_no already exists: %s", product.SPUNo) + } return nil, status.Errorf(codes.Internal, "create product: %v", err) } observability.IncProductsCreated()