enums

package module
v0.9.26 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2023 License: BSD-3-Clause Imports: 1 Imported by: 53

README

enums

Enums provides utilities for creating and using enumerated types in Go. There are two main parts of enums: package enums, which defines the Enum and BitFlag interfaces, and the enum code generation tool enumgen, which is based on dmarkham/enumer and stringer.

Add one of the two following comment directives after the type declaration of an enum:

  • //enums:enum -- for standard enum
  • //enums:bitflag -- for bit flag enum (see below for details)

BitFlag enums

This package implements bit flag enums using enum values that specify a bit index for the flag, which is then used with bit shifting to create the actual bit mask. Thus, the enum values are just sequential integers like a normal enum, which allows the names to be looked up using a slice index, and are generally easier to read and understand as simple integers.

The generated String() and SetString() methods operate using the bit-shifted mask values and return the set of active bit names (separated by an OR pipe |) for a given value. Use BitIndexString() to get the name associated with the bit index values (which are typically only used for setting and checking flags).

Documentation

Overview

Package enums provides common interfaces for enums and bit flag enums and utilities for using them

Package enums provides common interfaces for enums and bit flag enums and utilities for using them

Package enums provides common interfaces for enums and bit flag enums and utilities for using them

Index

Constants

View Source
const (
	// Version is the version of this package being used
	Version = "v0.9.26"
	// GitCommit is the commit just before the latest version commit
	GitCommit = "b0e4330"
	// VersionDate is the date-time of the latest version commit in UTC (in the format 'YYYY-MM-DD HH:MM', which is the Go format '2006-01-02 15:04')
	VersionDate = "2023-10-22 17:52"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type BitFlag

type BitFlag interface {
	Enum
	// Has returns whether these flags
	// have the given flag set.
	HasFlag(f BitFlag) bool
	// BitIndexString returns the string
	// representation of the bit flag if
	// the bit flag is a bit index value
	// (typically an enum constant), and
	// not an actual bit flag value.
	BitIndexString() string
}

BitFlag is the interface that all bit flag enum types satisfy. Bit flag enum types support all of the operations that standard enums do, and additionally can check if they have a given bit flag.

type BitFlagSetter added in v0.9.6

type BitFlagSetter interface {
	EnumSetter
	BitFlag
	// Set sets the value of the given
	// flags in these flags to the given value.
	SetFlag(on bool, f ...BitFlag)
	// SetStringOr sets the bit flag from its
	// string representation while preserving any
	// bit flags already set, and returns an
	// error if the string is invalid.
	SetStringOr(s string) error
}

BitFlagSetter is an expanded interface that all pointers to bit flag enum types satisfy. Pointers to bit flag enum types must satisfy all of the methods of EnumSetter and BitFlag, and must also be able to set a given bit flag.

type Enum

type Enum interface {
	fmt.Stringer
	// Int64 returns the enum value as an int64.
	Int64() int64
	// Desc returns the description of the enum value.
	Desc() string
	// IsValid returns whether the value is a
	// valid option for its enum type.
	IsValid() bool
	// Values returns all possible values this
	// enum type has.
	Values() []Enum
}

Enum is the interface that all enum types satisfy. Enum types must be convertable to strings and int64s, must be able to return a description of their value, must be able to report if they are valid, and must be able to return all possible enum values for their type.

type EnumSetter added in v0.9.6

type EnumSetter interface {
	Enum
	// SetString sets the enum value from its
	// string representation, and returns an
	// error if the string is invalid.
	SetString(s string) error
	// SetInt64 sets the enum value from an int64.
	SetInt64(i int64)
}

EnumSetter is an expanded interface that all pointers to enum types satisfy. Pointers to enum types must satisfy all of the methods of Enum, and must also be settable from strings and int64s.

Directories

Path Synopsis
cmd
enumgen
Package main provides the actual command line implementation of the enumgen library.
Package main provides the actual command line implementation of the enumgen library.
Package enumgen provides functions for generating enum methods for enum types.
Package enumgen provides functions for generating enum methods for enum types.

Jump to

Keyboard shortcuts

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