Skip to content

Commit

Permalink
Upstream TheRafaBonin's event helper
Browse files Browse the repository at this point in the history
  • Loading branch information
mactep committed Dec 18, 2023
1 parent 7a3f82c commit 974b327
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion pkg/events/handler.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package events

import "context"
import (
"context"
"regexp"

"github.com/TheRafaBonin/roxy"
)

type HandlerResponse int

Expand Down Expand Up @@ -30,3 +35,45 @@ type Handler interface {
// The function that will be called when a subscribed message is received.
Handle(ctx context.Context, topic string, decoder EventDecoder) HandlerResponse
}

/*
MatchTopicAndFormatsMessage matches the topic and formats the message.
It takes the following parameters:
- ctx: the context.Context object for the function.
- decoder: the thunderEvents.EventDecoder object for decoding the message.
- referenceTopic: the reference topic string for matching.
- topic: the topic string to match against the reference topic.
- message: the message object to be formatted.
It returns a pointer to the formatted message object and any error encountered.
if the match fails, nil is returned.
*/
func MatchTopicAndFormatsMessage[T interface{}](
ctx context.Context,
decoder EventDecoder,
referenceTopic string,
topic string,
message T,
) (*T, error) {
// Declare some variables
var match bool
var err error

match, err = regexp.MatchString(referenceTopic, topic)
err = roxy.Wrap(err, "Failed to match payment status topic string")
if err != nil {
return nil, err
}
if match {
err := decoder.Decode(&message)
roxy.Wrap(err, "unmarshalling message")
if err != nil {
return nil, err
}

return &message, nil
}

return nil, nil
}

0 comments on commit 974b327

Please sign in to comment.