enumer/README.md

84 lines
2.9 KiB
Markdown
Raw Normal View History

2015-12-29 16:29:35 +03:00
#Enumer
2016-01-21 13:49:12 +03:00
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](https://godoc.org/golang.org/x/tools/cmd/stringer).
2015-12-29 16:27:33 +03:00
2016-01-21 02:18:14 +03:00
##Generated functions and methods
When Enumer is applied to a type, it will generate three methods and one function:
* 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,
2015-12-31 17:36:39 +03:00
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.
2016-01-21 02:15:57 +03:00
* And two more methods, `MarshalJSON()` and `UnmarshalJSON()`, that makes the enum conform
the `json.Marshaler` and `json.Unmarshaler` interfaces. Very useful to use it in JSON APIs.
2015-12-31 17:36:39 +03:00
For example, if we have an enum type called `Pill`,
```go
2015-12-31 17:36:39 +03:00
type Pill int
2015-12-29 16:27:33 +03:00
2015-12-31 17:36:39 +03:00
const (
Placebo Pill = iota
Aspirin
Ibuprofen
Paracetamol
Acetaminophen = Paracetamol
)
```
executing `enumer -type=Pill` will generate a new file with four methods:
```go
2015-12-29 16:27:33 +03:00
func (i Pill) String() string {
//...
}
func PillString(s string) (Pill, error) {
//...
}
func (i Pill) MarshalJSON() ([]byte, error) {
//...
}
func (i *Pill) UnmarshalJSON(data []byte) error {
//...
}
2015-12-29 16:27:33 +03:00
```
2015-12-31 17:36:39 +03:00
From now on, we can:
```go
2015-12-31 17:36:39 +03:00
// 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
2016-01-21 02:15:57 +03:00
// 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"`
2015-12-31 17:36:39 +03:00
```
The generated code is exactly the same as the Stringer tool plus the mentioned additions, so you can use
2015-12-31 17:36:39 +03:00
**Enumer** where you are already using **Stringer** without any code change.
2015-12-29 16:27:33 +03:00
2015-12-31 17:36:39 +03:00
## How to use
The usage of Enumer is the same as Stringer, so you can refer to the [Stringer docs](https://godoc.org/golang.org/x/tools/cmd/stringer)
for more information.
2016-01-21 02:15:57 +03:00
There is only one flag added: `noJSON`. If this flag is set to true (i.e. `enumer -type=Pill -noJSON`),
the JSON related methods won't be generated.
## Additional functions of this fork
This fork additionally implements the Scanner and Valuer interface to use a enum seamlessly in a database model.
2016-01-21 02:18:14 +03:00
## Inspiring projects
* [Stringer](https://godoc.org/golang.org/x/tools/cmd/stringer)
* [jsonenums](https://github.com/campoy/jsonenums)