Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: encoding typo #12

Merged
merged 2 commits into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading