events.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 SignUpStarted struct {
  59. Timestamp time.Time `json:"timestamp"`
  60. Email string `json:"email"`
  61. Code string `json:"code"`
  62. }
  63. type SignUpCompleted struct {
  64. Timestamp time.Time `json:"timestamp"`
  65. Name string `json:"name"`
  66. Email string `json:"email"`
  67. }
  68. type UserUpdated struct {
  69. Timestamp time.Time `json:"timestamp"`
  70. Id int64 `json:"id"`
  71. Name string `json:"name"`
  72. Login string `json:"login"`
  73. Email string `json:"email"`
  74. }