message.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright © 2021 Ettore Di Giacinto <[email protected]>
  2. //
  3. // This program is free software; you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation; either version 2 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program; if not, see <http://www.gnu.org/licenses/>.
  15. package hub
  16. // Message gets converted to/from JSON and sent in the body of pubsub messages.
  17. type Message struct {
  18. Message string
  19. SenderID string
  20. Annotations map[string]string
  21. }
  22. type MessageOption func(cfg *Message) error
  23. // Apply applies the given options to the config, returning the first error
  24. // encountered (if any).
  25. func (m *Message) Apply(opts ...MessageOption) error {
  26. for _, opt := range opts {
  27. if opt == nil {
  28. continue
  29. }
  30. if err := opt(m); err != nil {
  31. return err
  32. }
  33. }
  34. return nil
  35. }
  36. func NewMessage(s string) *Message {
  37. return &Message{Message: s}
  38. }
  39. func (m *Message) Copy() *Message {
  40. copy := *m
  41. return &copy
  42. }
  43. func (m *Message) WithMessage(s string) *Message {
  44. copy := m.Copy()
  45. copy.Message = s
  46. return copy
  47. }