Changed the flag noJSON to json

This commit is contained in:
Alvaro Lopez Espinosa 2016-05-21 13:34:12 +01:00
parent e9e87a4508
commit 986f3e9353
3 changed files with 9 additions and 10 deletions

View File

@ -3,7 +3,7 @@ Enumer is a tool to generate Go code that adds useful methods to Go enums (const
It started as a fork of [Rob Pikes Stringer tool](https://godoc.org/golang.org/x/tools/cmd/stringer). It started as a fork of [Rob Pikes Stringer tool](https://godoc.org/golang.org/x/tools/cmd/stringer).
##Generated functions and methods ##Generated functions and methods
When Enumer is applied to a type, it will generate three methods and one function: 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 * 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. the `Stringer` interface, so whenever you print an enum value, you'll get the string name instead of a number.
@ -11,8 +11,8 @@ the `Stringer` interface, so whenever you print an enum value, you'll get the st
when you need to read enum values from the command line arguments, from a configuration file, 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 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 * When the flag `json` is provided, two more methods will be generated, `MarshalJSON()` and `UnmarshalJSON()`. Those make
the `json.Marshaler` and `json.Unmarshaler` interfaces. Very useful to use it in JSON APIs. 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`, For example, if we have an enum type called `Pill`,
```go ```go
@ -26,7 +26,7 @@ const (
Acetaminophen = Paracetamol Acetaminophen = Paracetamol
) )
``` ```
executing `enumer -type=Pill` will generate a new file with four methods: executing `enumer -type=Pill -json` will generate a new file with four methods:
```go ```go
func (i Pill) String() string { func (i Pill) String() string {
//... //...
@ -72,8 +72,7 @@ The generated code is exactly the same as the Stringer tool plus the mentioned a
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) 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. for more information.
There is only one flag added: `noJSON`. If this flag is set to true (i.e. `enumer -type=Pill -noJSON`), As mentioned before, there is only one flag added: `json`.
the JSON related methods won't be generated.
## Inspiring projects ## Inspiring projects
* [Stringer](https://godoc.org/golang.org/x/tools/cmd/stringer) * [Stringer](https://godoc.org/golang.org/x/tools/cmd/stringer)

View File

@ -30,7 +30,7 @@ var golden = []Golden{
{"prime", prime_in, prime_out}, {"prime", prime_in, prime_out},
} }
var goldenJSON = []Golden { var goldenJSON = []Golden{
{"prime", prime_json_in, prime_json_out}, {"prime", prime_json_in, prime_json_out},
} }

View File

@ -82,7 +82,7 @@ import (
var ( var (
typeNames = flag.String("type", "", "comma-separated list of type names; must be set") typeNames = flag.String("type", "", "comma-separated list of type names; must be set")
noJSON = flag.Bool("noJSON", false, "if true, json marshaling methods will NOT be included. Default: false") json = flag.Bool("json", false, "if true, json marshaling methods will be generated. Default: false")
output = flag.String("output", "", "output file name; default srcdir/<type>_string.go") output = flag.String("output", "", "output file name; default srcdir/<type>_string.go")
) )
@ -134,13 +134,13 @@ func main() {
g.Printf("package %s", g.pkg.name) g.Printf("package %s", g.pkg.name)
g.Printf("\n") g.Printf("\n")
g.Printf("import \"fmt\"\n") // Used by all methods. g.Printf("import \"fmt\"\n") // Used by all methods.
if !*noJSON { if *json {
g.Printf("import \"encoding/json\"\n") g.Printf("import \"encoding/json\"\n")
} }
// Run generate for each type. // Run generate for each type.
for _, typeName := range types { for _, typeName := range types {
g.generate(typeName, !*noJSON) g.generate(typeName, *json)
} }
// Format the output. // Format the output.