events.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package events
  2. import (
  3. "reflect"
  4. "time"
  5. )
  6. // Events can be passed to external systems via for example AMQP
  7. // Treat these events as basically DTOs so changes has to be backward compatible
  8. type Priority string
  9. const (
  10. PRIO_DEBUG Priority = "DEBUG"
  11. PRIO_INFO Priority = "INFO"
  12. PRIO_ERROR Priority = "ERROR"
  13. )
  14. type Event struct {
  15. Timestamp time.Time `json:"timestamp"`
  16. }
  17. type OnTheWireEvent struct {
  18. EventType string `json:"event_type"`
  19. Priority Priority `json:"priority"`
  20. Timestamp time.Time `json:"timestamp"`
  21. Payload interface{} `json:"payload"`
  22. }
  23. type EventBase interface {
  24. ToOnWriteEvent() *OnTheWireEvent
  25. }
  26. func ToOnWriteEvent(event interface{}) (*OnTheWireEvent, error) {
  27. eventType := reflect.TypeOf(event).Elem()
  28. wireEvent := OnTheWireEvent{
  29. Priority: PRIO_INFO,
  30. EventType: eventType.Name(),
  31. Payload: event,
  32. }
  33. baseField := reflect.Indirect(reflect.ValueOf(event)).FieldByName("Timestamp")
  34. if baseField.IsValid() {
  35. wireEvent.Timestamp = baseField.Interface().(time.Time)
  36. } else {
  37. wireEvent.Timestamp = time.Now()
  38. }
  39. return &wireEvent, nil
  40. }
  41. type OrgCreated struct {
  42. Timestamp time.Time `json:"timestamp"`
  43. Id int64 `json:"id"`
  44. Name string `json:"name"`
  45. }
  46. type OrgUpdated struct {
  47. Timestamp time.Time `json:"timestamp"`
  48. Id int64 `json:"id"`
  49. Name string `json:"name"`
  50. }
  51. type UserCreated struct {
  52. Timestamp time.Time `json:"timestamp"`
  53. Id int64 `json:"id"`
  54. Name string `json:"name"`
  55. Login string `json:"login"`
  56. Email string `json:"email"`
  57. }
  58. type UserSignedUp struct {
  59. Timestamp time.Time `json:"timestamp"`
  60. Id int64 `json:"id"`
  61. Name string `json:"name"`
  62. Login string `json:"login"`
  63. Email string `json:"email"`
  64. }
  65. type UserUpdated struct {
  66. Timestamp time.Time `json:"timestamp"`
  67. Id int64 `json:"id"`
  68. Name string `json:"name"`
  69. Login string `json:"login"`
  70. Email string `json:"email"`
  71. }