-
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 5 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,42 @@ | ||
package cqrs | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// ObjectName name returns object name in format [package].[type name]. | ||
// It ignores if the value is a pointer or not. | ||
func ObjectName(v interface{}) string { | ||
s := fmt.Sprintf("%T", v) | ||
s = strings.TrimLeft(s, "*") | ||
|
||
return s | ||
} | ||
|
||
// StructName name returns struct name in format [type name]. | ||
// It ignores if the value is a pointer or not. | ||
func StructName(v interface{}) string { | ||
segments := strings.Split(fmt.Sprintf("%T", v), ".") | ||
|
||
return segments[len(segments)-1] | ||
} | ||
|
||
type namedMessage interface { | ||
roblaszczak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Name() string | ||
} | ||
|
||
// MessageName returns the name from a message implementing the following interface: | ||
// type namedMessage interface { | ||
// Name() string | ||
// } | ||
// It ignores if the value is a pointer or not. | ||
func MessageName(fallback func(v interface{}) string) func(v interface{}) string { | ||
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. what do you think about NamedObject? 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. Following the pattern I suggested above, this could be 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. 👍 |
||
return func(v interface{}) string { | ||
if v, ok := v.(namedMessage); ok { | ||
return v.Name() | ||
} | ||
|
||
return fallback(v) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package cqrs_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/ThreeDotsLabs/watermill/components/cqrs" | ||
) | ||
|
||
func TestObjectName(t *testing.T) { | ||
type Object struct{} | ||
|
||
assert.Equal(t, "cqrs_test.Object", cqrs.ObjectName(Object{})) | ||
assert.Equal(t, "cqrs_test.Object", cqrs.ObjectName(&Object{})) | ||
} | ||
|
||
func BenchmarkObjectName(b *testing.B) { | ||
type Object struct{} | ||
o := Object{} | ||
|
||
for i := 0; i < b.N; i++ { | ||
cqrs.ObjectName(o) | ||
} | ||
} | ||
|
||
func TestStructName(t *testing.T) { | ||
type Object struct{} | ||
|
||
assert.Equal(t, "Object", cqrs.StructName(Object{})) | ||
assert.Equal(t, "Object", cqrs.StructName(&Object{})) | ||
} | ||
|
||
func TestMessageName(t *testing.T) { | ||
assert.Equal(t, "named object", cqrs.MessageName(cqrs.StructName)(namedObject{})) | ||
assert.Equal(t, "named object", cqrs.MessageName(cqrs.StructName)(&namedObject{})) | ||
|
||
// Test fallback | ||
type Object struct{} | ||
|
||
assert.Equal(t, "Object", cqrs.MessageName(cqrs.StructName)(Object{})) | ||
assert.Equal(t, "Object", cqrs.MessageName(cqrs.StructName)(&Object{})) | ||
} | ||
|
||
type namedObject struct{} | ||
|
||
func (namedObject) Name() string { | ||
return "named object" | ||
} |
This file was deleted.
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.
maybe ObjectNameWithoutPackage? it is pretty long, but explicit :)
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.
I was rather thinking about renaming
ObjectName
toFullyQualifiedStructName
and leave this as is, butObjectNameWithoutPackage
works as well.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.
ok, like I like the idea of FullyQualifiedStructName :)