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
// limitations under the License.
// modified by Odyssey
// JSONenums is a tool to automate the creation of methods that satisfy the
// fmt.Stringer, json.Marshaler and json.Unmarshaler interfaces.
// Given the name of a (signed or unsigned) integer type T that has constants
@ -87,10 +89,16 @@ import (
"github.com/campoy/jsonenums/parser"
)
type customMapping struct {
Raw string
Custom string
}
var (
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")
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")
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")
customValueMappings = flag.String("custom", "", "semicolon list of custom mappings for type values")
)
func main() {
@ -113,6 +121,12 @@ func main() {
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)
if err != nil {
log.Fatalf("parsing package: %v", err)
@ -121,11 +135,11 @@ func main() {
var analysis = struct {
Command string
PackageName string
TypesAndValues map[string][]string
TypesAndValues map[string][]customMapping
}{
Command: strings.Join(os.Args[1:], " "),
PackageName: pkg.Name,
TypesAndValues: make(map[string][]string),
TypesAndValues: make(map[string][]customMapping),
}
// Run generate for each type.
@ -134,7 +148,23 @@ func main() {
if err != nil {
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
if err := generatedTmpl.Execute(&buf, analysis); err != nil {

View File

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