diff --git a/encoding.go b/encoding.go index 8c0dc58..31c0ace 100644 --- a/encoding.go +++ b/encoding.go @@ -8,7 +8,12 @@ import ( headerVal "github.com/bxcodec/goqueue/headers/value" ) +// EncoderFn is a function type that encodes a message into a byte slice. +// It takes a context and a message as input and returns the encoded data and an error (if any). type EncoderFn func(ctx context.Context, m Message) (data []byte, err error) + +// DecoderFn is a function type that decodes a byte slice into a Message. +// It takes a context and a byte slice as input and returns a Message and an error. type DecoderFn func(ctx context.Context, data []byte) (m Message, err error) var ( @@ -28,27 +33,37 @@ var ( ) var ( - GoquEncodingMap = sync.Map{} + // GoQueueEncodingMap is a concurrent-safe map used for encoding in GoQueue. + GoQueueEncodingMap = sync.Map{} ) -func AddGoquEncoding(contentType headerVal.ContentType, encoding *Encoding) { - GoquEncodingMap.Store(contentType, encoding) +// AddGoQueueEncoding stores the given encoding for the specified content type in the GoQueueEncodingMap. +// The GoQueueEncodingMap is a concurrent-safe map that maps content types to encodings. +// The content type is specified by the `contentType` parameter, and the encoding is specified by the `encoding` parameter. +// This function is typically used to register custom encodings for specific content types in the GoQueue library. +func AddGoQueueEncoding(contentType headerVal.ContentType, encoding *Encoding) { + GoQueueEncodingMap.Store(contentType, encoding) } -func GetGoquEncoding(contentType headerVal.ContentType) (res *Encoding, ok bool) { - if encoding, ok := GoquEncodingMap.Load(contentType); ok { +// GetGoQueueEncoding returns the encoding associated with the given content type. +// It looks up the encoding in the GoQueueEncodingMap and returns it along with a boolean value indicating if the encoding was found. +// If the encoding is not found, it returns nil and false. +func GetGoQueueEncoding(contentType headerVal.ContentType) (res *Encoding, ok bool) { + if encoding, ok := GoQueueEncodingMap.Load(contentType); ok { return encoding.(*Encoding), ok } return nil, false } +// Encoding represents an encoding configuration for a specific content type. type Encoding struct { - ContentType headerVal.ContentType - Encode EncoderFn - Decode DecoderFn + ContentType headerVal.ContentType // The content type associated with this encoding. + Encode EncoderFn // The encoding function used to encode data. + Decode DecoderFn // The decoding function used to decode data. } var ( + // JSONEncoding represents the encoding configuration for JSON. JSONEncoding = &Encoding{ ContentType: headerVal.ContentTypeJSON, Encode: JSONEncoder, diff --git a/publisher/rabbitmq/publisher.go b/publisher/rabbitmq/publisher.go index 6077a04..ca9e22e 100644 --- a/publisher/rabbitmq/publisher.go +++ b/publisher/rabbitmq/publisher.go @@ -99,7 +99,7 @@ func (r *rabbitMQ) buildPublisher() goqueue.PublisherFunc { m.ServiceAgent = headerVal.RabbitMQ m.Timestamp = timestamp m.ID = id - encoder, ok := goqueue.GetGoquEncoding(m.ContentType) + encoder, ok := goqueue.GetGoQueueEncoding(m.ContentType) if !ok { encoder = goqueue.DefaultEncoding }