mirror of
https://ids-dev.idsbuybuybuy.com/ids-ecommerce/ids-svc-product.git
synced 2026-11-29 16:57:41 +08:00
feat: implement postgresInt32Array type for scanning PostgreSQL INTEGER[] values
This commit is contained in:
parent
9567b967b2
commit
ee3c935f70
@ -6,6 +6,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
@ -416,14 +417,56 @@ func (r *ProductRepository) UnbindProductsFromProductCategory(ctx context.Contex
|
||||
|
||||
type productCategoryScanner interface{ Scan(...any) error }
|
||||
|
||||
// postgresInt32Array adapts PostgreSQL INTEGER[] values returned by pgx's
|
||||
// database/sql driver (as "{1,6}") to the protobuf-facing []int32 field.
|
||||
// database/sql cannot scan that textual value directly into []int32.
|
||||
type postgresInt32Array []int32
|
||||
|
||||
func (values *postgresInt32Array) Scan(source any) error {
|
||||
if source == nil {
|
||||
*values = nil
|
||||
return nil
|
||||
}
|
||||
var raw string
|
||||
switch value := source.(type) {
|
||||
case string:
|
||||
raw = value
|
||||
case []byte:
|
||||
raw = string(value)
|
||||
default:
|
||||
return fmt.Errorf("unsupported PostgreSQL integer array type %T", source)
|
||||
}
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "{}" {
|
||||
*values = []int32{}
|
||||
return nil
|
||||
}
|
||||
if len(raw) < 2 || raw[0] != '{' || raw[len(raw)-1] != '}' {
|
||||
return fmt.Errorf("invalid PostgreSQL integer array %q", raw)
|
||||
}
|
||||
parts := strings.Split(raw[1:len(raw)-1], ",")
|
||||
parsed := make([]int32, 0, len(parts))
|
||||
for _, part := range parts {
|
||||
value, err := strconv.ParseInt(strings.TrimSpace(part), 10, 32)
|
||||
if err != nil {
|
||||
return fmt.Errorf("parse PostgreSQL integer array element %q: %w", part, err)
|
||||
}
|
||||
parsed = append(parsed, int32(value))
|
||||
}
|
||||
*values = parsed
|
||||
return nil
|
||||
}
|
||||
|
||||
func scanProductCategory(row productCategoryScanner) (*ProductCategoryRecord, error) {
|
||||
var id sql.NullInt64
|
||||
var parent sql.NullInt64
|
||||
var deleted sql.NullTime
|
||||
var channelList postgresInt32Array
|
||||
value := &ProductCategoryRecord{}
|
||||
if err := row.Scan(&id, &parent, &value.Level, &value.Name, &value.Priority, &value.RuleID, &value.Status, &value.CategoryType, &value.ChannelList, &value.HasChildren, &value.HasPropRequired, &value.Extra, &value.CertStatusCode, &value.CertStatusPass, &value.CertStatusMsg, &value.Version, &value.CreatedAt, &value.UpdatedAt, &deleted, &value.ProductCount); err != nil {
|
||||
if err := row.Scan(&id, &parent, &value.Level, &value.Name, &value.Priority, &value.RuleID, &value.Status, &value.CategoryType, &channelList, &value.HasChildren, &value.HasPropRequired, &value.Extra, &value.CertStatusCode, &value.CertStatusPass, &value.CertStatusMsg, &value.Version, &value.CreatedAt, &value.UpdatedAt, &deleted, &value.ProductCount); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
value.ChannelList = []int32(channelList)
|
||||
value.ID = formatID(id.Int64)
|
||||
if parent.Valid {
|
||||
value.ParentID = formatID(parent.Int64)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user