From dfdf7c01c312d12d96a62d678971498de3e62af5 Mon Sep 17 00:00:00 2001 From: Iman Tumorang Date: Sun, 9 Jun 2024 21:30:12 +0300 Subject: [PATCH] fix: encoding typo (#12) --- encoding.go | 31 +++++++++++++++++++++++-------- publisher/rabbitmq/publisher.go | 2 +- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/encoding.go b/encoding.go index 8c0dc58..e4be546 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 }