improved the generated code by using maps and using existing String method if available

This commit is contained in:
Francesc Campoy 2015-01-31 11:09:02 +01:00
parent 3da4e90d43
commit a673e22384
3 changed files with 51 additions and 114 deletions

View File

@ -7,34 +7,23 @@ import (
"fmt"
)
func (r ShirtSize) String() string {
switch r {
case NA:
return "NA"
case XS:
return "XS"
case S:
return "S"
case M:
return "M"
case L:
return "L"
case XL:
return "XL"
default:
return "unknown ShirtSize"
}
var _ShirtSizeValueToName = map[ShirtSize]string{
NA: "NA", XS: "XS", S: "S", M: "M", L: "L", XL: "XL",
}
func (r ShirtSize) MarshalJSON() ([]byte, error) {
return json.Marshal(r.String())
if s, ok := r.(fmt.Stringer); ok {
return json.Marshal(s.String())
}
s, ok := _ShirtSizeValueToName[r]
if !ok {
return nil, fmt.Errorf("invalid ShirtSize: %d", r)
}
return json.Marshal(s)
}
var _ShirtSizeNameToValue = map[string]ShirtSize{
"NA": NA, "XS": XS, "S": S, "M": M, "L": L, "XL": XL,
}
func (r *ShirtSize) UnmarshalJSON(data []byte) error {
@ -42,28 +31,10 @@ func (r *ShirtSize) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(data, &s); err != nil {
return fmt.Errorf("ShirtSize should be a string, got %s", data)
}
switch s {
case "NA":
*r = NA
case "XS":
*r = XS
case "S":
*r = S
case "M":
*r = M
case "L":
*r = L
case "XL":
*r = XL
default:
v, ok := _ShirtSizeNameToValue[s]
if !ok {
return fmt.Errorf("invalid ShirtSize %q", s)
}
*r = v
return nil
}

View File

@ -7,37 +7,23 @@ import (
"fmt"
)
func (r WeekDay) String() string {
switch r {
case Monday:
return "Monday"
case Tuesday:
return "Tuesday"
case Wednesday:
return "Wednesday"
case Thursday:
return "Thursday"
case Friday:
return "Friday"
case Saturday:
return "Saturday"
case Sunday:
return "Sunday"
default:
return "unknown WeekDay"
}
var _WeekDayValueToName = map[WeekDay]string{
Monday: "Monday", Tuesday: "Tuesday", Wednesday: "Wednesday", Thursday: "Thursday", Friday: "Friday", Saturday: "Saturday", Sunday: "Sunday",
}
func (r WeekDay) MarshalJSON() ([]byte, error) {
return json.Marshal(r.String())
if s, ok := r.(fmt.Stringer); ok {
return json.Marshal(s.String())
}
s, ok := _WeekDayValueToName[r]
if !ok {
return nil, fmt.Errorf("invalid WeekDay: %d", r)
}
return json.Marshal(s)
}
var _WeekDayNameToValue = map[string]WeekDay{
"Monday": Monday, "Tuesday": Tuesday, "Wednesday": Wednesday, "Thursday": Thursday, "Friday": Friday, "Saturday": Saturday, "Sunday": Sunday,
}
func (r *WeekDay) UnmarshalJSON(data []byte) error {
@ -45,31 +31,10 @@ func (r *WeekDay) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(data, &s); err != nil {
return fmt.Errorf("WeekDay should be a string, got %s", data)
}
switch s {
case "Monday":
*r = Monday
case "Tuesday":
*r = Tuesday
case "Wednesday":
*r = Wednesday
case "Thursday":
*r = Thursday
case "Friday":
*r = Friday
case "Saturday":
*r = Saturday
case "Sunday":
*r = Sunday
default:
v, ok := _WeekDayNameToValue[s]
if !ok {
return fmt.Errorf("invalid WeekDay %q", s)
}
*r = v
return nil
}

View File

@ -19,19 +19,23 @@ import (
)
{{range $typename, $values := .TypesAndValues}}
func (r {{$typename}}) String() string {
switch r {
{{range $values}}
case {{.}}:
return "{{.}}"
{{end}}
default:
return "unknown {{$typename}}"
}
var _{{$typename}}ValueToName = map[{{$typename}}]string {
{{range $values}}{{.}}: "{{.}}",{{end}}
}
func (r {{$typename}}) MarshalJSON() ([]byte, error) {
return json.Marshal(r.String())
if s, ok := r.(fmt.Stringer); ok {
return json.Marshal(s.String())
}
s, ok := _{{$typename}}ValueToName[r]
if !ok {
return nil, fmt.Errorf("invalid {{$typename}}: %d", r)
}
return json.Marshal(s)
}
var _{{$typename}}NameToValue = map[string]{{$typename}} {
{{range $values}}"{{.}}": {{.}},{{end}}
}
func (r *{{$typename}}) UnmarshalJSON(data []byte) error {
@ -39,14 +43,11 @@ func (r *{{$typename}}) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(data, &s); err != nil {
return fmt.Errorf("{{$typename}} should be a string, got %s", data)
}
switch s {
{{range $values}}
case "{{.}}":
*r = {{.}}
{{end}}
default:
v, ok := _{{$typename}}NameToValue[s]
if !ok {
return fmt.Errorf("invalid {{$typename}} %q", s)
}
*r = v
return nil
}