Documentation ¶
Index ¶
- func EnumNames[T EnumDefinition](enums ...T) (names []string)
- func GetEnumMap[T EnumDefinition]() map[string]T
- func GetEnums[T EnumDefinition](names ...string) (res []T)
- func IsValidEnum[T EnumDefinition](name string) bool
- func NewEnum[T EnumDefinition](name string, args ...any) T
- func Size[T EnumDefinition]() int
- func ValueOf[T EnumDefinition](name string) *T
- func ValueOfIgnoreCase[T EnumDefinition](name string) *T
- func Values[T EnumDefinition]() []T
- type Enum
- func (e Enum) Compare(other EnumDefinition) int
- func (e Enum) Equals(other EnumDefinition) bool
- func (e Enum) Init(args ...any) any
- func (e Enum) MarshalJSON() ([]byte, error)
- func (e Enum) MarshalText() (text []byte, err error)
- func (e Enum) Name() string
- func (e Enum) Ordinal() int
- func (e Enum) String() string
- func (e Enum) Type() string
- type EnumDefinition
- type EnumSet
- type UnsafeEnumSet
- func (set *UnsafeEnumSet[E]) Add(e E) bool
- func (set *UnsafeEnumSet[E]) AddRange(begin, end E) int
- func (set *UnsafeEnumSet[E]) Clear()
- func (set *UnsafeEnumSet[E]) Clone() EnumSet[E]
- func (set *UnsafeEnumSet[E]) Contains(enums ...E) bool
- func (set *UnsafeEnumSet[E]) ContainsAll(enumSet EnumSet[E]) bool
- func (set *UnsafeEnumSet[E]) Each(f func(e E) bool)
- func (set *UnsafeEnumSet[E]) Equals(enumSet EnumSet[E]) bool
- func (set *UnsafeEnumSet[E]) IsEmpty() bool
- func (set *UnsafeEnumSet[E]) Len() int
- func (set *UnsafeEnumSet[E]) MarshalJSON() ([]byte, error)
- func (set *UnsafeEnumSet[E]) Names() []string
- func (set *UnsafeEnumSet[E]) Remove(e E) bool
- func (set *UnsafeEnumSet[E]) RemoveRange(begin, end E) int
- func (set *UnsafeEnumSet[E]) String() string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetEnumMap ¶
func GetEnumMap[T EnumDefinition]() map[string]T
GetEnumMap 获取所有枚举,以name->enum map的形式返回
func GetEnums ¶ added in v1.2.0
func GetEnums[T EnumDefinition](names ...string) (res []T)
GetEnums 根据枚举名字列表获得一批枚举
func NewEnum ¶
func NewEnum[T EnumDefinition](name string, args ...any) T
NewEnum 新建枚举, 如果枚举(同类型)已经存在,则会抛出panic,禁止重复创建枚举
func Size ¶ added in v1.1.0
func Size[T EnumDefinition]() int
func ValueOfIgnoreCase ¶
func ValueOfIgnoreCase[T EnumDefinition](name string) *T
ValueOfIgnoreCase 忽略大小写获取枚举, 涉及到一次反射调用,性能比ValueOf略差
Types ¶
type Enum ¶
type Enum struct {
// contains filtered or unexported fields
}
func (Enum) Compare ¶
func (e Enum) Compare(other EnumDefinition) int
func (Enum) Equals ¶
func (e Enum) Equals(other EnumDefinition) bool
func (Enum) MarshalJSON ¶
func (Enum) MarshalText ¶ added in v1.2.0
type EnumDefinition ¶
type EnumDefinition interface { // Stringer 支持打印输出 fmt.Stringer // Marshaler 支持枚举序列化 json.Marshaler // TextMarshaler 支持枚举序列化 encoding.TextMarshaler // Init 枚举初始化。使用方不应该直接调用这个方法。(即使调用也没有意义,这限定为一个值方法) Init(args ...any) any // Name 枚举名称,同一类型枚举应该唯一 Name() string // Equals 枚举对比 Equals(other EnumDefinition) bool // Type 实际的枚举类型 Type() string // Ordinal 获取枚举序数 Ordinal() int // Compare 枚举比较方法 Compare(other EnumDefinition) int }
type EnumSet ¶ added in v1.1.0
type EnumSet[E EnumDefinition] interface { // Stringer 支持标准输出及格式化 fmt.Stringer // Marshaler 支持json序列化 json.Marshaler // Add 往set添加元素,添加成功则返回true,如果已经存在则返回false Add(e E) bool // AddRange 按照枚举的序数,连续添加一段枚举,返回实际添加的数量(排除已经存在的) AddRange(begin, end E) int // Remove 删除元素,删除成功则返回true,如果元素原本不存在则返回false Remove(e E) bool // RemoveRange 按照枚举的序数,连续删除一段枚举,返回实际删除的数量(排除原本不存在的) RemoveRange(begin, end E) int // IsEmpty set是否为空 IsEmpty() bool // Clear 清理set Clear() // Len set内当前的枚举数量 Len() int // Contains 是否包含指定的枚举,只要有1个不存在则返回false Contains(enums ...E) bool // ContainsAll 判断是否包含另外一个enumSet(子集关系) ContainsAll(set EnumSet[E]) bool // Equals 判断两个EnumSet是否相同 Equals(set EnumSet[E]) bool // Each set迭代方法, f方法如果返回false,则中止迭代 Each(f func(e E) bool) // Names 返回set中已有枚举的Name表示 Names() []string // Clone 深拷贝一份set Clone() EnumSet[E] }
EnumSet 枚举set,一般在枚举非常多时使用
type UnsafeEnumSet ¶ added in v1.1.0
type UnsafeEnumSet[E EnumDefinition] struct { // contains filtered or unexported fields }
func NewUnsafeEnumSet ¶ added in v1.1.0
func NewUnsafeEnumSet[E EnumDefinition]() *UnsafeEnumSet[E]
func (*UnsafeEnumSet[E]) Add ¶ added in v1.1.0
func (set *UnsafeEnumSet[E]) Add(e E) bool
func (*UnsafeEnumSet[E]) AddRange ¶ added in v1.1.0
func (set *UnsafeEnumSet[E]) AddRange(begin, end E) int
func (*UnsafeEnumSet[E]) Clear ¶ added in v1.1.0
func (set *UnsafeEnumSet[E]) Clear()
func (*UnsafeEnumSet[E]) Clone ¶ added in v1.1.0
func (set *UnsafeEnumSet[E]) Clone() EnumSet[E]
func (*UnsafeEnumSet[E]) Contains ¶ added in v1.1.0
func (set *UnsafeEnumSet[E]) Contains(enums ...E) bool
func (*UnsafeEnumSet[E]) ContainsAll ¶ added in v1.1.0
func (set *UnsafeEnumSet[E]) ContainsAll(enumSet EnumSet[E]) bool
func (*UnsafeEnumSet[E]) Each ¶ added in v1.1.0
func (set *UnsafeEnumSet[E]) Each(f func(e E) bool)
func (*UnsafeEnumSet[E]) Equals ¶ added in v1.1.0
func (set *UnsafeEnumSet[E]) Equals(enumSet EnumSet[E]) bool
func (*UnsafeEnumSet[E]) IsEmpty ¶ added in v1.1.0
func (set *UnsafeEnumSet[E]) IsEmpty() bool
func (*UnsafeEnumSet[E]) Len ¶ added in v1.1.0
func (set *UnsafeEnumSet[E]) Len() int
func (*UnsafeEnumSet[E]) MarshalJSON ¶ added in v1.1.0
func (set *UnsafeEnumSet[E]) MarshalJSON() ([]byte, error)
func (*UnsafeEnumSet[E]) Names ¶ added in v1.1.0
func (set *UnsafeEnumSet[E]) Names() []string
func (*UnsafeEnumSet[E]) Remove ¶ added in v1.1.0
func (set *UnsafeEnumSet[E]) Remove(e E) bool
func (*UnsafeEnumSet[E]) RemoveRange ¶ added in v1.1.0
func (set *UnsafeEnumSet[E]) RemoveRange(begin, end E) int
func (*UnsafeEnumSet[E]) String ¶ added in v1.1.0
func (set *UnsafeEnumSet[E]) String() string
Click to show internal directories.
Click to hide internal directories.