Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CQRS marshalers #78

Merged
merged 6 commits into from
May 25, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions components/cqrs/marshaler_name.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package cqrs

import (
"github.com/ThreeDotsLabs/watermill/message"
)

// NameMarshaler retrieves the name from a message implementing the following interface:
// type namedMessage interface {
// Name() string
// }
//
// It wraps another marshaler which takes care of the message serialization/deserialization.
// If none provided, it falls back to JSON marshaler.
type NameMarshaler struct {
Marshaler CommandEventMarshaler
}

func (m *NameMarshaler) marshaler() CommandEventMarshaler {
if m.Marshaler == nil {
return JSONMarshaler{}
}

return m.Marshaler
}

type namedMessage interface {
Name() string
}

func (m *NameMarshaler) Marshal(v interface{}) (*message.Message, error) {
msg, err := m.marshaler().Marshal(v)
if err != nil {
return nil, err
}

msg.Metadata.Set("name", m.Name(v))

return msg, nil
}

func (m *NameMarshaler) Unmarshal(msg *message.Message, v interface{}) error {
return m.marshaler().Unmarshal(msg, v)
}

func (m *NameMarshaler) Name(v interface{}) string {
if v, ok := v.(namedMessage); ok {
return v.Name()
}

return m.marshaler().Name(v)
}

func (m *NameMarshaler) NameFromMessage(msg *message.Message) string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, I'm not sure if the marshaller should have this method. Maybe I'm losing some context here but for me, it should go to message.Message.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's part of the CommandEventMarshaler interface

return msg.Metadata.Get("name")
}
61 changes: 61 additions & 0 deletions components/cqrs/marshaler_name_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package cqrs_test

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/ThreeDotsLabs/watermill"
"github.com/ThreeDotsLabs/watermill/components/cqrs"
)

type NamedEvent struct {
TestEvent
}

func (e NamedEvent) Name() string {
return "testEvent"
}

func TestNameMarshaler(t *testing.T) {
eventToMarshal := NamedEvent{
TestEvent: TestEvent{
ID: watermill.NewULID(),
When: time.Date(2016, time.August, 15, 14, 13, 12, 0, time.UTC),
},
}

marshaler := &cqrs.NameMarshaler{}

msg, err := marshaler.Marshal(eventToMarshal)
require.NoError(t, err)

assert.Equal(t, "testEvent", marshaler.NameFromMessage(msg))

eventToUnmarshal := NamedEvent{}
err = marshaler.Unmarshal(msg, &eventToUnmarshal)
require.NoError(t, err)

assert.EqualValues(t, eventToMarshal, eventToUnmarshal)
assert.Equal(t, "testEvent", marshaler.Name(eventToUnmarshal))
}

func TestNameMarshaler_NotNamedMessage(t *testing.T) {
eventToMarshal := TestEvent{
ID: watermill.NewULID(),
When: time.Date(2016, time.August, 15, 14, 13, 12, 0, time.UTC),
}

marshaler := &cqrs.NameMarshaler{}

msg, err := marshaler.Marshal(eventToMarshal)
require.NoError(t, err)

eventToUnmarshal := TestEvent{}
err = marshaler.Unmarshal(msg, &eventToUnmarshal)
require.NoError(t, err)

assert.EqualValues(t, eventToMarshal, eventToUnmarshal)
}
49 changes: 49 additions & 0 deletions components/cqrs/marshaler_structname.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package cqrs

import (
"fmt"
"strings"

"github.com/ThreeDotsLabs/watermill/message"
)

// StructNameMarshaler uses the struct name (without the package) as the message name.
//
// It wraps another marshaler which takes care of the message serialization/deserialization.
// If none provided, it falls back to JSON marshaler.
type StructNameMarshaler struct {
Marshaler CommandEventMarshaler
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

after some thinking, maybe it will be a better idea to not add it as an implementation of Marshaler interface, but like function and add an extra attribute for existing marshallers, like:

type JSONMarshaler struct {
	NewUUID func() string
        GenerateName func(v interface{}) string
}

with default GenerateName = ObjectName.
In that case, you may rename object.go to something more meaningful, and keep all names generations methods like functions

In that case, the implementations are still more about output format, and naming is decoupled.

What do you think? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me. I will update the PR soon

}

func (m *StructNameMarshaler) marshaler() CommandEventMarshaler {
if m.Marshaler == nil {
return JSONMarshaler{}
}

return m.Marshaler
}

func (m *StructNameMarshaler) Marshal(v interface{}) (*message.Message, error) {
msg, err := m.marshaler().Marshal(v)
if err != nil {
return nil, err
}

msg.Metadata.Set("name", m.Name(v))

return msg, nil
}

func (m *StructNameMarshaler) Unmarshal(msg *message.Message, v interface{}) error {
return m.marshaler().Unmarshal(msg, v)
}

func (m *StructNameMarshaler) Name(v interface{}) string {
segments := strings.Split(fmt.Sprintf("%T", v), ".")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if v is pointer it will have * prefix, do we want to remove it? 😉

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so, but it gets removed: https://play.golang.org/p/97dR-e246vB

Copy link
Member

@roblaszczak roblaszczak May 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, of course, it is :)


return segments[len(segments)-1]
}

func (m *StructNameMarshaler) NameFromMessage(msg *message.Message) string {
return msg.Metadata.Get("name")
}
33 changes: 33 additions & 0 deletions components/cqrs/marshaler_structname_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package cqrs_test

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/ThreeDotsLabs/watermill"
"github.com/ThreeDotsLabs/watermill/components/cqrs"
)

func TestStructNameMarshaler(t *testing.T) {
eventToMarshal := TestEvent{
ID: watermill.NewULID(),
When: time.Date(2016, time.August, 15, 14, 13, 12, 0, time.UTC),
}

marshaler := &cqrs.StructNameMarshaler{}

msg, err := marshaler.Marshal(eventToMarshal)
require.NoError(t, err)

assert.Equal(t, "TestEvent", marshaler.NameFromMessage(msg))

eventToUnmarshal := TestEvent{}
err = marshaler.Unmarshal(msg, &eventToUnmarshal)
require.NoError(t, err)

assert.EqualValues(t, eventToMarshal, eventToUnmarshal)
assert.Equal(t, "TestEvent", marshaler.Name(eventToUnmarshal))
}