GraphQL API and utilities for the rpdata project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.4 KiB

  1. package models
  2. import (
  3. "fmt"
  4. "io"
  5. )
  6. // StoryCategory represents the category of a story.
  7. type StoryCategory string
  8. const (
  9. // StoryCategoryInfo is a story category, see GraphQL documentation.
  10. StoryCategoryInfo StoryCategory = "Info"
  11. // StoryCategoryNews is a story category, see GraphQL documentation.
  12. StoryCategoryNews StoryCategory = "News"
  13. // StoryCategoryDocument is a story category, see GraphQL documentation.
  14. StoryCategoryDocument StoryCategory = "Document"
  15. // StoryCategoryBackground is a story category, see GraphQL documentation.
  16. StoryCategoryBackground StoryCategory = "Background"
  17. // StoryCategoryStory is a story category, see GraphQL documentation.
  18. StoryCategoryStory StoryCategory = "Story"
  19. )
  20. // IsValid returns whether the category is one of the valid values.
  21. func (e *StoryCategory) IsValid() bool {
  22. switch *e {
  23. case StoryCategoryInfo, StoryCategoryNews, StoryCategoryDocument, StoryCategoryBackground, StoryCategoryStory:
  24. return true
  25. default:
  26. return false
  27. }
  28. }
  29. // UnmarshalGQL unmarshals
  30. func (e *StoryCategory) UnmarshalGQL(v interface{}) error {
  31. str, ok := v.(string)
  32. if !ok {
  33. return fmt.Errorf("enums must be strings")
  34. }
  35. if !e.IsValid() {
  36. return fmt.Errorf("%s is not a valid StoryCategory", str)
  37. }
  38. *e = StoryCategory(str)
  39. return nil
  40. }
  41. // MarshalGQL turns it into a JSON string
  42. func (e StoryCategory) MarshalGQL(w io.Writer) {
  43. fmt.Fprint(w, "\""+string(e)+"\"")
  44. }