The new logbot, not committed from the wrong terminal window this time.
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.

49 lines
1.0 KiB

6 years ago
  1. package models
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "time"
  6. )
  7. // A Change is a change in the timeline.
  8. type Change struct {
  9. ID string `json:"id"`
  10. Model string `json:"model"`
  11. Op string `json:"op"`
  12. Author string `json:"author"`
  13. Date time.Time `json:"date"`
  14. Objects []ChangeObject `json:"objects"`
  15. }
  16. // A ChangeObject is an object affected by the change.
  17. type ChangeObject struct {
  18. Data json.RawMessage `json:"-"`
  19. TypeName string `json:"__typename"`
  20. }
  21. // Channel parses the ChangeObject as a channel.
  22. func (co *ChangeObject) Channel() (Channel, error) {
  23. if co.TypeName != "Channel" {
  24. return Channel{}, errors.New("Incorrect Type Name")
  25. }
  26. channel := Channel{}
  27. err := json.Unmarshal(co.Data, &channel)
  28. return channel, err
  29. }
  30. // UnmarshalJSON implements json.Unmarshaller
  31. func (co *ChangeObject) UnmarshalJSON(b []byte) error {
  32. data := make([]byte, len(b))
  33. copy(data, b)
  34. co.Data = data
  35. err := json.Unmarshal(data, co)
  36. if err != nil {
  37. return err
  38. }
  39. return nil
  40. }