snapshot of https://github.com/ThreeDotsLabs/watermill.git (master, 2026-09-15; blobs > 131072B omitted)
Some checks failed
master / ci (push) Failing after 0s
Some checks failed
master / ci (push) Failing after 0s
This commit is contained in:
58
components/cqrs/name.go
Normal file
58
components/cqrs/name.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package cqrs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// FullyQualifiedStructName returns object name in format [package].[type name].
|
||||
// For example, for the struct:
|
||||
//
|
||||
// package events
|
||||
// type UserCreated struct {}
|
||||
//
|
||||
// it will return "events.UserCreated".
|
||||
//
|
||||
// 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 returns struct name in format [type name].
|
||||
// For example, for the struct:
|
||||
//
|
||||
// package events
|
||||
// type UserCreated struct {}
|
||||
//
|
||||
// it will return "UserCreated".
|
||||
//
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user