From dfc5db481e751702335ca2e90b079a09778ce266 Mon Sep 17 00:00:00 2001 From: Yannick Briffa Date: Wed, 3 Jan 2018 16:23:39 +0100 Subject: [PATCH] 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}} } )