-
Notifications
You must be signed in to change notification settings - Fork 414
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
Changes from 2 commits
5405e49
a476006
9801b33
aee4af5
8400f41
c420641
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 { | ||
return msg.Metadata.Get("name") | ||
} |
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) | ||
} |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
with default In that case, the implementations are still more about output format, and naming is decoupled. What do you think? :) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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), ".") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if v is pointer it will have There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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") | ||
} |
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)) | ||
} |
There was a problem hiding this comment.
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
.There was a problem hiding this comment.
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