go-enumerator is a tool for generating compile-time safe enumerations in Go. It follows the sealed interface pattern to forbid value manipulation outside the package.
Project main goal is to deliver a simple, easy-to-use, reliable tool for generating compile-time safe enumerations in Go.
-
Provide true Algebraic Data Type enumeration implementation.
-
Support multiple usage contexts (e.g. XML marshalling, database storage).
Use standard go install command to install go-enumerator executable:
go install github.com/tompaz3/go-enumeratorThis will install the go-enumerator executable in your $GOPATH/bin directory.
go-enumerator generates Go compile-time safe enumerations. It is encouraged to create enums in their own, separate packages to forbid value manipulation.
To generate the enum use the go-enumerator binary directly or through //go:generate directive.
Generate using go-enumerator binary:
# generate Color enum with Undefined,Red,Green,Blue values
# with JSON marshalling support, undefined value
# and deserialize unknown values to Undefined
go-enumerator -destination ./color/color.go -package color -type Color -values Undefined,Red,Green,Blue -undefined Undefined -marshal-json -unmarshal-json-to-undefined --copyright ../../LICENSEGenerate using //go:generate directive:
// generate Color enum with Undefined,Red,Green,Blue values
// with JSON marshalling support, undefined value
// and deserialize unknown values to Undefined
//go:generate go-enumerator -destination ./color/color.go -package color -type Color -values Undefined,Red,Green,Blue -undefined Undefined -marshal-json -unmarshal-json-to-undefined --copyright ../../LICENSEgo-enumerator supports the following arguments:
| Argument | Default value | Description | Examples |
|---|---|---|---|
destination |
"" |
Optional: Destination file path. os.Stdout if empty |
|
package |
"" |
Required: Package name |
|
type |
"" |
Required: Enum type name |
|
values |
"" |
Required: Enum values separated by comma |
|
undefined |
"" |
Optional: Enum undefined value (used for |
|
marshal-json |
false |
Optional: Generate JSON marshalling methods |
|
unmarshal-json-to-undefined |
false |
Optional: Deserialize unknown values to |
|
copyright |
"" |
Optional: Copyright notice to be included in the generated file |
|
go-check-sumtype |
false |
Optional: Add |
|
version |
false |
Optional: Print version. Will ignore all the other flags and simply print the executable version. |
|
color.go file is the example generated file - using the //go:generate command as specified above (Generate using go:generate directive).
The generated file will consist of:
-
Copyright notice (if
copyrightparameter specified) -
Package declaration
-
Enum interface definition with the
typename,sealedType()(sealed function),String() stringandToJSONMarshallable() MarshallableTypefunctions (see MarshallableType) for details. -
Base struct implementation
-
Global variable declarations with enum values
-
String() stringfunction -
Values() []Typefunction -
Of(name string) (Type, bool)function implementation for mapping the enum based on the string value -
OfOrUndefined(name string) Typefunction implementation for mapping the enum based on the string value, returningundefinedif the value is not found - only ifundefinedparameter is specified -
ToJSONMarshallable() MarshallableTypefunction to change this enum to JSON marshallable type - only ifmarshal-jsonparameter is specified
-
-
MarshallableTypetype for JSON marshalling. Separate type is used asjson.Unmarshalerrequires pointer receiver. If you ever want to use the enum in a struct that implementsjson.Marshalerorjson.Unmarshaler, use the relatedMarshallableTypetype.-
MarshalJSON() ([]byte, error)function implementation for JSON marshalling. -
UnmarshalJSON(data []byte) errorfunction implementation for JSON unmarshalling.
-
-
InvalidTypeNameErr- error for invalid enum type name, returned byOf(name string) (Type, error)function
Enum has global variables with enum values, which can be used in a type-safe manner.
package color
var (
Undefined = baseColor{name: "Undefined"} // Undefined value
Red = baseColor{name: "Red"} // Red value
Green = baseColor{name: "Green"} // Green value
Blue = baseColor{name: "Blue"} // Blue value
)-
String() string— transforms enum tostringvalue (implementsfmt.Stringerinterface) -
Values() []Type— returns a new slice consisting of all the values of this enum. -
Of(name string) (Type, error)— mapsstringvalue to enum value. Returns the enum value orInvalidColorNameErrif the value is not found. -
OfOrUndefined(name string) Type— mapsstringvalue to enum value. Returns the enum value orUndefinedif the value is not found. -
ToJSONMarshallable() MarshallableType— transforms enum toMarshallableType(implementsjson.Marshalerandjson.Unmarshalerinterfaces)
MarshallableColor is a special type for JSON marshalling. Standard Color enum (interface) does not support JSON marshalling. To marshal the enum, use the MarshallableColor intermediate type.
-
MarshalJSON() ([]byte, error)- marshals the enum to JSON. -
UnmarshalJSON(data []byte) error- unmarshals the enum from JSON. -
ToEnum() Color- convertsMarshallableColortoColorenum.
The generator is licensed under the MIT License. License available at LICENSE.
No contribution policy has been defined yet. It is a tiny, single-contributor project.
The project is considered feature-complete at the moment. Most likely, will be updated for bug fixing and vulnerability patches only.
In case the author cannot maintain the project, a new strategy will be created to keep the project alive.