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" "fmt"
) )
func (r ShirtSize) String() string { var _ShirtSizeValueToName = map[ShirtSize]string{
switch r { NA: "NA", XS: "XS", S: "S", M: "M", L: "L", XL: "XL",
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"
}
} }
func (r ShirtSize) MarshalJSON() ([]byte, error) { 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 { 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 { if err := json.Unmarshal(data, &s); err != nil {
return fmt.Errorf("ShirtSize should be a string, got %s", data) return fmt.Errorf("ShirtSize should be a string, got %s", data)
} }
switch s { v, ok := _ShirtSizeNameToValue[s]
if !ok {
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:
return fmt.Errorf("invalid ShirtSize %q", s) return fmt.Errorf("invalid ShirtSize %q", s)
} }
*r = v
return nil return nil
} }

View File

@ -7,37 +7,23 @@ import (
"fmt" "fmt"
) )
func (r WeekDay) String() string { var _WeekDayValueToName = map[WeekDay]string{
switch r { Monday: "Monday", Tuesday: "Tuesday", Wednesday: "Wednesday", Thursday: "Thursday", Friday: "Friday", Saturday: "Saturday", Sunday: "Sunday",
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"
}
} }
func (r WeekDay) MarshalJSON() ([]byte, error) { 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 { 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 { if err := json.Unmarshal(data, &s); err != nil {
return fmt.Errorf("WeekDay should be a string, got %s", data) return fmt.Errorf("WeekDay should be a string, got %s", data)
} }
switch s { v, ok := _WeekDayNameToValue[s]
if !ok {
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:
return fmt.Errorf("invalid WeekDay %q", s) return fmt.Errorf("invalid WeekDay %q", s)
} }
*r = v
return nil return nil
} }

View File

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