add the ability to have custom mappings of type values

This commit is contained in:
Alex Hokanson 2016-09-12 12:40:53 -04:00
parent ff3de3c0dd
commit 4e2c88698e
2 changed files with 39 additions and 9 deletions

View File

@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// modified by Odyssey
// JSONenums is a tool to automate the creation of methods that satisfy the // JSONenums is a tool to automate the creation of methods that satisfy the
// fmt.Stringer, json.Marshaler and json.Unmarshaler interfaces. // fmt.Stringer, json.Marshaler and json.Unmarshaler interfaces.
// Given the name of a (signed or unsigned) integer type T that has constants // Given the name of a (signed or unsigned) integer type T that has constants
@ -78,10 +80,16 @@ import (
"github.com/campoy/jsonenums/parser" "github.com/campoy/jsonenums/parser"
) )
type customMapping struct {
Raw string
Custom string
}
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")
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")
customValueMappings = flag.String("custom", "", "semicolon list of custom mappings for type values")
) )
func main() { func main() {
@ -104,6 +112,12 @@ func main() {
dir, err) dir, err)
} }
customValueNames := make(map[string]string)
for _, mapping := range strings.Split(*customValueMappings, ";") {
splitValues := strings.Split(mapping, ",")
customValueNames[splitValues[0]] = splitValues[1]
}
pkg, err := parser.ParsePackage(dir) pkg, err := parser.ParsePackage(dir)
if err != nil { if err != nil {
log.Fatalf("parsing package: %v", err) log.Fatalf("parsing package: %v", err)
@ -112,11 +126,11 @@ func main() {
var analysis = struct { var analysis = struct {
Command string Command string
PackageName string PackageName string
TypesAndValues map[string][]string TypesAndValues map[string][]customMapping
}{ }{
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][]customMapping),
} }
// Run generate for each type. // Run generate for each type.
@ -125,7 +139,23 @@ func main() {
if err != nil { if err != nil {
log.Fatalf("finding values for type %v: %v", typeName, err) log.Fatalf("finding values for type %v: %v", typeName, err)
} }
analysis.TypesAndValues[typeName] = values
var newValues []customMapping
for _, oldValue := range values {
val, ok := customValueNames[oldValue]
if ok {
newValues = append(newValues, customMapping{
Raw: oldValue,
Custom: val,
})
} else {
newValues = append(newValues, customMapping{
Raw: oldValue,
Custom: oldValue,
})
}
}
analysis.TypesAndValues[typeName] = newValues
var buf bytes.Buffer var buf bytes.Buffer
if err := generatedTmpl.Execute(&buf, analysis); err != nil { if err := generatedTmpl.Execute(&buf, analysis); err != nil {

View File

@ -22,12 +22,12 @@ import (
var ( var (
_{{$typename}}NameToValue = map[string]{{$typename}} { _{{$typename}}NameToValue = map[string]{{$typename}} {
{{range $values}}"{{.}}": {{.}}, {{range $values}}"{{.Custom}}": {{.Raw}},
{{end}} {{end}}
} }
_{{$typename}}ValueToName = map[{{$typename}}]string { _{{$typename}}ValueToName = map[{{$typename}}]string {
{{range $values}}{{.}}: "{{.}}", {{range $values}}{{.Raw}}: "{{.Custom}}",
{{end}} {{end}}
} }
) )
@ -36,7 +36,7 @@ func init() {
var v {{$typename}} var v {{$typename}}
if _, ok := interface{}(v).(fmt.Stringer); ok { if _, ok := interface{}(v).(fmt.Stringer); ok {
_{{$typename}}NameToValue = map[string]{{$typename}} { _{{$typename}}NameToValue = map[string]{{$typename}} {
{{range $values}}interface{}({{.}}).(fmt.Stringer).String(): {{.}}, {{range $values}}interface{}({{.Raw}}).(fmt.Stringer).String(): {{.Raw}},
{{end}} {{end}}
} }
} }