From ef9a220565dd77453c4166bc208562e12c447519 Mon Sep 17 00:00:00 2001 From: marco Date: Sat, 6 Feb 2016 22:46:59 +0100 Subject: [PATCH] implemented sql flag --- README.md | 8 +++----- stringer.go | 9 +++++++-- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index ef794c0..6782913 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/stringer.go b/stringer.go index f4edc2f..9b142da 100644 --- a/stringer.go +++ b/stringer.go @@ -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/_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 - g.addValueAndScanMethod(typeName) + if *sql { + g.addValueAndScanMethod(typeName) + } } // splitIntoRuns breaks the values into runs of contiguous sequences.