Skip to content

Riven-Spell/enum

Repository files navigation

enum

Go Reference GitHub go.mod Go version

Yet another Golang enum implementation. Follows a pattern I developed in the course of work.

Using a standard EnumImpl

The two type parameters are the result type of the enum, and the parent struct itself.

package foobar

import (
	"github.com/Riven-Spell/enum/v2"
)

// Define our enum in a non-exported type
type eTestInt struct {
	// Value type, then enum struct type.
	// We do this as a embedded value to expose the functions.
	enum.EnumImpl[TestInt, eTestInt]
}

// Export our enum
var ETestInt eTestInt

// Define our value, exported
type TestInt int

// Export a stringifier
func (t TestInt) String() string {
	return ETestInt.String(t)
}

// Define our values
func (eTestInt) Foo() TestInt {
	return 1
}

func (eTestInt) Bar() TestInt {
	return 2
}

func (eTestInt) Baz() TestInt {
	return 3
}

Using a BitflagEnumImpl

Both BitflagEnumImpl and BitflagImpl take their backing uint type, the resulting flag type itself, and the parent enum struct.

Not returning bitflags by literal value adds a little complexity, but allows us to export convenience functions with it.

If you need the byte-for-byte value for whatever reason, BitflagImpl exports Value() Raw.

package foobar

import (
	"github.com/Riven-Spell/enum/v2"
)

type eTestBitflag struct {
	enum.BitflagEnumImpl[uint16, TestBitflag, eTestBitflag]
}

var ETestBitFlag = eTestBitflag{}

func (e eTestBitflag) Foo() TestBitflag {
	return e.FromRawValue(1)
}

func (e eTestBitflag) Bar() TestBitflag {
	return e.FromRawValue(1 << 1)
}

func (e eTestBitflag) Baz() TestBitflag {
	return e.FromRawValue(1 << 2)
}

type TestBitflag struct {
	enum.BitflagImpl[uint16, TestBitflag, eTestBitflag]
}

func (t TestBitflag) String() string {
	return ETestBitFlag.String(t)
}

About

Yet Another Enum implementation for Golang

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages