implemented sql flag

This commit is contained in:
marco 2016-02-06 22:46:59 +01:00
parent cd583bfe41
commit ef9a220565
2 changed files with 10 additions and 7 deletions

View File

@ -72,11 +72,9 @@ 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 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.
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
be implemented to seamlessly use the enum in a database model.
## Inspiring projects
* [Stringer](https://godoc.org/golang.org/x/tools/cmd/stringer)

View File

@ -83,6 +83,7 @@ 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.")
output = flag.String("output", "", "output file name; default srcdir/<type>_string.go")
)
@ -135,10 +136,12 @@ func main() {
g.Printf("\n")
g.Printf("import (\n")
g.Printf("\t\"fmt\"\n")
g.Printf("\t\"database/sql/driver\"\n")
if !*noJSON {
g.Printf("\t\"encoding/json\"\n")
}
if *sql {
g.Printf("\t\"database/sql/driver\"\n")
}
g.Printf(")\n")
// Run generate for each type.
@ -324,7 +327,9 @@ func (g *Generator) generate(typeName string, includeJSON bool) {
}
// SQL
if *sql {
g.addValueAndScanMethod(typeName)
}
}
// splitIntoRuns breaks the values into runs of contiguous sequences.