github.com/wfusion/gofusion@v1.1.14/common/infra/watermill/components/forwarder/envelope.go (about) 1 package forwarder 2 3 import ( 4 "github.com/pkg/errors" 5 6 "github.com/wfusion/gofusion/common/infra/watermill/message" 7 "github.com/wfusion/gofusion/common/utils" 8 "github.com/wfusion/gofusion/common/utils/serialize/json" 9 ) 10 11 // messageEnvelope wraps Watermill message and contains destination topic. 12 type messageEnvelope struct { 13 DestinationTopic string `json:"destination_topic"` 14 15 UUID string `json:"uuid"` 16 Payload []byte `json:"payload"` 17 Metadata map[string]string `json:"metadata"` 18 } 19 20 func newMessageEnvelope(destTopic string, msg *message.Message) (*messageEnvelope, error) { 21 e := &messageEnvelope{ 22 DestinationTopic: destTopic, 23 UUID: msg.UUID, 24 Payload: msg.Payload, 25 Metadata: msg.Metadata, 26 } 27 28 if err := e.validate(); err != nil { 29 return nil, errors.Wrap(err, "cannot create a message envelope") 30 } 31 32 return e, nil 33 } 34 35 func (e *messageEnvelope) validate() error { 36 if e.DestinationTopic == "" { 37 return errors.New("unknown destination topic") 38 } 39 40 return nil 41 } 42 43 func wrapMessageInEnvelope(destinationTopic string, msg *message.Message) (*message.Message, error) { 44 envelope, err := newMessageEnvelope(destinationTopic, msg) 45 if err != nil { 46 return nil, errors.Wrap(err, "cannot envelope a message") 47 } 48 49 envelopedMessage, err := json.Marshal(envelope) 50 if err != nil { 51 return nil, errors.Wrap(err, "cannot marshal a message") 52 } 53 54 wrappedMsg := message.NewMessage(utils.UUID(), envelopedMessage) 55 wrappedMsg.SetContext(msg.Context()) 56 57 return wrappedMsg, nil 58 } 59 60 func unwrapMessageFromEnvelope(msg *message.Message) ( 61 destinationTopic string, unwrappedMsg *message.Message, err error) { 62 envelopedMsg := messageEnvelope{} 63 if err := json.Unmarshal(msg.Payload, &envelopedMsg); err != nil { 64 return "", nil, errors.Wrap(err, "cannot unmarshal message wrapped in an envelope") 65 } 66 67 if err := envelopedMsg.validate(); err != nil { 68 return "", nil, errors.Wrap(err, "an unmarshalled message envelope is invalid") 69 } 70 71 watermillMessage := message.NewMessage(envelopedMsg.UUID, envelopedMsg.Payload) 72 watermillMessage.Metadata = envelopedMsg.Metadata 73 watermillMessage.SetContext(msg.Context()) 74 75 return envelopedMsg.DestinationTopic, watermillMessage, nil 76 }