A Go tool to auto generate methods for your enums
Go to file
Álvaro López Espinosa ad5bc9c3e5 Merge pull request #3 from webguerilla/master
Scanner and Valuer interface
2016-10-26 16:45:19 +01:00
testdata Added a method to the generated code to get the enum value from the string name. Fixed all tests. 2015-12-29 13:14:54 +00:00
README.md Merge remote-tracking branch 'upstream/master' 2016-10-20 13:42:25 +02:00
endtoend_test.go Uncommented commented code by error 2015-12-29 13:48:01 +00:00
enumer.go Added a new flag/feature to generate json marshaling methods 2016-01-19 19:39:33 +00:00
golden_test.go Changed the flag noJSON to json 2016-05-21 13:34:12 +01:00
sql.go bug fix if string instead of []byte is returned from db (this is the 2016-10-21 15:20:44 +02:00
stringer.go Merge remote-tracking branch 'upstream/master' 2016-10-20 13:42:25 +02:00
util_test.go Added a method to the generated code to get the enum value from the string name. Fixed all tests. 2015-12-29 13:14:54 +00:00

README.md

#Enumer Enumer is a tool to generate Go code that adds useful methods to Go enums (constants with a specific type). It started as a fork of Rob Pikes Stringer tool.

##Generated functions and methods When Enumer is applied to a type, it will generate:

  • A method String() that returns the string representation of the enum value. This makes the enum conform the Stringer interface, so whenever you print an enum value, you'll get the string name instead of a number.
  • A function <Type>String(s string) to get the enum value from its string representation. This is useful when you need to read enum values from the command line arguments, from a configuration file, from a REST API request... In short, from those places where using the real enum value (an integer) would be almost meaningless or hard to trace or use by a human.
  • When the flag json is provided, two more methods will be generated, MarshalJSON() and UnmarshalJSON(). Those make the enum conform the json.Marshaler and json.Unmarshaler interfaces. Very useful to use it in JSON APIs.

For example, if we have an enum type called Pill,

type Pill int

const (
	Placebo Pill = iota
	Aspirin
	Ibuprofen
	Paracetamol
	Acetaminophen = Paracetamol
)

executing enumer -type=Pill -json will generate a new file with four methods:

func (i Pill) String() string {
    //...
}

func PillString(s string) (Pill, error) {
    //...
}

func (i Pill) MarshalJSON() ([]byte, error) {
	//...
}

func (i *Pill) UnmarshalJSON(data []byte) error {
	//...
}

From now on, we can:

// Convert any Pill value to string
var aspirinString string = Aspirin.String()
// (or use it in any place where a Stringer is accepted)
fmt.Println("I need ", Paracetamol) // Will print "I need Paracetamol"

// Convert a string with the enum name to the corresponding enum value
pill, err := PillString("Ibuprofen")
if err != nil {
    fmt.Println("Unrecognized pill: ", err)
    return
}
// Now pill == Ibuprofen

// Marshal/unmarshal to/from json strings, either directly or automatically when
// the enum is a field of a struct
pillJSON := Aspirin.MarshalJSON()
// Now pillJSON == `"Aspirin"`

The generated code is exactly the same as the Stringer tool plus the mentioned additions, so you can use Enumer where you are already using Stringer without any code change.

How to use

The usage of Enumer is the same as Stringer, so you can refer to the Stringer docs for more information.

There are two flags added: json and sql. If the json flag is set to true (i.e. enumer -type=Pill -json), the JSON related methods will be generated. And if the sql flag is set to true, the Scanner and Valuer interface will be implemented to seamlessly use the enum in a database model.

Inspiring projects