Files
iceking2nd 81c7295893
sdk-ci / test (push) Failing after 8s
feat: 益铸 EntitleForge SDK 独立仓库首个版本
- 自主仓 git.wxccs.org/EntitleForge/EntitleForge 拆分(协议实现单一归属:
  license/crypto/ulid 随 SDK 迁出,服务端反向依赖本仓)
- sdk:Client 三模式(文件/远程/代理)、GRACE 状态机、查询 API(6.6ns/0 alloc)
- license:三段式编解码与严格解析;crypto:Ed25519 信任链 + Proxy 受约束证书
- 零第三方依赖(CI 有依赖闭包断言红线);go.mod 零 require
- CI:Gitea Actions(test + 零依赖断言 + 三平台交叉编译冒烟)
2026-11-09 15:42:22 +08:00

97 lines
2.9 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package license
import "fmt"
// ParseLooseEntitlements 把宽松类型(yaml/json 管理面输入)映射为强类型集合。
//
// 裸值自动推断:bool→b、int→q、string→t、[]any(string)→c;
// 显式写法(消除 string 歧义):{t: q, v: 50},类型错位严格报错。
// cmd/efctl 与服务端 Issuer 共用本函数(禁止两套实现漂移)。
func ParseLooseEntitlements(raw map[string]any) (map[string]Entitlement, error) {
out := make(map[string]Entitlement, len(raw))
for fid, v := range raw {
switch val := v.(type) {
case bool:
out[fid] = Bool(val)
case int:
out[fid] = Quantity(int64(val))
case int64:
out[fid] = Quantity(val)
case float64:
// JSON 反序列化(DB JSON 列 / 管理 API body)把整数变成 float64,
// 整数值接受,非整数拒绝
if val != float64(int64(val)) {
return nil, fmt.Errorf("授权项 %q: 数量必须为整数(got %v)", fid, val)
}
out[fid] = Quantity(int64(val))
case string:
out[fid] = TierValue(val)
case []any:
list := make([]string, 0, len(val))
for _, item := range val {
s, ok := item.(string)
if !ok {
return nil, fmt.Errorf("授权项 %q: 约束集合元素必须为字符串", fid)
}
list = append(list, s)
}
out[fid] = Constraint(list)
case []string:
out[fid] = Constraint(val)
case map[string]any:
tRaw, _ := val["t"].(string)
vRaw, ok := val["v"]
if !ok {
return nil, fmt.Errorf("授权项 %q: 显式写法缺少 v", fid)
}
switch ValueType(tRaw) {
case TypeBool:
b, ok := vRaw.(bool)
if !ok {
return nil, fmt.Errorf("授权项 %q: b 类型值必须为布尔", fid)
}
out[fid] = Bool(b)
case TypeQuantity:
switch n := vRaw.(type) {
case int:
out[fid] = Quantity(int64(n))
case int64:
out[fid] = Quantity(n)
case float64:
if n != float64(int64(n)) {
return nil, fmt.Errorf("授权项 %q: q 类型值必须为整数", fid)
}
out[fid] = Quantity(int64(n))
default:
return nil, fmt.Errorf("授权项 %q: q 类型值必须为整数", fid)
}
case TypeTier:
s, ok := vRaw.(string)
if !ok {
return nil, fmt.Errorf("授权项 %q: t 类型值必须为字符串", fid)
}
out[fid] = TierValue(s)
case TypeConstraint:
items, ok := vRaw.([]any)
if !ok {
return nil, fmt.Errorf("授权项 %q: c 类型值必须为字符串列表", fid)
}
list := make([]string, 0, len(items))
for _, item := range items {
s, ok := item.(string)
if !ok {
return nil, fmt.Errorf("授权项 %q: c 集合元素必须为字符串", fid)
}
list = append(list, s)
}
out[fid] = Constraint(list)
default:
return nil, fmt.Errorf("授权项 %q: 未知显式类型 %q(支持 b|q|t|c)", fid, tRaw)
}
default:
return nil, fmt.Errorf("授权项 %q: 不支持的值类型 %T", fid, v)
}
}
return out, nil
}