variant

package
v0.3.68 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 14, 2026 License: LGPL-2.1 Imports: 17 Imported by: 4

Documentation

Overview

Package variant 提供 GAP 消息和 RPC 负载使用的动态值模型。

Variant 是统一的协议值包装,持有 TypeId 和对应的可读值。内置值包括整数、 浮点数、布尔值、字节串、字符串、Null、Array、Map、Error 和 CallChain。 自定义值需要实现 Value 接口,并通过 VariantCreator 注册后,才能根据 TypeId 反序列化。

常用入口:

  • NewVariant:把已有 ReadableValue 包装为 Variant。
  • ToVariant:把常见 Go 值转换为 Variant。
  • Variant.ToNative:把反序列化后的 GAP 值转换为指定 Go reflect.Type。
  • Array.Snapshot:冻结 Array 的编码载荷,用于延迟交付或跨 goroutine 交付后的写出。快照 Array 是只读形态,只用于后续编码。

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidCast 表示动态值无法安全转换为目标 Go 类型。
	ErrInvalidCast = fmt.Errorf("%w: invalid cast", ErrVariant)
)
View Source
var (
	// ErrNotDeclared 表示指定动态值类型 ID 尚未注册。
	ErrNotDeclared = fmt.Errorf("%w: variant not declared", ErrVariant)
)
View Source
var (
	// ErrSnapshotReadonly 表示试图向只读数组快照解码数据。
	ErrSnapshotReadonly = fmt.Errorf("%w: snapshot array is readonly", ErrVariant)
)
View Source
var (
	// ErrVariant 是 GAP 动态值处理错误的根错误。
	ErrVariant = errors.New("gap-variant")
)

Functions

This section is empty.

Types

type Array

type Array struct {
	Items         []Variant        // 非快照形态的数组项。
	IsSnapshot    bool             // 是否为只读编码快照。
	SnapshotBytes binaryutil.Bytes // 快照编码;可回收快照使用完后必须释放。
}

Array 保存有序动态值;快照形态只保留已编码字节且不可写入。

func NewArray added in v0.3.68

func NewArray[T any](arr []T) (Array, error)

NewArray 将 Go 切片逐项转换为动态值数组。

func (Array) Indirect

func (v Array) Indirect() any

Indirect 返回数组值本身。

func (Array) Read

func (v Array) Read(p []byte) (int, error)

Read 将数组或其快照编码到 p。

func (Array) ReleaseIfSnapshot added in v0.3.68

func (v Array) ReleaseIfSnapshot()

ReleaseIfSnapshot 释放快照持有的可回收字节缓冲区;非快照调用无效。

func (Array) Size

func (v Array) Size() int

Size 返回数组编码后的字节数。

func (Array) Snapshot added in v0.3.68

func (v Array) Snapshot(recyclable bool) (Array, error)

Snapshot 创建数组编码的只读副本;recyclable 为 true 时使用完必须调用 ReleaseIfSnapshot。

func (Array) TypeId added in v0.1.57

func (Array) TypeId() TypeId

TypeId 返回数组的内置类型 ID。

func (*Array) Write

func (v *Array) Write(p []byte) (int, error)

Write 从 p 解码数组;快照形态返回 ErrSnapshotReadonly。

type Bool

type Bool bool

Bool 是 GAP 的 bool 动态值。

func (Bool) Indirect

func (v Bool) Indirect() any

Indirect 返回 bool。

func (Bool) Read

func (v Bool) Read(p []byte) (int, error)

Read 将值编码到 p。

func (Bool) Size

func (Bool) Size() int

Size 返回值编码后的字节数。

func (Bool) TypeId added in v0.1.57

func (Bool) TypeId() TypeId

TypeId 返回 bool 的内置类型 ID。

func (*Bool) Write

func (v *Bool) Write(p []byte) (int, error)

Write 从 p 解码值。

type Byte

type Byte byte

Byte 是 GAP 的 byte 动态值。

func (Byte) Indirect

func (v Byte) Indirect() any

Indirect 返回 byte。

func (Byte) Read

func (v Byte) Read(p []byte) (int, error)

Read 将值编码到 p。

func (Byte) Size

func (Byte) Size() int

Size 返回值编码后的字节数。

func (Byte) TypeId added in v0.1.57

func (Byte) TypeId() TypeId

TypeId 返回 byte 的内置类型 ID。

func (*Byte) Write

func (v *Byte) Write(p []byte) (int, error)

Write 从 p 解码值。

type Bytes

type Bytes []byte

Bytes 是 GAP 的字节切片动态值。

func (Bytes) Indirect

func (v Bytes) Indirect() any

Indirect 返回底层字节切片,不执行复制。

func (Bytes) Read

func (v Bytes) Read(p []byte) (int, error)

Read 将长度前缀和字节内容编码到 p。

func (Bytes) Size

func (v Bytes) Size() int

Size 返回长度前缀和内容的总字节数。

func (Bytes) TypeId added in v0.1.57

func (Bytes) TypeId() TypeId

TypeId 返回字节切片的内置类型 ID。

func (*Bytes) Write

func (v *Bytes) Write(p []byte) (int, error)

Write 从 p 解码并复制字节内容。

type Call added in v0.1.69

type Call struct {
	Svc       string    // 服务名。
	Addr      string    // 通信地址。
	Timestamp time.Time // 产生或转发调用的时间。
	Transit   bool      // 是否为中转节点。
}

Call 描述 RPC 调用链中的一个来源或中转节点。

type CallChain added in v0.1.69

type CallChain []Call

CallChain 按调用传播顺序保存来源和中转节点。

func (CallChain) First added in v0.1.92

func (v CallChain) First() Call

First 返回调用链首项;空调用链返回零值。

func (CallChain) Indirect added in v0.1.69

func (v CallChain) Indirect() any

Indirect 返回调用链值本身。

func (CallChain) Last added in v0.1.92

func (v CallChain) Last() Call

Last 返回调用链末项;空调用链返回零值。

func (CallChain) Read added in v0.1.69

func (v CallChain) Read(p []byte) (int, error)

Read 将调用链编码到 p。

func (CallChain) Size added in v0.1.69

func (v CallChain) Size() int

Size 返回调用链编码后的字节数。

func (CallChain) TypeId added in v0.1.69

func (CallChain) TypeId() TypeId

TypeId 返回调用链的内置类型 ID。

func (*CallChain) Write added in v0.1.69

func (v *CallChain) Write(p []byte) (int, error)

Write 从 p 解码调用链,时间戳转换为本地时区。

type Double

type Double float64

Double 是 GAP 的 float64 动态值。

func (Double) Indirect

func (v Double) Indirect() any

Indirect 返回 float64。

func (Double) Read

func (v Double) Read(p []byte) (int, error)

Read 将值编码到 p。

func (Double) Size

func (Double) Size() int

Size 返回值编码后的字节数。

func (Double) TypeId added in v0.1.57

func (Double) TypeId() TypeId

TypeId 返回 float64 的内置类型 ID。

func (*Double) Write

func (v *Double) Write(p []byte) (int, error)

Write 从 p 解码值。

type Error

type Error struct {
	Code    int32  // 错误码;零表示成功。
	Message string // 错误消息。
}

Error 是包含数值错误码和消息的可传输错误。

func Errorf added in v0.1.92

func Errorf(code int32, format string, args ...any) *Error

Errorf 创建带错误码和格式化消息的可传输错误。

func Errorln added in v0.1.92

func Errorln(code int32, message string) *Error

Errorln 创建带错误码和原始消息的可传输错误。

func NewError added in v0.3.68

func NewError(err error) *Error

NewError 将普通 error 转换为可传输错误;nil 转换为成功结果。

func (Error) Error

func (v Error) Error() string

Error 返回包含错误码和消息的文本。

func (*Error) Indirect

func (v *Error) Indirect() any

Indirect 返回错误指针。

func (Error) OK

func (v Error) OK() bool

OK 报告错误码是否为零。

func (Error) Read

func (v Error) Read(p []byte) (int, error)

Read 将错误编码到 p。

func (Error) Size

func (v Error) Size() int

Size 返回错误编码后的字节数。

func (Error) TypeId added in v0.1.57

func (Error) TypeId() TypeId

TypeId 返回错误的内置类型 ID。

func (*Error) Write

func (v *Error) Write(p []byte) (int, error)

Write 从 p 解码错误。

type Float

type Float float32

Float 是 GAP 的 float32 动态值。

func (Float) Indirect

func (v Float) Indirect() any

Indirect 返回 float32。

func (Float) Read

func (v Float) Read(p []byte) (int, error)

Read 将值编码到 p。

func (Float) Size

func (Float) Size() int

Size 返回值编码后的字节数。

func (Float) TypeId added in v0.1.57

func (Float) TypeId() TypeId

TypeId 返回 float32 的内置类型 ID。

func (*Float) Write

func (v *Float) Write(p []byte) (int, error)

Write 从 p 解码值。

type IVariantCreator

type IVariantCreator interface {
	// Declare 注册值指针的具体类型;重复 ID 会 panic。
	Declare(v Value)
	// New 创建指定类型 ID 对应的新值指针。
	New(typeId TypeId) (Value, error)
	// NewReflected 创建指定类型 ID 对应的新反射值。
	NewReflected(typeId TypeId) (reflect.Value, error)
}

IVariantCreator 按类型 ID 注册并构造动态值。

func VariantCreator

func VariantCreator() IVariantCreator

VariantCreator 返回已注册全部内置值的进程级类型构建器。

type Int

type Int int

Int 是 GAP 的 int 动态值,按变长有符号整数编码。

func (Int) Indirect

func (v Int) Indirect() any

Indirect 返回 int。

func (Int) Read

func (v Int) Read(p []byte) (int, error)

Read 将值编码到 p。

func (Int) Size

func (v Int) Size() int

Size 返回值编码后的字节数。

func (Int) TypeId added in v0.1.57

func (Int) TypeId() TypeId

TypeId 返回 int 的内置类型 ID。

func (*Int) Write

func (v *Int) Write(p []byte) (int, error)

Write 从 p 解码值。

type Int8

type Int8 int8

Int8 是 GAP 的 int8 动态值。

func (Int8) Indirect

func (v Int8) Indirect() any

Indirect 返回 int8。

func (Int8) Read

func (v Int8) Read(p []byte) (int, error)

Read 将值编码到 p。

func (Int8) Size

func (Int8) Size() int

Size 返回值编码后的字节数。

func (Int8) TypeId added in v0.1.57

func (Int8) TypeId() TypeId

TypeId 返回 int8 的内置类型 ID。

func (*Int8) Write

func (v *Int8) Write(p []byte) (int, error)

Write 从 p 解码值。

type Int16

type Int16 int16

Int16 是 GAP 的 int16 动态值。

func (Int16) Indirect

func (v Int16) Indirect() any

Indirect 返回 int16。

func (Int16) Read

func (v Int16) Read(p []byte) (int, error)

Read 将值编码到 p。

func (Int16) Size

func (Int16) Size() int

Size 返回值编码后的字节数。

func (Int16) TypeId added in v0.1.57

func (Int16) TypeId() TypeId

TypeId 返回 int16 的内置类型 ID。

func (*Int16) Write

func (v *Int16) Write(p []byte) (int, error)

Write 从 p 解码值。

type Int32

type Int32 int32

Int32 是 GAP 的 int32 动态值。

func (Int32) Indirect

func (v Int32) Indirect() any

Indirect 返回 int32。

func (Int32) Read

func (v Int32) Read(p []byte) (int, error)

Read 将值编码到 p。

func (Int32) Size

func (Int32) Size() int

Size 返回值编码后的字节数。

func (Int32) TypeId added in v0.1.57

func (Int32) TypeId() TypeId

TypeId 返回 int32 的内置类型 ID。

func (*Int32) Write

func (v *Int32) Write(p []byte) (int, error)

Write 从 p 解码值。

type Int64

type Int64 int64

Int64 是 GAP 的 int64 动态值。

func (Int64) Indirect

func (v Int64) Indirect() any

Indirect 返回 int64。

func (Int64) Read

func (v Int64) Read(p []byte) (int, error)

Read 将值编码到 p。

func (Int64) Size

func (v Int64) Size() int

Size 返回值编码后的字节数。

func (Int64) TypeId added in v0.1.57

func (Int64) TypeId() TypeId

TypeId 返回 int64 的内置类型 ID。

func (*Int64) Write

func (v *Int64) Write(p []byte) (int, error)

Write 从 p 解码值。

type Map

type Map struct {
	// Entries 保存键值对,允许使用可比较的 Variant 作为键。
	Entries generic.UnorderedSliceMap[Variant, Variant]
}

Map 以无序切片映射保存动态键值对。

func NewMapFromGoMap added in v0.3.68

func NewMapFromGoMap[K comparable, V any](m map[K]V) (Map, error)

NewMapFromGoMap 将 Go map 的键和值转换为动态值映射;迭代顺序不保证稳定。

func NewMapFromSliceMap added in v0.3.68

func NewMapFromSliceMap[K cmp.Ordered, V any](m generic.SliceMap[K, V]) (Map, error)

NewMapFromSliceMap 按 SliceMap 的顺序转换键值对。

func NewMapFromUnorderedSliceMap added in v0.3.68

func NewMapFromUnorderedSliceMap[K comparable, V any](m generic.UnorderedSliceMap[K, V]) (Map, error)

NewMapFromUnorderedSliceMap 按当前存储顺序转换键值对。

func (Map) Indirect

func (v Map) Indirect() any

Indirect 返回映射值本身。

func (Map) Read

func (v Map) Read(p []byte) (int, error)

Read 将动态值映射编码到 p。

func (Map) Size

func (v Map) Size() int

Size 返回映射编码后的字节数。

func (Map) TypeId added in v0.1.57

func (Map) TypeId() TypeId

TypeId 返回映射的内置类型 ID。

func (*Map) Write

func (v *Map) Write(p []byte) (int, error)

Write 从 p 解码动态值映射。

type Null

type Null struct{}

Null 是不携带负载的 GAP 空值。

func (Null) Indirect

func (Null) Indirect() any

Indirect 返回 nil。

func (Null) Read

func (Null) Read(p []byte) (int, error)

Read 不写入数据并立即结束。

func (Null) Size

func (Null) Size() int

Size 始终返回零。

func (Null) TypeId added in v0.1.57

func (Null) TypeId() TypeId

TypeId 返回空值的内置类型 ID。

func (Null) Write

func (Null) Write(p []byte) (int, error)

Write 不读取数据。

type ReadableValue added in v0.3.59

type ReadableValue interface {
	io.Reader
	// Size 返回值编码后的字节数。
	Size() int
	// TypeId 返回动态值类型 ID。
	TypeId() TypeId
	// Indirect 返回对应的 Go 值。
	Indirect() any
}

ReadableValue 表示可编码到 GAP 动态值字节流的值。

type String

type String string

String 是 GAP 的字符串动态值。

func (String) Indirect

func (v String) Indirect() any

Indirect 返回 string。

func (String) Read

func (v String) Read(p []byte) (int, error)

Read 将长度前缀和字符串内容编码到 p。

func (String) Size

func (v String) Size() int

Size 返回长度前缀和字符串内容的总字节数。

func (String) TypeId added in v0.1.57

func (String) TypeId() TypeId

TypeId 返回字符串的内置类型 ID。

func (*String) Write

func (v *String) Write(p []byte) (int, error)

Write 从 p 解码字符串。

type TypeId

type TypeId uint32

TypeId 标识 GAP 动态值的具体类型。

const (
	// TypeId_None 表示未设置动态值类型。
	TypeId_None TypeId = iota
	// TypeId_Int 标识 int。
	TypeId_Int
	// TypeId_Int8 标识 int8。
	TypeId_Int8
	// TypeId_Int16 标识 int16。
	TypeId_Int16
	// TypeId_Int32 标识 int32。
	TypeId_Int32
	// TypeId_Int64 标识 int64。
	TypeId_Int64
	// TypeId_Uint 标识 uint。
	TypeId_Uint
	// TypeId_Uint8 标识 uint8。
	TypeId_Uint8
	// TypeId_Uint16 标识 uint16。
	TypeId_Uint16
	// TypeId_Uint32 标识 uint32。
	TypeId_Uint32
	// TypeId_Uint64 标识 uint64。
	TypeId_Uint64
	// TypeId_Float 标识 float32。
	TypeId_Float
	// TypeId_Double 标识 float64。
	TypeId_Double
	// TypeId_Byte 标识 byte。
	TypeId_Byte
	// TypeId_Bool 标识 bool。
	TypeId_Bool
	// TypeId_Bytes 标识 []byte。
	TypeId_Bytes
	// TypeId_String 标识 string。
	TypeId_String
	// TypeId_Null 标识空值。
	TypeId_Null
	// TypeId_Array 标识动态值数组。
	TypeId_Array
	// TypeId_Map 标识动态值映射。
	TypeId_Map
	// TypeId_Error 标识可传输错误。
	TypeId_Error
	// TypeId_CallChain 标识 RPC 调用链。
	TypeId_CallChain
	// TypeId_Customize 是自定义类型 ID 的起始偏移。
	TypeId_Customize = 32
)

内置动态值类型 ID。自定义类型必须从 TypeId_Customize 起分配。

func GenTypeId added in v0.3.68

func GenTypeId(v any) TypeId

GenTypeId 根据具名 Value 类型的完整名称生成自定义类型 ID。

func GenTypeIdT added in v0.3.68

func GenTypeIdT[T any]() TypeId

GenTypeIdT 根据具名类型 T 的完整名称生成自定义类型 ID;*T 必须实现 Value。

func (TypeId) New

func (t TypeId) New() (Value, error)

New 使用进程级类型构建器创建此类型的新值。

func (TypeId) NewReflected

func (t TypeId) NewReflected() (reflect.Value, error)

NewReflected 使用进程级类型构建器创建此类型的新反射值。

func (TypeId) Read

func (t TypeId) Read(p []byte) (int, error)

Read 将类型 ID 编码到 p。

func (TypeId) Size

func (t TypeId) Size() int

Size 返回类型 ID 的固定编码字节数。

func (*TypeId) Write

func (t *TypeId) Write(p []byte) (int, error)

Write 从 p 解码类型 ID。

type Uint

type Uint uint

Uint 是 GAP 的 uint 动态值,按变长无符号整数编码。

func (Uint) Indirect

func (v Uint) Indirect() any

Indirect 返回 uint。

func (Uint) Read

func (v Uint) Read(p []byte) (int, error)

Read 将值编码到 p。

func (Uint) Size

func (v Uint) Size() int

Size 返回值编码后的字节数。

func (Uint) TypeId added in v0.1.57

func (Uint) TypeId() TypeId

TypeId 返回 uint 的内置类型 ID。

func (*Uint) Write

func (v *Uint) Write(p []byte) (int, error)

Write 从 p 解码值。

type Uint8

type Uint8 uint8

Uint8 是 GAP 的 uint8 动态值。

func (Uint8) Indirect

func (v Uint8) Indirect() any

Indirect 返回 uint8。

func (Uint8) Read

func (v Uint8) Read(p []byte) (int, error)

Read 将值编码到 p。

func (Uint8) Size

func (Uint8) Size() int

Size 返回值编码后的字节数。

func (Uint8) TypeId added in v0.1.57

func (Uint8) TypeId() TypeId

TypeId 返回 uint8 的内置类型 ID。

func (*Uint8) Write

func (v *Uint8) Write(p []byte) (int, error)

Write 从 p 解码值。

type Uint16

type Uint16 uint16

Uint16 是 GAP 的 uint16 动态值。

func (Uint16) Indirect

func (v Uint16) Indirect() any

Indirect 返回 uint16。

func (Uint16) Read

func (v Uint16) Read(p []byte) (int, error)

Read 将值编码到 p。

func (Uint16) Size

func (Uint16) Size() int

Size 返回值编码后的字节数。

func (Uint16) TypeId added in v0.1.57

func (Uint16) TypeId() TypeId

TypeId 返回 uint16 的内置类型 ID。

func (*Uint16) Write

func (v *Uint16) Write(p []byte) (int, error)

Write 从 p 解码值。

type Uint32

type Uint32 uint32

Uint32 是 GAP 的 uint32 动态值。

func (Uint32) Indirect

func (v Uint32) Indirect() any

Indirect 返回 uint32。

func (Uint32) Read

func (v Uint32) Read(p []byte) (int, error)

Read 将值编码到 p。

func (Uint32) Size

func (Uint32) Size() int

Size 返回值编码后的字节数。

func (Uint32) TypeId added in v0.1.57

func (Uint32) TypeId() TypeId

TypeId 返回 uint32 的内置类型 ID。

func (*Uint32) Write

func (v *Uint32) Write(p []byte) (int, error)

Write 从 p 解码值。

type Uint64

type Uint64 uint64

Uint64 是 GAP 的 uint64 动态值。

func (Uint64) Indirect

func (v Uint64) Indirect() any

Indirect 返回 uint64。

func (Uint64) Read

func (v Uint64) Read(p []byte) (int, error)

Read 将值编码到 p。

func (Uint64) Size

func (v Uint64) Size() int

Size 返回值编码后的字节数。

func (Uint64) TypeId added in v0.1.57

func (Uint64) TypeId() TypeId

TypeId 返回 uint64 的内置类型 ID。

func (*Uint64) Write

func (v *Uint64) Write(p []byte) (int, error)

Write 从 p 解码值。

type Value

type Value interface {
	ReadableValue
	io.Writer
}

Value 表示既可编码也可从字节流解码的动态值。

type Variant

type Variant struct {
	TypeId    TypeId        // 动态值类型 ID。
	Value     ReadableValue // 实际值。
	Reflected reflect.Value // 解码自定义类型时创建的反射值;自行构造时可为空。
}

Variant 将类型 ID 与可编码值关联,并可保留解码时创建的反射值。

func NewVariant added in v0.3.68

func NewVariant(v ReadableValue) (Variant, error)

NewVariant 用值自身的类型 ID 创建动态值包装。

func ToVariant added in v0.3.68

func ToVariant(a any) (Variant, error)

ToVariant 将受支持的 Go 值或 reflect.Value 转换为 GAP 动态值。

func (Variant) IsValid added in v0.3.59

func (v Variant) IsValid() bool

IsValid 报告类型 ID 是否与实际值声明的类型一致。

func (Variant) Read

func (v Variant) Read(p []byte) (int, error)

Read 将带类型信息的动态值编码到 p。

func (Variant) Size

func (v Variant) Size() int

Size 返回动态值连同类型信息编码后的字节数;无效值返回零。

func (Variant) ToNative added in v0.3.68

func (v Variant) ToNative(valueRT reflect.Type) (reflect.Value, error)

ToNative 将动态值转换为 valueRT;数值转换不允许缩窄底层存储宽度。

func (*Variant) Write

func (v *Variant) Write(p []byte) (int, error)

Write 从 p 解码动态值,并通过 VariantCreator 构造其具体类型。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL