Address the issue when the enum value is passed as null
This commit is contained in:
parent
7058b320c2
commit
2d3ec00166
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user