-
Notifications
You must be signed in to change notification settings - Fork 414
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
127d033
commit cc6954d
Showing
6 changed files
with
105 additions
and
39 deletions.
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.