enum

package module
v0.0.0-...-de5f122 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2022 License: Apache-2.0 Imports: 6 Imported by: 0

README

enum

Easy to use generic named enum library for go.

Introduction

This library implements support for named enums without requiring code generation this is achieved by the obvious approach of requiring the user to give enum names (intead of values) and values (IDs in the context of this library) are auto-generated per associated type starting from 0 and monotonicaly increasing in declaration order.

Creating Enums

Here is how enums are usually created with Go:

type MyType int

const (
    Unknown MyType = iota  // 0
    One                    // 1
    Two                    // 2
)

In the simple example above, enums have no associated names whatsoever and one would have to manually keep track of names if they needed to use those. Libraries like enumer try to solve that by using code generation to add support for keeping track of enum names (plus other niceties).

And here is how they would be created with this library:

type MyType int

var (
    Unknown = enum.New[MyType]("Unknown")  // 0
    One     = enum.New[MyType]("One")      // 1
    Two     = enum.New[MyType]("Two")      // 2
)

Here each enum is associated with a specific type (MyType), has a specific name passed as a parameter to the New call and has an associated auto generated ID.

Using Enums

Below you can find some possibly interesting usage scenarios in the context of enums.

Getting Enum name:

unknownName := Unknown.Name()  // "Unknown"

Getting Enum ID/value:

unknownID := Unknown.ID()  // 0

TODO(bga): Finish this.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Enum

type Enum[T constraints.Integer] struct {
	// contains filtered or unexported fields
}

Enum represents a named Enum that is associaterd with an ID. Enum IDs are auto-generated starting from 0 and monotonically increasing in declaration order. The zero value of an Enum is not valid. It is safe to use this type to create other types (type OtherType Enum[MyEnumType]) as it does not implement any methods itself and, instead, delegates all methods to embedded types.

func EnumByTypeAndName

func EnumByTypeAndName[T constraints.Integer](name string) (Enum[T], error)

EnumByTypeAndName returns the enum associated with the given type and name. If there is no such enum, a non-nil error is returned.

func EnumsByType

func EnumsByType[T constraints.Integer]() []Enum[T]

EnumsByType returns all enums associated with the given type T.

func New

func New[T constraints.Integer](name string) Enum[T]

New returns a new Enum associated with the given name and type T.

func (Enum) ID

func (e Enum) ID() T

ID returns the numeric ID associated with this Enum instance.

func (Enum) MarshalJSON

func (e Enum) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (Enum) MarshalText

func (e Enum) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (Enum) Name

func (e Enum) Name() string

Name returns the name associated with this Enum instance.

func (*Enum) Scan

func (e *Enum) Scan(value any) error

Scan implements the sql.Scanner interface.

func (Enum) String

func (e Enum) String() string

String implements the fmt.Stringer interface.

func (*Enum) UnmarshalJSON

func (e *Enum) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (*Enum) UnmarshalText

func (e *Enum) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

func (*Enum) Valid

func (e *Enum) Valid() bool

Valid returns true if the Enum is valid or false otherwise. Default Enum instances are invalid. Use New to create a valid one (or use the unmarshalling methods to initialize one created in place).

func (Enum) Value

func (e Enum) Value() (driver.Value, error)

Value implements the driver.Valuer interface.

Jump to

Keyboard shortcuts

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