This commit is contained in:
Alex Hokanson 2017-05-05 00:30:19 +00:00 committed by GitHub
commit ccf407f956
2 changed files with 39 additions and 9 deletions

View File

@ -11,6 +11,8 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
// 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
@ -87,10 +89,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() {
@ -113,6 +121,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)
@ -121,11 +135,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.
@ -134,7 +148,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

@ -31,12 +31,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}}
} }
) )
@ -45,7 +45,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}}
} }
} }