adds flag -lower and -upper
This commit is contained in:
parent
7058b320c2
commit
dfc5db481e
@ -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,
|
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
|
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`
|
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.
|
This is not an official Google product (experimental or otherwise), it is just code that happens to be owned by Google.
|
||||||
|
10
jsonenums.go
10
jsonenums.go
@ -89,6 +89,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")
|
||||||
|
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")
|
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")
|
outputSuffix = flag.String("suffix", "_jsonenums", "suffix to be added to the output file")
|
||||||
)
|
)
|
||||||
@ -100,6 +102,10 @@ func main() {
|
|||||||
}
|
}
|
||||||
types := strings.Split(*typeNames, ",")
|
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 ".".
|
// Only one directory at a time can be processed, and the default is ".".
|
||||||
dir := "."
|
dir := "."
|
||||||
if args := flag.Args(); len(args) == 1 {
|
if args := flag.Args(); len(args) == 1 {
|
||||||
@ -122,10 +128,14 @@ func main() {
|
|||||||
Command string
|
Command string
|
||||||
PackageName string
|
PackageName string
|
||||||
TypesAndValues map[string][]string
|
TypesAndValues map[string][]string
|
||||||
|
Lower bool
|
||||||
|
Upper bool
|
||||||
}{
|
}{
|
||||||
Command: strings.Join(os.Args[1:], " "),
|
Command: strings.Join(os.Args[1:], " "),
|
||||||
PackageName: pkg.Name,
|
PackageName: pkg.Name,
|
||||||
TypesAndValues: make(map[string][]string),
|
TypesAndValues: make(map[string][]string),
|
||||||
|
Lower: *outputLower,
|
||||||
|
Upper: *outputUpper,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run generate for each type.
|
// Run generate for each type.
|
||||||
|
16
template.go
16
template.go
@ -15,9 +15,15 @@
|
|||||||
|
|
||||||
package main
|
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
|
// generated by jsonenums {{.Command}}; DO NOT EDIT
|
||||||
|
|
||||||
package {{.PackageName}}
|
package {{.PackageName}}
|
||||||
@ -27,16 +33,18 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
{{ $upper := .Upper}}
|
||||||
|
{{ $lower := .Lower}}
|
||||||
{{range $typename, $values := .TypesAndValues}}
|
{{range $typename, $values := .TypesAndValues}}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
_{{$typename}}NameToValue = map[string]{{$typename}} {
|
_{{$typename}}NameToValue = map[string]{{$typename}} {
|
||||||
{{range $values}}"{{.}}": {{.}},
|
{{range $values}}"{{ if $lower }}{{ toLower . }}{{ else if $upper }}{{ toUpper .}}{{ else }}{{.}}{{end}}": {{.}},
|
||||||
{{end}}
|
{{end}}
|
||||||
}
|
}
|
||||||
|
|
||||||
_{{$typename}}ValueToName = map[{{$typename}}]string {
|
_{{$typename}}ValueToName = map[{{$typename}}]string {
|
||||||
{{range $values}}{{.}}: "{{.}}",
|
{{range $values}}{{.}}: "{{ if $lower }}{{ toLower . }}{{ else if $upper }}{{ toUpper .}}{{ else }}{{.}}{{end}}",
|
||||||
{{end}}
|
{{end}}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user