Stringenum
A go tool to auto-generate methods for enum types based on string.
Install / Use
/learn @therne/StringenumREADME
stringenum
A go tool to auto-generate serialization / validation methods for enum types aliasing string.
Features
- Supports JSON, YAML serialization
- Implements
Validatorinterface - Does not support default values yet. But it has good fit with third-party default value modules like creasty/defaults because your types are basically a string!
Installation
You need to install stringenum to generate enum stub codes.
$ go get github.com/therne/stringenum/...
Usage
On the top of your type definition sources, add go generate clause to generate stub codes with stringenum.
+ //go:generate stringenum Kind
package mytype
type Kind string
const (
Apple = Kind("apple")
Google = Kind("google")
)
Then, run go generate to generate stub codes:
$ go generate ./...
Generated Values and Methods
<Type>Values: A list of all available values in the enum.<Type>FromString(string): Casts string into the enum. An error is returned if given string is not defined on the enum.IsValid(): Returns false if the value is not defined on the enum.Validate(): Returns an error if the value is not defined on the enum.MarshalText/UnmarshalText: Implementsencoding.TextMarshaler/TextUnmarshalerinterface for JSON / YAML serialization.String(): Casts the enum into astring. Implementsfmt.Stringerinterface.
