github.com/wfusion/gofusion@v1.1.14/common/infra/watermill/components/cqrs/marshaler.go (about)

     1  package cqrs
     2  
     3  import (
     4  	"github.com/wfusion/gofusion/common/infra/watermill/message"
     5  )
     6  
     7  // CommandEventMarshaler marshals Commands and Events to Watermill's messages and vice versa.
     8  // Payload of the command needs to be marshaled to []bytes.
     9  type CommandEventMarshaler interface {
    10  	// Marshal marshals Command or Event to Watermill's message.
    11  	Marshal(v any) (*message.Message, error)
    12  
    13  	// Unmarshal unmarshals watermill's message to v Command or Event.
    14  	Unmarshal(msg *message.Message, v any) (err error)
    15  
    16  	// Name returns the name of Command or Event.
    17  	// Name is used to determine, that received command or event is event which we want to handle.
    18  	Name(v any) string
    19  
    20  	// NameFromMessage returns the name of Command or Event from Watermill's message (generated by Marshal).
    21  	//
    22  	// When we have Command or Event marshaled to Watermill's message,
    23  	// we should use NameFromMessage instead of Name to avoid unnecessary unmarshaling.
    24  	NameFromMessage(msg *message.Message) string
    25  }