enum

package
v3.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2024 License: BSD-3-Clause Imports: 4 Imported by: 0

Documentation

Overview

package enum provides an API for manipulating enumerations.

Index

Constants

This section is empty.

Variables

View Source
var AllRepresentations = []Representation{
	None, Identifier, Number,
}

AllRepresentations lists all 3 values in order.

Functions

func QuotedString

func QuotedString(s string) []byte

Types

type Enum

type Enum interface {
	Ordinal() int
	String() string
	IsValid() bool
}

Enum is a generic contract for all generated enums.

type Enums

type Enums []Enum

Enums is a slice of Enum.

func (Enums) Ordinals

func (es Enums) Ordinals() []int

Ordinals gets the ordinal values of the enums in the same order.

func (Enums) Strings

func (es Enums) Strings() []string

Strings gets the string values of the enums in the same order.

type FloatEnum

type FloatEnum interface {
	Enum
	Float() float64
}

FloatEnum is a specialisation for those enums that have float32 or float64 as the underlying type.

type FloatEnums

type FloatEnums []FloatEnum

FloatEnums is a slice of FloatEnum.

func (FloatEnums) Floats

func (es FloatEnums) Floats() []float64

Floats gets the values of the enums in the same order.

func (FloatEnums) Ordinals

func (es FloatEnums) Ordinals() []int

Ordinals gets the ordinal values of the enums in the same order.

func (FloatEnums) Strings

func (es FloatEnums) Strings() []string

Strings gets the string values of the enums in the same order.

type IntEnum

type IntEnum interface {
	Enum
	Int() int
}

IntEnum is a specialisation for those enums that have int or similar as the underlying type.

type IntEnums

type IntEnums []IntEnum

IntEnums is a slice of IntEnum.

func (IntEnums) Ints

func (es IntEnums) Ints() []int

Ints gets the values of the enums in the same order.

func (IntEnums) Ordinals

func (es IntEnums) Ordinals() []int

Ordinals gets the ordinal values of the enums in the same order.

func (IntEnums) Strings

func (es IntEnums) Strings() []string

Strings gets the string values of the enums in the same order.

type Representation

type Representation int
const (
	None       Representation = iota // disables the feature (new in v3)
	Identifier                       // uses the main identifier of the corresponding constant
	Number                           // the value of the enumerant as a decimal number
)

func AsRepresentation

func AsRepresentation(s string) (Representation, error)

AsRepresentation parses a string to find the corresponding Representation, accepting either one of the string values or a number. The input representation is determined by representationMarshalTextRep. It wraps Parse. The input case does not matter.

func MustParseRepresentation

func MustParseRepresentation(s string) Representation

MustParseRepresentation is similar to AsRepresentation except that it panics on error. The input case does not matter.

func RepresentationOf

func RepresentationOf(v int) Representation

RepresentationOf returns a Representation based on an ordinal number. This is the inverse of Ordinal. If the ordinal is out of range, an invalid Representation is returned.

func (Representation) IsValid

func (v Representation) IsValid() bool

IsValid determines whether a Representation is one of the defined constants.

func (Representation) Ordinal

func (v Representation) Ordinal() int

Ordinal returns the ordinal number of a Representation. This is an integer counting from zero. It is *not* the same as the const number assigned to the value.

func (*Representation) Parse

func (v *Representation) Parse(in string) error

Parse parses a string to find the corresponding Representation, accepting one of the string values or a number. The input representation is determined by None. It is used by AsRepresentation. The input case does not matter.

Usage Example

v := new(Representation)
err := v.Parse(s)
...  etc

func (Representation) String

func (v Representation) String() string

String returns the literal string representation of a Representation, which is the same as the const identifier but without prefix or suffix.

Jump to

Keyboard shortcuts

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