diff --git a/event_message.go b/event_message.go index 3eec53c..1bc4e20 100644 --- a/event_message.go +++ b/event_message.go @@ -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" ) @@ -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 @@ -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{} @@ -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 } @@ -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 } diff --git a/event_message_test.go b/event_message_test.go index 824adfd..e0fceff 100644 --- a/event_message_test.go +++ b/event_message_test.go @@ -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) {