Address the issue when the enum value is passed as null

This commit is contained in:
Armin Pašalić 2018-01-18 23:53:24 +01:00
parent 7058b320c2
commit 2d3ec00166
3 changed files with 13 additions and 0 deletions

View File

@ -41,6 +41,7 @@ func init() {
}
}
// MarshalJSON is generated so ShirtSize satisfies json.Marshaler.
func (r ShirtSize) MarshalJSON() ([]byte, error) {
if s, ok := interface{}(r).(fmt.Stringer); ok {
return json.Marshal(s.String())
@ -52,7 +53,11 @@ func (r ShirtSize) MarshalJSON() ([]byte, error) {
return json.Marshal(s)
}
// UnmarshalJSON is generated so ShirtSize satisfies json.Unmarshaler.
func (r *ShirtSize) UnmarshalJSON(data []byte) error {
if string(data) == "null" {
return nil
}
var s string
if err := json.Unmarshal(data, &s); err != nil {
return fmt.Errorf("ShirtSize should be a string, got %s", data)

View File

@ -44,6 +44,7 @@ func init() {
}
}
// MarshalJSON is generated so WeekDay satisfies json.Marshaler.
func (r WeekDay) MarshalJSON() ([]byte, error) {
if s, ok := interface{}(r).(fmt.Stringer); ok {
return json.Marshal(s.String())
@ -55,7 +56,11 @@ func (r WeekDay) MarshalJSON() ([]byte, error) {
return json.Marshal(s)
}
// UnmarshalJSON is generated so WeekDay satisfies json.Unmarshaler.
func (r *WeekDay) UnmarshalJSON(data []byte) error {
if string(data) == "null" {
return nil
}
var s string
if err := json.Unmarshal(data, &s); err != nil {
return fmt.Errorf("WeekDay should be a string, got %s", data)

View File

@ -65,6 +65,9 @@ func (r {{$typename}}) MarshalJSON() ([]byte, error) {
// UnmarshalJSON is generated so {{$typename}} satisfies json.Unmarshaler.
func (r *{{$typename}}) UnmarshalJSON(data []byte) error {
if string(data) == "null" {
return nil
}
var s string
if err := json.Unmarshal(data, &s); err != nil {
return fmt.Errorf("{{$typename}} should be a string, got %s", data)