Skip to content

Commit

Permalink
Merge pull request #16 from kumparan/addParseNatsEventMessageFromBytes
Browse files Browse the repository at this point in the history
feature: add ParseNatsEventMessageFromBytes
  • Loading branch information
atjhoendz authored Oct 7, 2022
2 parents 1d973f0 + 0f1f780 commit 23c0bdd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
7 changes: 7 additions & 0 deletions event_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,13 @@ func ParseJSON(in string) (*NatsEventMessage, error) {
return msg, err
}

// ParseNatsEventMessageFromBytes :nodoc:
func ParseNatsEventMessageFromBytes(in []byte) (*NatsEventMessage, error) {
msg := &NatsEventMessage{}
err := tapao.Unmarshal(in, msg, tapao.With(tapao.JSON))
return msg, err
}

// ParseNatsEventAuditLogMessageFromBytes :nodoc:
func ParseNatsEventAuditLogMessageFromBytes(in []byte) (*NatsEventAuditLogMessage, error) {
msg := &NatsEventAuditLogMessage{}
Expand Down
17 changes: 17 additions & 0 deletions event_message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,23 @@ func TestNatsEventMessage_ParseJSON(t *testing.T) {
assert.Equal(t, msg, parsed)
}

func TestNatsEventMessage_ParseNatsEventMessageFromBytes(t *testing.T) {
now := time.Now().Format(NatsEventTimeFormat)
json := fmt.Sprintf("{\"NatsEvent\":{\"id\":123,\"user_id\":333,\"tenant_id\":0,\"time\":\"%s\",\"subject\":\"\"},\"body\":\"[\\\"test\\\"]\",\"old_body\":\"\",\"request\":null,\"error\":null}", now)
natsEvent := &NatsEvent{
ID: 123,
UserID: 333,
Time: now,
}
body := []string{"test"}
msg := NewNatsEventMessage().WithEvent(natsEvent).WithBody(body)

parsed, err := ParseNatsEventMessageFromBytes([]byte(json))
require.NoError(t, err)

assert.Equal(t, msg, parsed)
}

func TestNatsEventAuditLogMessage_Build(t *testing.T) {
type User struct {
ID int64 `json:"id"`
Expand Down

0 comments on commit 23c0bdd

Please sign in to comment.