Skip to content

Commit

Permalink
Merge pull request #8 from kumparan/feature/add-IDStr-field
Browse files Browse the repository at this point in the history
feature: add IDStr field
  • Loading branch information
zipzap11 authored Aug 23, 2022
2 parents 1bbb160 + 6a6c7e9 commit 60e3c3a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
15 changes: 12 additions & 3 deletions event_message.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package ferstream

import (
"github.com/kumparan/go-utils"
"google.golang.org/protobuf/proto"

"github.com/kumparan/go-utils"
"github.com/kumparan/tapao"
"github.com/pkg/errors"
)
Expand All @@ -12,6 +12,7 @@ type (
// NatsEvent :nodoc:
NatsEvent struct {
ID int64 `json:"id"`
IDString string `json:"id_string"`
UserID int64 `json:"user_id"`
TenantID int64 `json:"tenant_id"`
Subject string `json:"subject"` // empty on publish
Expand Down Expand Up @@ -66,6 +67,14 @@ func (n *NatsEvent) GetSubject() string {
return n.Subject
}

// GetIDString :nodoc:
func (n *NatsEvent) GetIDString() string {
if n == nil {
return ""
}
return n.IDString
}

// NewNatsEventMessage :nodoc:
func NewNatsEventMessage() *NatsEventMessage {
return &NatsEventMessage{}
Expand All @@ -78,7 +87,7 @@ func (n *NatsEventMessage) Build() (data []byte, err error) {
}

if n.NatsEvent == nil {
n.wrapError(errors.New("empty nats nats event"))
n.wrapError(errors.New("empty nats event"))
return nil, n.Error
}

Expand All @@ -93,7 +102,7 @@ func (n *NatsEventMessage) Build() (data []byte, err error) {

// WithEvent :nodoc:
func (n *NatsEventMessage) WithEvent(e *NatsEvent) *NatsEventMessage {
if e.GetID() <= 0 {
if e.GetID() <= 0 && e.GetIDString() == "" {
n.wrapError(errors.New("empty id"))
return n
}
Expand Down
42 changes: 30 additions & 12 deletions event_message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,21 +179,39 @@ func TestNatsEventMessage_Build(t *testing.T) {
}

func TestNatsEventMessage_ToJSONString(t *testing.T) {
natsEvent := &NatsEvent{
ID: 123,
UserID: 333,
}
body := []string{"test"}
t.Run("success", func(t *testing.T) {
natsEvent := &NatsEvent{
ID: 123,
UserID: 333,
}
body := []string{"test"}

msg := NewNatsEventMessage().
WithEvent(natsEvent).
WithBody(body)
msg := NewNatsEventMessage().
WithEvent(natsEvent).
WithBody(body)

parsed, err := msg.ToJSONString()
require.NoError(t, err)
parsed, err := msg.ToJSONString()
require.NoError(t, err)
expectedRes := "{\"NatsEvent\":{\"id\":123,\"id_string\":\"\",\"user_id\":333,\"tenant_id\":0,\"subject\":\"\"},\"body\":\"[\\\"test\\\"]\",\"old_body\":\"\",\"request\":null,\"error\":null}"
assert.Equal(t, expectedRes, parsed)
})

expectedRes := "{\"NatsEvent\":{\"id\":123,\"user_id\":333,\"tenant_id\":0,\"subject\":\"\"},\"body\":\"[\\\"test\\\"]\",\"old_body\":\"\",\"request\":null,\"error\":null}"
assert.Equal(t, expectedRes, parsed)
t.Run("success with IDString", func(t *testing.T) {
natsEvent := &NatsEvent{
UserID: 333,
IDString: "630484ae00f0d71df588a0ab",
}
body := []string{"test"}

msg := NewNatsEventMessage().
WithEvent(natsEvent).
WithBody(body)

parsed, err := msg.ToJSONString()
require.NoError(t, err)
expectedRes := "{\"NatsEvent\":{\"id\":0,\"id_string\":\"630484ae00f0d71df588a0ab\",\"user_id\":333,\"tenant_id\":0,\"subject\":\"\"},\"body\":\"[\\\"test\\\"]\",\"old_body\":\"\",\"request\":null,\"error\":null}"
assert.Equal(t, expectedRes, parsed)
})
}

func TestNatsEventMessage_ToJSONByte(t *testing.T) {
Expand Down

0 comments on commit 60e3c3a

Please sign in to comment.