Skip to content

Commit

Permalink
fix: encoding typo (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
bxcodec authored Jun 9, 2024
1 parent 9caeeca commit dfdf7c0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
31 changes: 23 additions & 8 deletions encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion publisher/rabbitmq/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit dfdf7c0

Please sign in to comment.