From dfc5db481e751702335ca2e90b079a09778ce266 Mon Sep 17 00:00:00 2001 From: Yannick Briffa Date: Wed, 3 Jan 2018 16:23:39 +0100 Subject: [PATCH 1/3] adds flag -lower and -upper --- README.md | 3 ++- jsonenums.go | 10 ++++++++++ template.go | 16 ++++++++++++---- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 712ae30..67a2844 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,7 @@ The `-type` flag accepts a comma-separated list of types so a single run can generate methods for multiple types. The default output file is t_jsonenums.go, where t is the lower-cased name of the first type listed. The suffix can be overridden with the `-suffix` flag and a prefix may be added with the `-prefix` -flag. +flag. The `-lower` flag sets the values of the enums in lower case while the `-upper` +sets it in upper case. This is not an official Google product (experimental or otherwise), it is just code that happens to be owned by Google. diff --git a/jsonenums.go b/jsonenums.go index 7d26291..343f3d2 100644 --- a/jsonenums.go +++ b/jsonenums.go @@ -89,6 +89,8 @@ import ( var ( typeNames = flag.String("type", "", "comma-separated list of type names; must be set") + outputLower = flag.Bool("lower", false, "set the json enum values to lower case") + outputUpper = flag.Bool("upper", false, "set the json enum values to upper case") outputPrefix = flag.String("prefix", "", "prefix to be added to the output file") outputSuffix = flag.String("suffix", "_jsonenums", "suffix to be added to the output file") ) @@ -100,6 +102,10 @@ func main() { } types := strings.Split(*typeNames, ",") + if *outputLower && *outputUpper { + log.Fatal("cannot -upper and -lower in the same time") + } + // Only one directory at a time can be processed, and the default is ".". dir := "." if args := flag.Args(); len(args) == 1 { @@ -122,10 +128,14 @@ func main() { Command string PackageName string TypesAndValues map[string][]string + Lower bool + Upper bool }{ Command: strings.Join(os.Args[1:], " "), PackageName: pkg.Name, TypesAndValues: make(map[string][]string), + Lower: *outputLower, + Upper: *outputUpper, } // Run generate for each type. diff --git a/template.go b/template.go index 332c2a3..ca1ac10 100644 --- a/template.go +++ b/template.go @@ -15,9 +15,15 @@ package main -import "text/template" +import ( + "strings" + "text/template" +) -var generatedTmpl = template.Must(template.New("generated").Parse(` +var generatedTmpl = template.Must(template.New("generated").Funcs(template.FuncMap{ + "toLower": strings.ToLower, + "toUpper": strings.ToUpper, +}).Parse(` // generated by jsonenums {{.Command}}; DO NOT EDIT package {{.PackageName}} @@ -27,16 +33,18 @@ import ( "fmt" ) +{{ $upper := .Upper}} +{{ $lower := .Lower}} {{range $typename, $values := .TypesAndValues}} var ( _{{$typename}}NameToValue = map[string]{{$typename}} { - {{range $values}}"{{.}}": {{.}}, + {{range $values}}"{{ if $lower }}{{ toLower . }}{{ else if $upper }}{{ toUpper .}}{{ else }}{{.}}{{end}}": {{.}}, {{end}} } _{{$typename}}ValueToName = map[{{$typename}}]string { - {{range $values}}{{.}}: "{{.}}", + {{range $values}}{{.}}: "{{ if $lower }}{{ toLower . }}{{ else if $upper }}{{ toUpper .}}{{ else }}{{.}}{{end}}", {{end}} } ) From c64b41d7d9a28e16a6fed5cfbc0471cf9844754a Mon Sep 17 00:00:00 2001 From: Yannick Briffa Date: Thu, 11 Jan 2018 17:46:53 +0100 Subject: [PATCH 2/3] feature: adds no-stringer argument --- jsonenums.go | 13 ++++++++----- template.go | 3 +++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/jsonenums.go b/jsonenums.go index 343f3d2..480f350 100644 --- a/jsonenums.go +++ b/jsonenums.go @@ -88,11 +88,12 @@ import ( ) var ( - typeNames = flag.String("type", "", "comma-separated list of type names; must be set") - outputLower = flag.Bool("lower", false, "set the json enum values to lower case") - outputUpper = flag.Bool("upper", false, "set the json enum values to upper case") - outputPrefix = flag.String("prefix", "", "prefix to be added to the output file") - outputSuffix = flag.String("suffix", "_jsonenums", "suffix to be added to the output file") + typeNames = flag.String("type", "", "comma-separated list of type names; must be set") + outputLower = flag.Bool("lower", false, "set the json enum values to lower case") + outputUpper = flag.Bool("upper", false, "set the json enum values to upper case") + outputNoStringer = flag.Bool("no-stringer", false, "disable the usage of stringer if exists") + outputPrefix = flag.String("prefix", "", "prefix to be added to the output file") + outputSuffix = flag.String("suffix", "_jsonenums", "suffix to be added to the output file") ) func main() { @@ -130,12 +131,14 @@ func main() { TypesAndValues map[string][]string Lower bool Upper bool + NoStringer bool }{ Command: strings.Join(os.Args[1:], " "), PackageName: pkg.Name, TypesAndValues: make(map[string][]string), Lower: *outputLower, Upper: *outputUpper, + NoStringer: *outputNoStringer, } // Run generate for each type. diff --git a/template.go b/template.go index ca1ac10..764dd48 100644 --- a/template.go +++ b/template.go @@ -35,6 +35,7 @@ import ( {{ $upper := .Upper}} {{ $lower := .Lower}} +{{ $noStringer := .NoStringer }} {{range $typename, $values := .TypesAndValues}} var ( @@ -49,6 +50,7 @@ var ( } ) +{{ if not $noStringer }} func init() { var v {{$typename}} if _, ok := interface{}(v).(fmt.Stringer); ok { @@ -58,6 +60,7 @@ func init() { } } } +{{ end }} // MarshalJSON is generated so {{$typename}} satisfies json.Marshaler. func (r {{$typename}}) MarshalJSON() ([]byte, error) { From 41fbf3154efa22a9db85c91d71f1e10d3c7e4d09 Mon Sep 17 00:00:00 2001 From: Yannick Briffa Date: Wed, 7 Mar 2018 15:16:34 +0100 Subject: [PATCH 3/3] remove stringer in MarshalJSON --- template.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/template.go b/template.go index 764dd48..7049a8a 100644 --- a/template.go +++ b/template.go @@ -64,10 +64,11 @@ func init() { // MarshalJSON is generated so {{$typename}} satisfies json.Marshaler. func (r {{$typename}}) MarshalJSON() ([]byte, error) { + {{ if not $noStringer -}} if s, ok := interface{}(r).(fmt.Stringer); ok { return json.Marshal(s.String()) } - s, ok := _{{$typename}}ValueToName[r] + {{ end }}s, ok := _{{$typename}}ValueToName[r] if !ok { return nil, fmt.Errorf("invalid {{$typename}}: %d", r) }