diff --git a/README.md b/README.md index 6782913..6e4d84e 100644 --- a/README.md +++ b/README.md @@ -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 Pike’s Stringer tool](https://godoc.org/golang.org/x/tools/cmd/stringer). ##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 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, 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. -* 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. +* 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`, ```go @@ -26,7 +26,7 @@ const ( 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 func (i Pill) String() string { //... @@ -72,8 +72,8 @@ 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) for more information. -There are two flags added: `noJSON` and `sql`. If the noJSON flag is set to true (i.e. `enumer -type=Pill -noJSON`), -the JSON related methods won't be generated. And if the sql flag is set to true, the Scanner and Valuer interface will +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 diff --git a/golden_test.go b/golden_test.go index 21d2684..500abd6 100644 --- a/golden_test.go +++ b/golden_test.go @@ -30,7 +30,7 @@ var golden = []Golden{ {"prime", prime_in, prime_out}, } -var goldenJSON = []Golden { +var goldenJSON = []Golden{ {"prime", prime_json_in, prime_json_out}, } diff --git a/stringer.go b/stringer.go index b5c1a01..bdfcb78 100644 --- a/stringer.go +++ b/stringer.go @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// +build go1.5 + // Stringer is a tool to automate the creation of methods that satisfy the fmt.Stringer // interface. Given the name of a (signed or unsigned) integer type T that has constants // defined, stringer will create a new self-contained Go source file implementing @@ -80,8 +82,8 @@ import ( var ( 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") sql = flag.Bool("sql", false, "if true, the Scanner and Valuer interface will be implemented.") + json = flag.Bool("json", false, "if true, json marshaling methods will be generated. Default: false") output = flag.String("output", "", "output file name; default srcdir/_string.go") ) @@ -89,7 +91,7 @@ var ( func Usage() { fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0]) fmt.Fprintf(os.Stderr, "\tstringer [flags] -type T [directory]\n") - fmt.Fprintf(os.Stderr, "\tstringer [flags[ -type T files... # Must be a single package\n") + fmt.Fprintf(os.Stderr, "\tstringer [flags] -type T files... # Must be a single package\n") fmt.Fprintf(os.Stderr, "For more information, see:\n") fmt.Fprintf(os.Stderr, "\thttp://godoc.org/golang.org/x/tools/cmd/stringer\n") fmt.Fprintf(os.Stderr, "Flags:\n") @@ -134,17 +136,17 @@ func main() { g.Printf("\n") g.Printf("import (\n") g.Printf("\t\"fmt\"\n") - if !*noJSON { - g.Printf("\t\"encoding/json\"\n") - } if *sql { g.Printf("\t\"database/sql/driver\"\n") } + if *json { + g.Printf("\t\"encoding/json\"\n") + } g.Printf(")\n") // Run generate for each type. for _, typeName := range types { - g.generate(typeName, !*noJSON) + g.generate(typeName, *json) } // Format the output.