enum

package
v2.16.0 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2022 License: BSD-3-Clause Imports: 4 Imported by: 1

Documentation

Overview

package enum provides an API for manipulating enumerations.

Index

Constants

This section is empty.

Variables

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

AllRepresentations lists all 4 values in order.

Functions

func QuotedString added in v2.15.0

func QuotedString(s string) []byte

Types

type Enum

type Enum interface {
	Ordinal() int
	String() string
	Tag() 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.

func (Enums) Tags added in v2.8.0

func (es Enums) Tags() []string

Tags gets the tag values of the enums in the same order. If no tags have been defined, this returns the same as Strings.

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.

func (FloatEnums) Tags added in v2.8.0

func (es FloatEnums) Tags() []string

Tags gets the tag values of the enums in the same order. If no tags have been defined, this returns the same as Strings.

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.

func (IntEnums) Tags added in v2.8.0

func (es IntEnums) Tags() []string

Tags gets the tag values of the enums in the same order. If no tags have been defined, this returns the same as Strings.

type Representation

type Representation int
const (
	Identifier Representation = iota
	Tag
	Number // the value of the enumerant as a decimal number
	Ordinal
)

func AsRepresentation added in v2.10.0

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 added in v2.10.0

func MustParseRepresentation(s string) Representation

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

func RepresentationOf added in v2.10.0

func RepresentationOf(i 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) Int added in v2.10.0

func (i Representation) Int() int

Int returns the int value, which is not necessarily the same as the ordinal. It serves to facilitate polymorphism (see enum.IntEnum).

func (Representation) IsValid added in v2.10.0

func (i Representation) IsValid() bool

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

func (Representation) Ordinal added in v2.10.0

func (i 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 added in v2.10.0

func (v *Representation) Parse(s 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 representationMarshalTextRep. It is used by AsRepresentation. The input case does not matter.

Usage Example

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

func (Representation) String added in v2.10.0

func (i Representation) String() string

String returns the literal string representation of a Representation, which is the same as the const identifier.

func (Representation) Tag added in v2.10.0

func (i Representation) Tag() string

Tag returns the string representation of a Representation. This is an alias for String.

Jump to

Keyboard shortcuts

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