jsonenums/example/weekday_jsonenums.go

76 lines
1.0 KiB
Go
Raw Normal View History

2015-01-29 11:35:15 +01:00
// generated by jsonenums -type=WeekDay; DO NOT EDIT
package testdata
import (
"encoding/json"
"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"
}
}
func (r WeekDay) MarshalJSON() ([]byte, error) {
return json.Marshal(r.String())
}
func (r *WeekDay) UnmarshalJSON(data []byte) error {
var s string
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:
return nil, fmt.Errorf("invalid WeekDay %q", s)
}
return nil
}