Merge remote-tracking branch 'upstream/master'

This commit is contained in:
marco 2016-10-20 13:42:25 +02:00
commit af92007434
3 changed files with 15 additions and 13 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,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) 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 are two flags added: `noJSON` and `sql`. If the noJSON flag is set to true (i.e. `enumer -type=Pill -noJSON`), 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 won't be generated. And if the sql flag is set to true, the Scanner and Valuer interface will 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. be implemented to seamlessly use the enum in a database model.
## Inspiring projects ## Inspiring projects

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

@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // 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 // 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 // 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 // defined, stringer will create a new self-contained Go source file implementing
@ -80,8 +82,8 @@ 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")
sql = flag.Bool("sql", false, "if true, the Scanner and Valuer interface will be implemented.") 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/<type>_string.go") output = flag.String("output", "", "output file name; default srcdir/<type>_string.go")
) )
@ -89,7 +91,7 @@ var (
func Usage() { func Usage() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0]) 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 [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, "For more information, see:\n")
fmt.Fprintf(os.Stderr, "\thttp://godoc.org/golang.org/x/tools/cmd/stringer\n") fmt.Fprintf(os.Stderr, "\thttp://godoc.org/golang.org/x/tools/cmd/stringer\n")
fmt.Fprintf(os.Stderr, "Flags:\n") fmt.Fprintf(os.Stderr, "Flags:\n")
@ -134,17 +136,17 @@ func main() {
g.Printf("\n") g.Printf("\n")
g.Printf("import (\n") g.Printf("import (\n")
g.Printf("\t\"fmt\"\n") g.Printf("\t\"fmt\"\n")
if !*noJSON {
g.Printf("\t\"encoding/json\"\n")
}
if *sql { if *sql {
g.Printf("\t\"database/sql/driver\"\n") g.Printf("\t\"database/sql/driver\"\n")
} }
if *json {
g.Printf("\t\"encoding/json\"\n")
}
g.Printf(")\n") g.Printf(")\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.