mirror of https://github.com/dmarkham/enumer.git
Updated readme with the new JSON additions
This commit is contained in:
parent
8ff8ff6be4
commit
6764143423
39
README.md
39
README.md
|
@ -1,11 +1,18 @@
|
|||
#Enumer
|
||||
Enumer generates Go code to get string names from enum values and viceversa.
|
||||
It is a fork of [Rob Pike’s Stringer tool](https://godoc.org/golang.org/x/tools/cmd/stringer)
|
||||
but adding a *"string to enum value"* method to the generated code.
|
||||
Enumer is a tool to generate Go code that adds useful methods Go enums (constants with a specific type)
|
||||
It started as a fork of [Rob Pike’s Stringer tool](https://godoc.org/golang.org/x/tools/cmd/stringer).
|
||||
|
||||
This is useful when you need to read enum values from the command line arguments, from a configuration file,
|
||||
#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,
|
||||
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
|
||||
be almost meaningless or hard to trace or use by a human.
|
||||
* And two more methods, `MarshalJSON()` and `UnmarshalJSON()`, that makes the enum conform to
|
||||
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`,
|
||||
```go
|
||||
|
@ -19,7 +26,7 @@ const (
|
|||
Acetaminophen = Paracetamol
|
||||
)
|
||||
```
|
||||
executing `enumer -type=Pill` will generate a new file with two methods:
|
||||
executing `enumer -type=Pill` will generate a new file with four methods:
|
||||
```go
|
||||
func (i Pill) String() string {
|
||||
//...
|
||||
|
@ -28,6 +35,14 @@ 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:
|
||||
```go
|
||||
|
@ -43,11 +58,17 @@ if err != nil {
|
|||
return
|
||||
}
|
||||
// Now pill == Ibuprofen
|
||||
|
||||
// Or marshal/unmarshal to/from json strings
|
||||
|
||||
```
|
||||
|
||||
The generated code is exactly the same as the Stringer tool plus the `<Type>String` method, so you can use
|
||||
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, no changes were introduced.
|
||||
For more information please refer to the [Stringer docs](https://godoc.org/golang.org/x/tools/cmd/stringer)
|
||||
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.
|
||||
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.
|
||||
|
||||
|
|
Loading…
Reference in New Issue