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