Documentation
¶
Overview ¶
Package enum: Utilities to manage Go native enum with custom metadata Provides type-safe enum descriptors with metadata binding Supports two generics: enumCode and metaType
enum: Go 原生枚举元数据管理工具 提供带元数据绑定的类型安全枚举描述符 支持双泛型:enumCode 和 metaType
Package enum: Collection management to handle Go native enum metadata Provides indexed collections of enum descriptors with lookup methods Enables fast lookup using code value with efficient enum handling
enum: Go 原生枚举元数据集合管理 提供带有查找方法的枚举描述符索引集合 支持按 code 枚举值快速检索,实现高效枚举处理
Index ¶
- type Enum
- type Enums
- func (c *Enums[C, M]) Get(code C) *Enum[C, M]
- func (c *Enums[C, M]) GetDefault() *Enum[C, M]
- func (c *Enums[C, M]) GetDefaultCode() C
- func (c *Enums[C, M]) List() []C
- func (c *Enums[C, M]) ListValid() []C
- func (c *Enums[C, M]) Lookup(code C) (*Enum[C, M], bool)
- func (c *Enums[C, M]) MustGet(code C) *Enum[C, M]
- func (c *Enums[C, M]) SetDefault(enum *Enum[C, M])
- func (c *Enums[C, M]) SetDefaultCode(code C)
- func (c *Enums[C, M]) SetDefaultValid(valid bool)
- func (c *Enums[C, M]) UnsetDefault()
- func (c *Enums[C, M]) WithDefault(enum *Enum[C, M]) *Enums[C, M]
- func (c *Enums[C, M]) WithDefaultCode(code C) *Enums[C, M]
- func (c *Enums[C, M]) WithDefaultValid(valid bool) *Enums[C, M]
- func (c *Enums[C, M]) WithUnsetDefault() *Enums[C, M]
- type MetaDesc
- type MetaNone
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Enum ¶
type Enum[enumCode comparable, metaType any] struct { // contains filtered or unexported fields }
Enum wraps a Go native enum with custom metadata Associates custom metadata with the enum value via Meta() method Uses two generics to maintain type checking across enum and metadata
Enum 使用自定义元数据包装 Go 原生枚举 通过 Meta() 方法关联枚举值与自定义元数据 使用双泛型在枚举和元数据类型间保持类型安全
func NewEnum ¶
func NewEnum[enumCode comparable](code enumCode) *Enum[enumCode, *MetaNone]
NewEnum creates a new Enum instance with Go native enum Use this when you just need enum mapping without description Returns a reference to the created Enum instance
创建新的 Enum 实例,绑定 Go 原生枚举 当只需要枚举映射而不需要描述时使用此函数 返回创建的 Enum 实例指针
func NewEnumWithDesc ¶
func NewEnumWithDesc[enumCode comparable](code enumCode, description string) *Enum[enumCode, *MetaDesc]
NewEnumWithDesc creates a new Enum instance with Go native enum and description Use this when you need both enum mapping and human-readable description The description param provides custom description used in docs and UI
创建带有 Go 原生枚举和描述的新 Enum 实例 当需要枚举映射和人类可读描述时使用此函数 description 参数提供用于文档和显示的自定义描述
func NewEnumWithMeta ¶
func NewEnumWithMeta[enumCode comparable, metaType any](code enumCode, meta metaType) *Enum[enumCode, metaType]
NewEnumWithMeta creates a new Enum instance with Go native enum and custom metadata Use this when you need customized metadata types beyond simple string description The meta param accepts custom metadata types (e.g. i18n descriptions)
创建带有 Go 原生枚举和自定义元数据的新 Enum 实例 当需要超越简单字符串描述的灵活元数据类型时使用此函数 meta 参数接受任意自定义元数据类型(如双语描述)
type Enums ¶
type Enums[C comparable, M any] struct { // contains filtered or unexported fields }
Enums manages a collection of Enum instances with indexed lookups Maintains map enabling efficient lookup using code value Provides O(1) lookup when searching code value Includes a configurable default value returned when lookups miss
Enums 管理 Enum 实例集合并提供索引查找 维护映射表以通过 code 值高效检索 为 code 枚举值搜索提供 O(1) 查找 支持在查找失败时返回可选的默认值
func NewEnums ¶
func NewEnums[C comparable, M any](params ...*Enum[C, M]) *Enums[C, M]
NewEnums creates a new Enums collection from the given Enum instances Builds indexed map enabling efficient lookup using code value The first item becomes the default value if provided Returns a reference to the created Enums collection
从给定的 Enum 实例创建新的 Enums 集合 构建索引映射以通过 code 值高效查找 如果提供了参数,第一个项成为默认值 返回创建的 Enums 集合指针
func (*Enums[C, M]) Get ¶
Get finds an Enum using its code value Returns default value if no enum with the given code exists Panics if no default value has been configured
通过 code 值检索 Enum 如果不存在具有给定 code 的枚举则返回默认值 如果未配置默认值则会 panic
func (*Enums[C, M]) GetDefault ¶
GetDefault returns the current default Enum value Panics if no default value has been configured
返回当前的默认 Enum 值 如果未配置默认值则会 panic
func (*Enums[C, M]) GetDefaultCode ¶
func (c *Enums[C, M]) GetDefaultCode() C
GetDefaultCode returns the code value of the default Enum Panics if no default value has been configured
返回默认 Enum 的 code 值 如果未配置默认值则会 panic
func (*Enums[C, M]) List ¶
func (c *Enums[C, M]) List() []C
List returns a slice containing each code value in the defined sequence Maintains the same sequence as enum values were registered
返回一个包含各 code 值的切片,次序与定义时一致 保持枚举值注册时的顺序
func (*Enums[C, M]) ListValid ¶
func (c *Enums[C, M]) ListValid() []C
ListValid returns a slice excluding the default code value If no default value is configured, returns each code value
返回一个切片,排除默认 code 值,其余按定义次序排列 如果未配置默认值,则返回所有 code 值
func (*Enums[C, M]) Lookup ¶
Lookup finds an Enum using its code value Returns the Enum and true if found, nil and false otherwise Use this when you need to check existence before accessing the value
通过 code 值查找 Enum 找到时返回 Enum 和 true,否则返回 nil 和 false 当需要在访问值之前检查是否存在时使用此方法
func (*Enums[C, M]) MustGet ¶
MustGet finds an Enum using its code value Panics if no enum with the given code exists
通过 code 值检索 Enum 如果不存在具有给定 code 的枚举则会 panic
func (*Enums[C, 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[C, M]) SetDefaultCode ¶
func (c *Enums[C, M]) SetDefaultCode(code C)
SetDefaultCode sets the default using a code value Panics if the specified code is not found in the collection
使用 code 值设置默认值 如果指定的 code 不存在则会 panic
func (*Enums[C, M]) SetDefaultValid ¶
SetDefaultValid marks the default value as active when true When active, Valid() includes the default Panics if no default value exists, panics if defaultValid has been set
标记默认值是否应被视为有效 当 valid 为 true 时,Valid() 包含默认值 如果无默认值或 defaultValid 已设置则会 panic
func (*Enums[C, M]) UnsetDefault ¶
func (c *Enums[C, M]) UnsetDefault()
UnsetDefault unsets the default Enum value Once invoked, Get() lookups panic if not found Panics if no default value exists at the moment
取消设置默认 Enum 值 调用此方法后,Get() 查找失败时会 panic 如果当前无默认值则会 panic
func (*Enums[C, 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[C, M]) WithDefaultCode ¶
WithDefaultCode sets the default using a code value and returns the Enums instance Convenient chain method when you know the default code value Panics if the specified code is not found in the collection
使用 code 值设置默认值并返回 Enums 实例 当你知道默认 code 值时的便捷链式方法 如果指定的 code 不存在则会 panic
func (*Enums[C, M]) WithDefaultValid ¶
WithDefaultValid marks the default as active and returns the Enums instance When active, Valid() includes the default Convenient chain method to configure default active state at initialization
标记默认值为有效并返回 Enums 实例 当 valid 为 true 时,Valid() 包含默认值 用于初始化时配置默认值有效性的便捷链式方法
func (*Enums[C, M]) WithUnsetDefault ¶
WithUnsetDefault unsets the default Enum value and returns the Enums instance Enables fluent chain-style configuration to remove default value Once invoked, Get() lookups panic if not found
取消设置默认 Enum 值并返回 Enums 实例 支持流式链式配置以移除默认值 之后 Get() 查找失败时会 panic
Directories
¶
| Path | Synopsis |
|---|---|
|
internal
|
|
|
demos/demo1x
command
|
|
|
demos/demo2x
command
|
|
|
demos/demo3x
command
|
|
|
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 |