Skip to content

Commit

Permalink
feature-fix: change field name to IDString
Browse files Browse the repository at this point in the history
  • Loading branch information
Francisco committed Aug 23, 2022
1 parent e54c9c9 commit 48e5a8e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 17 deletions.
10 changes: 5 additions & 5 deletions event_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type (
// NatsEvent :nodoc:
NatsEvent struct {
ID int64 `json:"id"`
IDStr string `json:"id_str"`
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 @@ -67,12 +67,12 @@ func (n *NatsEvent) GetSubject() string {
return n.Subject
}

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

// NewNatsEventMessage :nodoc:
Expand Down Expand Up @@ -102,7 +102,7 @@ func (n *NatsEventMessage) Build() (data []byte, err error) {

// WithEvent :nodoc:
func (n *NatsEventMessage) WithEvent(e *NatsEvent) *NatsEventMessage {
if e.GetID() <= 0 && e.GetIDStr() == "" {
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,\"id_str\":\"\",\"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 48e5a8e

Please sign in to comment.