Files
EntitleForge-SDK-go/license/entitlement.go
T
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

171 lines
4.2 KiB
Go

// Package license 实现 EntitleForge 三段式 License 封装的编解码与载荷结构。
// 设计依据:docs/design/03-license-format.md
package license
import (
"encoding/json"
"fmt"
"slices"
)
// ValueType 是 Feature 授权项的类型编码(docs/design/05-entitlement-model.md §2.2)。
type ValueType string
const (
TypeBool ValueType = "b" // 布尔开关
TypeQuantity ValueType = "q" // 数量配额
TypeTier ValueType = "t" // 订阅档位
TypeConstraint ValueType = "c" // 值域约束
)
// Entitlement 是 License 中对单个 Feature 的授权值。
// 自定义 JSON 序列化以保证类型严格(错误类型直接拒绝解码)与整数确定性输出。
type Entitlement struct {
Type ValueType
Bool bool
Int int64
Str string
List []string
}
func Bool(v bool) Entitlement { return Entitlement{Type: TypeBool, Bool: v} }
func Quantity(v int64) Entitlement { return Entitlement{Type: TypeQuantity, Int: v} }
func TierValue(v string) Entitlement { return Entitlement{Type: TypeTier, Str: v} }
func Constraint(v []string) Entitlement { return Entitlement{Type: TypeConstraint, List: v} }
// AsBool 在类型匹配时返回布尔值,否则零值。
func (e Entitlement) AsBool() bool {
if e.Type != TypeBool {
return false
}
return e.Bool
}
// AsInt 在类型匹配时返回数量值,否则 0。
func (e Entitlement) AsInt() int64 {
if e.Type != TypeQuantity {
return 0
}
return e.Int
}
// AsString 在类型匹配时返回字符串值,否则空串。
func (e Entitlement) AsString() string {
if e.Type != TypeTier {
return ""
}
return e.Str
}
// AsList 在类型匹配时返回约束集合(返回拷贝防调用方篡改),否则 nil。
func (e Entitlement) AsList() []string {
if e.Type != TypeConstraint {
return nil
}
out := make([]string, len(e.List))
copy(out, e.List)
return out
}
// Equal 判断两个授权项是否等价(含约束集合的逐元素比较)。
func (e Entitlement) Equal(other Entitlement) bool {
if e.Type != other.Type {
return false
}
switch e.Type {
case TypeBool:
return e.Bool == other.Bool
case TypeQuantity:
return e.Int == other.Int
case TypeTier:
return e.Str == other.Str
case TypeConstraint:
if len(e.List) != len(other.List) {
return false
}
for i := range e.List {
if e.List[i] != other.List[i] {
return false
}
}
return true
}
return false
}
// Allows 判断约束集合是否包含 value。
func (e Entitlement) Allows(value string) bool {
if e.Type != TypeConstraint {
return false
}
return slices.Contains(e.List, value)
}
func (e Entitlement) MarshalJSON() ([]byte, error) {
switch e.Type {
case TypeBool:
return json.Marshal(struct {
T string `json:"t"`
V bool `json:"v"`
}{string(e.Type), e.Bool})
case TypeQuantity:
return json.Marshal(struct {
T string `json:"t"`
V int64 `json:"v"`
}{string(e.Type), e.Int})
case TypeTier:
return json.Marshal(struct {
T string `json:"t"`
V string `json:"v"`
}{string(e.Type), e.Str})
case TypeConstraint:
return json.Marshal(struct {
T string `json:"t"`
V []string `json:"v"`
}{string(e.Type), e.List})
}
return nil, fmt.Errorf("entitlement: 未知类型 %q", e.Type)
}
func (e *Entitlement) UnmarshalJSON(data []byte) error {
var raw struct {
T string `json:"t"`
V json.RawMessage `json:"v"`
}
if err := json.Unmarshal(data, &raw); err != nil {
return fmt.Errorf("entitlement: %w", err)
}
if string(raw.V) == "null" {
return fmt.Errorf("entitlement: v 不允许为 null")
}
switch ValueType(raw.T) {
case TypeBool:
var v bool
if err := json.Unmarshal(raw.V, &v); err != nil {
return fmt.Errorf("entitlement b: %w", err)
}
*e = Bool(v)
case TypeQuantity:
var v int64
if err := json.Unmarshal(raw.V, &v); err != nil {
return fmt.Errorf("entitlement q: %w", err)
}
*e = Quantity(v)
case TypeTier:
var v string
if err := json.Unmarshal(raw.V, &v); err != nil {
return fmt.Errorf("entitlement t: %w", err)
}
*e = TierValue(v)
case TypeConstraint:
var v []string
if err := json.Unmarshal(raw.V, &v); err != nil {
return fmt.Errorf("entitlement c: %w", err)
}
*e = Constraint(v)
default:
return fmt.Errorf("entitlement: 未知类型 %q", raw.T)
}
return nil
}