Documentation
¶
Overview ¶
Package protoenum: Utilities to handle Protocol Buffer enum metadata management Provides type-safe enum descriptors with Go native enum binding and custom metadata Supports triple generic wrapping: protoEnum, basic, and metaType Enables seamless conversion between protobuf enums and Go native enum types
protoenum: Protocol Buffer 枚举元数据管理包装工具 提供带有 Go 原生枚举绑定和自定义元数据的类型安全枚举描述符 支持三泛型包装:protoEnum、basic 和 metaType 实现 protobuf 枚举与 Go 原生枚举类型之间的无缝转换
Package protoenum: Collection management to handle Protocol Buffer enum metadata Provides indexed collections of enum descriptors with multiple lookup methods Enables fast lookup using code, name, and basic value with efficient enum handling
protoenum: Protocol Buffer 枚举元数据集合管理 提供带有多种查找方法的枚举描述符索引集合 支持按代码、名称或 basic 枚举值快速检索,实现高效枚举处理
Index ¶
- type Enum
- func NewEnum[protoEnum ProtoEnum, basicEnum comparable](proto protoEnum, basic basicEnum) *Enum[protoEnum, basicEnum, *MetaNone]
- func NewEnumWithDesc[protoEnum ProtoEnum, basicEnum comparable](proto protoEnum, basic basicEnum, description string) *Enum[protoEnum, basicEnum, *MetaDesc]
- func NewEnumWithMeta[protoEnum ProtoEnum, basicEnum comparable, metaType any](proto protoEnum, basic basicEnum, meta metaType) *Enum[protoEnum, basicEnum, metaType]
- func (c *Enum[protoEnum, basicEnum, metaType]) Basic() basicEnum
- func (c *Enum[protoEnum, basicEnum, metaType]) Code() int32
- func (c *Enum[protoEnum, basicEnum, metaType]) Meta() metaType
- func (c *Enum[protoEnum, basicEnum, metaType]) Name() string
- func (c *Enum[protoEnum, basicEnum, metaType]) Proto() protoEnum
- type Enums
- func (c *Enums[P, B, M]) GetByBasic(basic B) *Enum[P, B, M]
- func (c *Enums[P, B, M]) GetByCode(code int32) *Enum[P, B, M]
- func (c *Enums[P, B, M]) GetByName(name string) *Enum[P, B, M]
- func (c *Enums[P, B, M]) GetByProto(proto P) *Enum[P, B, M]
- func (c *Enums[P, B, M]) GetDefault() *Enum[P, B, M]
- func (c *Enums[P, B, M]) GetDefaultBasic() B
- func (c *Enums[P, B, M]) GetDefaultProto() P
- func (c *Enums[P, B, M]) ListBasics() []B
- func (c *Enums[P, B, M]) ListProtos() []P
- func (c *Enums[P, B, M]) ListValidBasics() []B
- func (c *Enums[P, B, M]) ListValidProtos() []P
- func (c *Enums[P, B, M]) LookupByBasic(basic B) (*Enum[P, B, M], bool)
- func (c *Enums[P, B, M]) LookupByCode(code int32) (*Enum[P, B, M], bool)
- func (c *Enums[P, B, M]) LookupByName(name string) (*Enum[P, B, M], bool)
- func (c *Enums[P, B, M]) LookupByProto(proto P) (*Enum[P, B, M], bool)
- func (c *Enums[P, B, M]) MustGetByBasic(basic B) *Enum[P, B, M]
- func (c *Enums[P, B, M]) MustGetByCode(code int32) *Enum[P, B, M]
- func (c *Enums[P, B, M]) MustGetByName(name string) *Enum[P, B, M]
- func (c *Enums[P, B, M]) MustGetByProto(proto P) *Enum[P, B, M]
- func (c *Enums[P, B, M]) SetDefault(enum *Enum[P, B, M])
- func (c *Enums[P, B, M]) SetDefaultBasic(basic B)
- func (c *Enums[P, B, M]) SetDefaultProto(proto P)
- func (c *Enums[P, B, M]) SetDefaultValid(valid bool)
- func (c *Enums[P, B, M]) UnsetDefault()
- func (c *Enums[P, B, M]) WithDefault(enum *Enum[P, B, M]) *Enums[P, B, M]
- func (c *Enums[P, B, M]) WithDefaultBasic(basic B) *Enums[P, B, M]
- func (c *Enums[P, B, M]) WithDefaultCode(code int32) *Enums[P, B, M]
- func (c *Enums[P, B, M]) WithDefaultName(name string) *Enums[P, B, M]
- func (c *Enums[P, B, M]) WithDefaultProto(proto P) *Enums[P, B, M]
- func (c *Enums[P, B, M]) WithDefaultValid(valid bool) *Enums[P, B, M]
- func (c *Enums[P, B, M]) WithUnsetDefault() *Enums[P, B, M]
- type MetaDesc
- type MetaNone
- type ProtoEnum
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Enum ¶
type Enum[protoEnum ProtoEnum, basicEnum comparable, metaType any] struct { // contains filtered or unexported fields }
Enum wraps a Protocol Buffer enum with Go native enum and custom metadata Bridges protobuf enum (protoEnum) with Go native enum (basic) via Basic() method Associates custom metadata with the enum value via Meta() method Uses triple generics to maintain type checking across protobuf, Go native enum, and metadata
Enum 使用 Go 原生枚举和自定义元数据包装 Protocol Buffer 枚举 通过 Basic() 方法桥接 protobuf 枚举 (protoEnum) 和 Go 原生枚举 (basic) 通过 Meta() 方法关联枚举值与自定义元数据 使用三泛型在 protobuf、Go 原生枚举和元数据类型间保持类型安全
func NewEnum ¶
func NewEnum[protoEnum ProtoEnum, basicEnum comparable](proto protoEnum, basic basicEnum) *Enum[protoEnum, basicEnum, *MetaNone]
NewEnum creates a new Enum instance binding protobuf enum with Go native enum Use this when you just need enum mapping without description The basic param accepts Go native enum type (e.g. type StatusType string) Returns a reference to the created Enum instance, supporting chained invocation
创建新的 Enum 实例,绑定 protobuf 枚举与 Go 原生枚举 当只需要枚举映射而不需要描述时使用此函数 basic 参数接受 Go 原生枚举类型(如 type StatusType string) 返回创建的 Enum 实例指针以便链式调用
func NewEnumWithDesc ¶
func NewEnumWithDesc[protoEnum ProtoEnum, basicEnum comparable](proto protoEnum, basic basicEnum, description string) *Enum[protoEnum, basicEnum, *MetaDesc]
NewEnumWithDesc creates a new Enum instance with protobuf enum, Go native enum, and description Use this when you need both enum mapping and human-readable description The basic param accepts Go native enum type (e.g. type StatusType string) The description param provides custom description used in docs and UI rendering
创建带有 protobuf 枚举、Go 原生枚举和描述的新 Enum 实例 当需要枚举映射和人类可读描述时使用此函数 basic 参数接受 Go 原生枚举类型(如 type StatusType string) description 参数提供用于文档和显示的自定义描述
func NewEnumWithMeta ¶
func NewEnumWithMeta[protoEnum ProtoEnum, basicEnum comparable, metaType any](proto protoEnum, basic basicEnum, meta metaType) *Enum[protoEnum, basicEnum, metaType]
NewEnumWithMeta creates a new Enum instance with protobuf enum, Go native enum, and custom metadata Use this when you need customized metadata types beyond simple string description The basic param accepts Go native enum type (e.g. type StatusType string) The meta param accepts custom metadata types (e.g. i18n descriptions with multiple languages)
创建带有 protobuf 枚举、Go 原生枚举和自定义元数据的新 Enum 实例 当需要超越简单字符串描述的灵活元数据类型时使用此函数 basic 参数接受 Go 原生枚举类型(如 type StatusType string) meta 参数接受任意自定义元数据类型(如双语描述)
func (*Enum[protoEnum, basicEnum, metaType]) Basic ¶
func (c *Enum[protoEnum, basicEnum, metaType]) Basic() basicEnum
Basic returns the Go native enum value associated with this enum Enables type-safe conversion from protobuf enum to Go native enum (e.g. type StatusType string) Use this to get the basic enum value when working with Go native enum patterns Bridges protobuf enums with existing Go enum-based business logic with ease
返回与此枚举关联的 Go 原生枚举值 实现从 protobuf 枚举到 Go 原生枚举的类型安全转换(如 type StatusType string) 在使用 Go 原生枚举模式时使用此方法获取 basic 枚举值 无缝桥接 protobuf 枚举与现有基于 Go 枚举的业务逻辑
func (*Enum[protoEnum, basicEnum, metaType]) Code ¶
Code returns the numeric code of the enum as int32 Converts the Protocol Buffer enum value to a standard int32 type
返回枚举的数字代码作 int32 将 Protocol Buffer 枚举数字转换成标准 int32 类型
func (*Enum[protoEnum, basicEnum, metaType]) Meta ¶
func (c *Enum[protoEnum, basicEnum, metaType]) Meta() metaType
Meta returns the metadata associated with this enum Provides access to custom metadata like description via MetaDesc Use this when you need to access extended enum metadata
返回与此枚举关联的元数据 提供对自定义元数据(如通过 MetaDesc 获取描述)的访问 在需要访问额外的枚举元数据时使用此方法
func (*Enum[protoEnum, basicEnum, metaType]) Name ¶
Name returns the string name of the enum value Gets the Protocol Buffer enum's string representation
返回枚举值的字符串名称 获取 Protocol Buffer 枚举的字符串表示
func (*Enum[protoEnum, basicEnum, metaType]) Proto ¶
func (c *Enum[protoEnum, basicEnum, metaType]) Proto() protoEnum
Proto returns the underlying Protocol Buffer enum value Provides access to the source enum enabling Protocol Buffer operations
返回底层的 Protocol Buffer 枚举值 提供对源枚举的访问以进行 Protocol Buffer 操作
type Enums ¶
type Enums[P ProtoEnum, B comparable, M any] struct { // contains filtered or unexported fields }
Enums manages a collection of Enum instances with indexed lookups Maintains multiple maps enabling efficient lookup using different identifiers Provides O(1) lookup when searching proto, code, name, and basic value Includes a configurable default value returned when lookups miss
Enums 管理 Enum 实例集合并提供索引查找 维护四个映射表以通过不同标识符高效检索 为 proto、代码、名称和 basic 枚举值搜索提供 O(1) 查找性能 支持在查找失败时返回可选的默认值
func NewEnums ¶
func NewEnums[P ProtoEnum, B comparable, M any](params ...*Enum[P, B, M]) *Enums[P, B, M]
NewEnums creates a new Enums collection from the given Enum instances Builds indexed maps enabling efficient lookup using proto, code, name, and basic value The first item becomes the default value if provided Returns a reference to the created Enums collection, usable in lookup operations
从给定的 Enum 实例创建新的 Enums 集合 构建索引映射以通过 proto、代码、名称和 basic 枚举值高效查找 如果提供了参数,第一个项成为默认值 返回创建的 Enums 集合指针,可用于各种查找操作
func (*Enums[P, B, M]) GetByBasic ¶
GetByBasic finds an Enum using its Go native enum value Performs direct map lookup using the basic enum value Returns default value if no enum with the given basic enum exists Panics if no default value has been configured
通过 Go 原生枚举值检索 Enum 使用 basic 枚举值执行直接映射查找 如果不存在具有给定 basic 枚举的枚举则返回默认值 如果未配置默认值则会 panic
func (*Enums[P, B, M]) GetByCode ¶
GetByCode finds an Enum using its numeric code Performs direct map lookup using the int32 code value Returns default value if no enum with the given code exists Panics if no default value has been configured
通过数字代码检索 Enum 使用 int32 代码值执行直接映射查找 如果不存在具有给定代码的枚举则返回默认值 如果未配置默认值则会 panic
func (*Enums[P, B, M]) GetByName ¶
GetByName finds an Enum using its string name Performs direct map lookup using the enum name string Returns default value if no enum with the given name exists Panics if no default value has been configured
通过字符串名称检索 Enum 使用枚举名称字符串执行直接映射查找 如果不存在具有给定名称的枚举则返回默认值 如果未配置默认值则会 panic
func (*Enums[P, B, M]) GetByProto ¶
GetByProto finds an Enum using its Protocol Buffer enum value Uses the enum's numeric code when searching in the collection Returns default value if the enum is not found in the collection Panics if no default value has been configured
通过 Protocol Buffer 枚举值检索 Enum 使用枚举的数字代码在集合中查找 如果在集合中找不到枚举则返回默认值 如果未配置默认值则会 panic
func (*Enums[P, B, M]) GetDefault ¶
GetDefault returns the current default Enum value Panics if no default value has been configured
返回当前的默认 Enum 值 如果未配置默认值则会 panic
func (*Enums[P, B, M]) GetDefaultBasic ¶
func (c *Enums[P, B, M]) GetDefaultBasic() B
GetDefaultBasic returns the basicEnum value of the default Enum Panics if no default value has been configured
返回默认 Enum 的 basicEnum 值 如果未配置默认值则会 panic
func (*Enums[P, B, M]) GetDefaultProto ¶
func (c *Enums[P, B, M]) GetDefaultProto() P
GetDefaultProto returns the protoEnum value of the default Enum Panics if no default value has been configured
返回默认 Enum 的 protoEnum 值 如果未配置默认值则会 panic
func (*Enums[P, B, M]) ListBasics ¶
func (c *Enums[P, B, M]) ListBasics() []B
ListBasics returns a slice containing each basicEnum value in the defined sequence Maintains the same sequence as enum values were registered
返回一个包含各 basicEnum 值的切片,次序与定义时一致 保持枚举值注册时的顺序
func (*Enums[P, B, M]) ListProtos ¶
func (c *Enums[P, B, M]) ListProtos() []P
ListProtos returns a slice containing each protoEnum value in the defined sequence Maintains the same sequence as enum values were registered
返回一个包含各 protoEnum 值的切片,次序与定义时一致 保持枚举值注册时的顺序
func (*Enums[P, B, M]) ListValidBasics ¶
func (c *Enums[P, B, M]) ListValidBasics() []B
ListValidBasics returns a slice excluding the default basicEnum value. If no default value is configured, returns each basicEnum value.
返回一个切片,排除默认 basicEnum 值,其余按定义次序排列。 如果未配置默认值,则返回所有 basicEnum 值。
func (*Enums[P, B, M]) ListValidProtos ¶
func (c *Enums[P, B, M]) ListValidProtos() []P
ListValidProtos returns a slice excluding the default protoEnum value. If no default value is configured, returns each protoEnum value.
返回一个切片,排除默认 protoEnum 值,其余按定义次序排列。 如果未配置默认值,则返回所有 protoEnum 值。
func (*Enums[P, B, M]) LookupByBasic ¶
LookupByBasic finds an Enum using its Go native enum value Returns the Enum and true if found, nil and false otherwise Use this when you need to check existence before accessing the value
通过 Go 原生枚举值查找 Enum 找到时返回 Enum 和 true,否则返回 nil 和 false 当需要在访问值之前检查是否存在时使用此方法
func (*Enums[P, B, M]) LookupByCode ¶
LookupByCode finds an Enum using its numeric code Returns the Enum and true if found, nil and false otherwise Use this when you need to check existence before accessing the value
通过数字代码查找 Enum 找到时返回 Enum 和 true,否则返回 nil 和 false 当需要在访问值之前检查是否存在时使用此方法
func (*Enums[P, B, M]) LookupByName ¶
LookupByName finds an Enum using its string name Returns the Enum and true if found, nil and false otherwise Use this when you need to check existence before accessing the value
通过字符串名称查找 Enum 找到时返回 Enum 和 true,否则返回 nil 和 false 当需要在访问值之前检查是否存在时使用此方法
func (*Enums[P, B, M]) LookupByProto ¶
LookupByProto finds an Enum using its Protocol Buffer enum value Returns the Enum and true if found, nil and false otherwise Use this when you need to check existence before accessing the value
通过 Protocol Buffer 枚举值查找 Enum 找到时返回 Enum 和 true,否则返回 nil 和 false 当需要在访问值之前检查是否存在时使用此方法
func (*Enums[P, B, M]) MustGetByBasic ¶
MustGetByBasic finds an Enum using its Go native enum value Panics if no enum with the given basic enum exists
通过 Go 原生枚举值检索 Enum 如果不存在具有给定 basic 枚举的枚举则会 panic
func (*Enums[P, B, M]) MustGetByCode ¶
MustGetByCode finds an Enum using its numeric code Panics if no enum with the given code exists
通过数字代码检索 Enum 如果不存在具有给定代码的枚举则会 panic
func (*Enums[P, B, M]) MustGetByName ¶
MustGetByName finds an Enum using its string name Panics if no enum with the given name exists
通过字符串名称检索 Enum 如果不存在具有给定名称的枚举则会 panic
func (*Enums[P, B, M]) MustGetByProto ¶
MustGetByProto finds an Enum using its Protocol Buffer enum value Panics if the enum is not found in the collection
通过 Protocol Buffer 枚举值检索 Enum 如果在集合中找不到枚举则会 panic
func (*Enums[P, B, M]) SetDefault ¶
SetDefault sets the default Enum value to return when lookups miss Allows dynamic configuration of the fallback value post creation Panics if defaultEnum is nil, use UnsetDefault to remove the default value
设置查找失败时返回的默认 Enum 值 允许在创建后动态配置回退值 如果 defaultEnum 为 nil 则会 panic,使用 UnsetDefault 清除默认值
func (*Enums[P, B, M]) SetDefaultBasic ¶
func (c *Enums[P, B, M]) SetDefaultBasic(basic B)
SetDefaultBasic sets the default using a Go native enum value Panics if the specified basic enum is not found in the collection
使用 Go 原生枚举值设置默认值 如果指定的 basic 枚举不存在则会 panic
func (*Enums[P, B, M]) SetDefaultProto ¶
func (c *Enums[P, B, M]) SetDefaultProto(proto P)
SetDefaultProto sets the default using a Protocol Buffer enum value Panics if the specified proto enum is not found in the collection
使用 Protocol Buffer 枚举值设置默认值 如果指定的 proto 枚举不存在则会 panic
func (*Enums[P, B, M]) SetDefaultValid ¶
SetDefaultValid marks the default value as active when true When active, ListValidProtos and ListValidBasics include the default Panics if no default value exists, panics if defaultValid has been set
标记默认值是否应被视为有效 当 valid 为 true 时,ListValidProtos 和 ListValidBasics 包含默认值 如果无默认值或 defaultValid 已设置则会 panic
func (*Enums[P, B, M]) UnsetDefault ¶
func (c *Enums[P, B, M]) UnsetDefault()
UnsetDefault unsets the default Enum value Once invoked, GetByXxx lookups panic if not found Panics if no default value exists at the moment
取消设置默认 Enum 值 调用此方法后,GetByXxx 查找失败时会 panic 如果当前无默认值则会 panic
func (*Enums[P, B, M]) WithDefault ¶
WithDefault sets the default Enum value and returns the Enums instance Enables fluent chain-style configuration during initialization Convenient when setting defaults in package-scope variable declarations
设置默认 Enum 值并返回 Enums 实例 支持初始化时的流式链式配置 适用于在全局变量声明中设置默认值
func (*Enums[P, B, M]) WithDefaultBasic ¶
WithDefaultBasic sets the default using a basic enum and returns the Enums instance Convenient chain method when you know the default Go native enum value Panics if the specified basic enum is not found in the collection
使用 basic 枚举设置默认值并返回 Enums 实例 当你知道默认 Go 原生枚举值时的便捷链式方法 如果指定的 basic 枚举不存在则会 panic
func (*Enums[P, B, M]) WithDefaultCode ¶
WithDefaultCode sets the default using a numeric code and returns the Enums instance Convenient chain method when you know the default enum code Panics if the specified code is not found in the collection
使用数字代码设置默认值并返回 Enums 实例 当你知道默认枚举代码时的便捷链式方法 如果指定的代码不存在则会 panic
func (*Enums[P, B, M]) WithDefaultName ¶
WithDefaultName sets the default using an enum name and returns the Enums instance Convenient chain method when you know the default enum name Panics if the specified name is not found in the collection
使用枚举名称设置默认值并返回 Enums 实例 当你知道默认枚举名称时的便捷链式方法 如果指定的名称不存在则会 panic
func (*Enums[P, B, M]) WithDefaultProto ¶
WithDefaultProto sets the default using a proto enum and returns the Enums instance Convenient chain method when you know the default Protocol Buffer enum value Panics if the specified proto enum is not found in the collection
使用 proto 枚举设置默认值并返回 Enums 实例 当你知道默认 Protocol Buffer 枚举值时的便捷链式方法 如果指定的 proto 枚举不存在则会 panic
func (*Enums[P, B, M]) WithDefaultValid ¶
WithDefaultValid marks the default as active and returns the Enums instance When active, ListValidProtos and ListValidBasics include the default Convenient chain method to configure default active state at initialization
标记默认值为有效并返回 Enums 实例 当 valid 为 true 时,ListValidProtos 和 ListValidBasics 包含默认值 用于初始化时配置默认值有效性的便捷链式方法
func (*Enums[P, B, M]) WithUnsetDefault ¶
WithUnsetDefault unsets the default Enum value and returns the Enums instance Enables fluent chain-style configuration to remove default value Once invoked, GetByXxx lookups panic if not found
取消设置默认 Enum 值并返回 Enums 实例 支持流式链式配置以移除默认值 之后 GetByXxx 查找失败时会 panic
type MetaDesc ¶
type MetaDesc struct {
// contains filtered or unexported fields
}
MetaDesc represents metadata with string description attached to enums
MetaDesc 代表带字符串描述的枚举元数据
type MetaNone ¶
type MetaNone struct{}
MetaNone represents blank metadata when enums have no description
MetaNone 代表无描述枚举的空元数据
type ProtoEnum ¶
type ProtoEnum interface {
// String provides the standard name of the enum value as defined in protobuf schema
// Important when performing serialization, debugging, and human-readable enum identification
// String 提供 protobuf 模式中定义的枚举值规范名称标识符
// 在进行序列化、调试和人类可读的枚举识别时至关重要
String() string
// Number exposes the underlying numeric wire-format encoding used in protobuf serialization
// Enables efficient storage, transmission, and support with protobuf specifications
// Number 暴露 protobuf 序列化中使用的底层数值线格式编码
// 实现高效存储、传输以及与 protobuf 规范的兼容性
Number() protoreflect.EnumNumber
// comparable constraint matches protobuf enum patterns and enables map-index usage
// protobuf enums can be compared, this constraint maintains type-safe operations
// comparable 约束与 protobuf 枚举行为一致,并支持作为 map 键使用
// Protocol Buffer 枚举本身可比较,此约束确保类型安全
comparable
}
ProtoEnum establishes the core contract enabling Protocol Buffer enum integration Serves as the generic constraint enabling type-safe enum operations across each protobuf enum Bridges the native protobuf enum system with enhanced metadata management capabilities Important when maintaining compile-time type checking while adding runtime descriptive features
ProtoEnum 建立 Protocol Buffer 枚举集成的基础契约 作为泛型约束实现跨所有 protobuf 枚举的类型安全包装操作 在原生 protobuf 枚举系统与增强元数据管理能力之间建立桥梁 在添加运行时描述特性的同时保持编译时类型安全至关重要
Directories
¶
| Path | Synopsis |
|---|---|
|
internal
|
|
|
demos/demo1x
command
|
|
|
demos/demo2x
command
|
|
|
demos/demo3x
command
|
|
|
utils
Package utils: Private utilities handling pointers and zero values Provides generic functions to handle pointers and value extraction
|
Package utils: Private utilities handling pointers and zero values Provides generic functions to handle pointers and value extraction |
|
protos
|
|