jsonenums/example/weekday_jsonenums.go

70 lines
1.6 KiB
Go
Raw Normal View History

2015-02-04 14:40:08 +01:00
// generated by jsonenums -type=WeekDay; DO NOT EDIT
2015-01-29 11:35:15 +01:00
2015-01-29 17:00:50 +01:00
package main
2015-01-29 11:35:15 +01:00
import (
"encoding/json"
"fmt"
)
2015-02-04 14:40:08 +01:00
var (
_WeekDayNameToValue = map[string]WeekDay{
"Monday": Monday,
"Tuesday": Tuesday,
"Wednesday": Wednesday,
"Thursday": Thursday,
"Friday": Friday,
"Saturday": Saturday,
"Sunday": Sunday,
2015-01-29 11:35:15 +01:00
}
2015-02-04 14:40:08 +01:00
_WeekDayValueToName = map[WeekDay]string{
Monday: "Monday",
Tuesday: "Tuesday",
Wednesday: "Wednesday",
Thursday: "Thursday",
Friday: "Friday",
Saturday: "Saturday",
Sunday: "Sunday",
}
)
2015-01-29 11:35:15 +01:00
2015-01-31 12:21:55 +01:00
func init() {
var v WeekDay
if _, ok := interface{}(v).(fmt.Stringer); ok {
_WeekDayNameToValue = map[string]WeekDay{
2015-02-04 14:40:08 +01:00
interface{}(Monday).(fmt.Stringer).String(): Monday,
interface{}(Tuesday).(fmt.Stringer).String(): Tuesday,
interface{}(Wednesday).(fmt.Stringer).String(): Wednesday,
interface{}(Thursday).(fmt.Stringer).String(): Thursday,
interface{}(Friday).(fmt.Stringer).String(): Friday,
interface{}(Saturday).(fmt.Stringer).String(): Saturday,
interface{}(Sunday).(fmt.Stringer).String(): Sunday,
2015-01-31 12:21:55 +01:00
}
}
}
2015-02-04 14:40:08 +01:00
func (r WeekDay) MarshalJSON() ([]byte, error) {
if s, ok := interface{}(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)
}
2015-01-29 11:35:15 +01:00
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)
}
v, ok := _WeekDayNameToValue[s]
if !ok {
2015-01-29 17:00:50 +01:00
return fmt.Errorf("invalid WeekDay %q", s)
2015-01-29 11:35:15 +01:00
}
*r = v
2015-01-29 11:35:15 +01:00
return nil
}