-
Notifications
You must be signed in to change notification settings - Fork 388
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
roblaszczak
merged 6 commits into
ThreeDotsLabs:master
from
sagikazarmark:cqrs-marshalers
May 25, 2019
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5405e49
Add name marshaler
sagikazarmark a476006
Add struct name marshaler
sagikazarmark 9801b33
Extract name generator functions
sagikazarmark aee4af5
Replace name marshalers with name generator functions
sagikazarmark 8400f41
Add fallback to message name generator
sagikazarmark c420641
Improve name generator names
sagikazarmark File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package cqrs | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// FullyQualifiedStructName name returns object name in format [package].[type name]. | ||
// It ignores if the value is a pointer or not. | ||
func FullyQualifiedStructName(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 namedStruct interface { | ||
Name() string | ||
} | ||
|
||
// NamedStruct returns the name from a message implementing the following interface: | ||
// type namedStruct interface { | ||
// Name() string | ||
// } | ||
// It ignores if the value is a pointer or not. | ||
func NamedStruct(fallback func(v interface{}) string) func(v interface{}) string { | ||
return func(v interface{}) string { | ||
if v, ok := v.(namedStruct); ok { | ||
return v.Name() | ||
} | ||
|
||
return fallback(v) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 TestFullyQualifiedStructName(t *testing.T) { | ||
type Object struct{} | ||
|
||
assert.Equal(t, "cqrs_test.Object", cqrs.FullyQualifiedStructName(Object{})) | ||
assert.Equal(t, "cqrs_test.Object", cqrs.FullyQualifiedStructName(&Object{})) | ||
} | ||
|
||
func BenchmarkFullyQualifiedStructName(b *testing.B) { | ||
type Object struct{} | ||
o := Object{} | ||
|
||
for i := 0; i < b.N; i++ { | ||
cqrs.FullyQualifiedStructName(o) | ||
} | ||
} | ||
|
||
func TestStructName(t *testing.T) { | ||
type Object struct{} | ||
|
||
assert.Equal(t, "Object", cqrs.StructName(Object{})) | ||
assert.Equal(t, "Object", cqrs.StructName(&Object{})) | ||
} | ||
|
||
func TestNamedStruct(t *testing.T) { | ||
assert.Equal(t, "named object", cqrs.NamedStruct(cqrs.StructName)(namedObject{})) | ||
assert.Equal(t, "named object", cqrs.NamedStruct(cqrs.StructName)(&namedObject{})) | ||
|
||
// Test fallback | ||
type Object struct{} | ||
|
||
assert.Equal(t, "Object", cqrs.NamedStruct(cqrs.StructName)(Object{})) | ||
assert.Equal(t, "Object", cqrs.NamedStruct(cqrs.StructName)(&Object{})) | ||
} | ||
|
||
type namedObject struct{} | ||
|
||
func (namedObject) Name() string { | ||
return "named object" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 :)