Files
2026-09-15 21:27:46 +05:00

59 lines
1.2 KiB
Go

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)
}
}