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.

57 lines
1.6 KiB

  1. package models
  2. import (
  3. "fmt"
  4. "io"
  5. )
  6. // ChapterCommentMode represents the kind of tags.
  7. type ChapterCommentMode string
  8. const (
  9. // ChapterCommentModeDisabled is a chapter comment mode, see GraphQL documentation.
  10. ChapterCommentModeDisabled ChapterCommentMode = "Disabled"
  11. // ChapterCommentModeArticle is a chapter comment mode, see GraphQL documentation.
  12. ChapterCommentModeArticle ChapterCommentMode = "Article"
  13. // ChapterCommentModeChat is a chapter comment mode, see GraphQL documentation.
  14. ChapterCommentModeChat ChapterCommentMode = "Chat"
  15. // ChapterCommentModeMessage is a chapter comment mode, see GraphQL documentation.
  16. ChapterCommentModeMessage ChapterCommentMode = "Message"
  17. )
  18. // UnmarshalGQL unmarshals
  19. func (e *ChapterCommentMode) UnmarshalGQL(v interface{}) error {
  20. str, ok := v.(string)
  21. if !ok {
  22. return fmt.Errorf("enums must be strings")
  23. }
  24. *e = ChapterCommentMode(str)
  25. switch *e {
  26. case ChapterCommentModeDisabled, ChapterCommentModeArticle, ChapterCommentModeChat, ChapterCommentModeMessage:
  27. return nil
  28. default:
  29. return fmt.Errorf("%s is not a valid ChapterCommentMode", str)
  30. }
  31. }
  32. // IsEnabled returns true if comments are enabled.
  33. func (e ChapterCommentMode) IsEnabled() bool {
  34. return e != ChapterCommentModeDisabled && len(e) != 0
  35. }
  36. // MarshalGQL turns it into a JSON string
  37. func (e ChapterCommentMode) MarshalGQL(w io.Writer) {
  38. // Backwards compatibility: Empty value means disabled.
  39. if len(e) == 0 {
  40. w.Write(disabledChapterCommentModeBytes)
  41. return
  42. }
  43. fmt.Fprint(w, "\""+string(e)+"\"")
  44. }
  45. var disabledChapterCommentModeBytes = []byte(`"Disabled"`)