models2: Added IsValid() method to StoryCategory

This commit is contained in:
2018-09-29 16:38:21 +02:00
parent 9bcc28a0ea
commit 707dcb43eb
+15 -5
View File
@@ -21,6 +21,16 @@ const (
StoryCategoryStory StoryCategory = "Story"
)
// IsValid returns whether the category is one of the valid values.
func (e *StoryCategory) IsValid() bool {
switch *e {
case StoryCategoryInfo, StoryCategoryNews, StoryCategoryDocument, StoryCategoryBackground, StoryCategoryStory:
return true
default:
return false
}
}
// UnmarshalGQL unmarshals
func (e *StoryCategory) UnmarshalGQL(v interface{}) error {
str, ok := v.(string)
@@ -28,13 +38,13 @@ func (e *StoryCategory) UnmarshalGQL(v interface{}) error {
return fmt.Errorf("enums must be strings")
}
*e = StoryCategory(str)
switch *e {
case StoryCategoryInfo, StoryCategoryNews, StoryCategoryDocument, StoryCategoryBackground, StoryCategoryStory:
return nil
default:
if !e.IsValid() {
return fmt.Errorf("%s is not a valid StoryCategory", str)
}
*e = StoryCategory(str)
return nil
}
// MarshalGQL turns it into a JSON string