27 lines
806 B
Go
27 lines
806 B
Go
// Package spec 提供顺运宝规格身份键的唯一规范化实现。
|
|||
|
|
package spec
|
||
|
|
|
||
|
|
import (
|
||
|
|
"fmt"
|
||
|
|
"strings"
|
||
|
|
"unicode/utf8"
|
||
|
|
)
|
||
|
|
|
||
|
|
// MaxKeyRunes 是数据库 VARCHAR(191) 身份列允许的最大字符数。
|
||
|
|
const MaxKeyRunes = 191
|
||
|
|
|
||
|
|
// SpecKey 把顺运宝规格原文规范化成稳定身份键。
|
||
|
|
//
|
||
|
|
// 它只去掉首尾空白并把连续空白折叠为一个空格,不做颜色、尺码或繁简
|
||
|
|
// 推断。空规格和超过数据库列宽的规格返回错误,调用方不得截断。
|
||
|
|
func SpecKey(raw string) (string, error) {
|
||
|
|
key := strings.Join(strings.Fields(raw), " ")
|
||
|
|
if key == "" {
|
||
|
|
return "", fmt.Errorf("规格原文不能为空")
|
||
|
|
}
|
||
|
|
if utf8.RuneCountInString(key) > MaxKeyRunes {
|
||
|
|
return "", fmt.Errorf("规格身份键超过 %d 个字符", MaxKeyRunes)
|
||
|
|
}
|
||
|
|
return key, nil
|
||
|
|
}
|