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.

69 lines
1.7 KiB

  1. package models
  2. import (
  3. "fmt"
  4. "io"
  5. )
  6. // A Tag associates a story with other content, like other stories, logs and more.
  7. type Tag struct {
  8. Kind TagKind `bson:"kind"`
  9. Name string `bson:"name"`
  10. }
  11. // Equal returns true if the tags match one another.
  12. func (tag *Tag) Equal(other Tag) bool {
  13. return tag.Kind == other.Kind && tag.Name == other.Name
  14. }
  15. // IsChangeObject is an interface implementation to identify it as a valid
  16. // ChangeObject in GQL.
  17. func (*Tag) IsChangeObject() {
  18. panic("this method is a dummy, and so is its caller")
  19. }
  20. // TagKind represents the kind of tags.
  21. type TagKind string
  22. const (
  23. // TagKindOrganization is a tag kind, see GraphQL documentation.
  24. TagKindOrganization TagKind = "Organization"
  25. // TagKindCharacter is a tag kind, see GraphQL documentation.
  26. TagKindCharacter TagKind = "Character"
  27. // TagKindLocation is a tag kind, see GraphQL documentation.
  28. TagKindLocation TagKind = "Location"
  29. // TagKindEvent is a tag kind, see GraphQL documentation.
  30. TagKindEvent TagKind = "Event"
  31. // TagKindSeries is a tag kind, see GraphQL documentation.
  32. TagKindSeries TagKind = "Series"
  33. )
  34. // UnmarshalGQL unmarshals
  35. func (e *TagKind) UnmarshalGQL(v interface{}) error {
  36. str, ok := v.(string)
  37. if !ok {
  38. return fmt.Errorf("enums must be strings")
  39. }
  40. *e = TagKind(str)
  41. switch *e {
  42. case TagKindOrganization, TagKindCharacter, TagKindLocation, TagKindEvent, TagKindSeries:
  43. return nil
  44. default:
  45. return fmt.Errorf("%s is not a valid TagKind", str)
  46. }
  47. }
  48. // MarshalGQL turns it into a JSON string
  49. func (e TagKind) MarshalGQL(w io.Writer) {
  50. fmt.Fprint(w, "\""+string(e), "\"")
  51. }
  52. // TagFilter is a filter for tag listing.
  53. type TagFilter struct {
  54. Kind *TagKind `bson:"kind,omitempty"`
  55. }