github.com/wfusion/gofusion@v1.1.14/common/infra/watermill/components/cqrs/name.go (about)

     1  package cqrs
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  // FullyQualifiedStructName name returns object name in format [package].[type name].
     9  // It ignores if the value is a pointer or not.
    10  func FullyQualifiedStructName(v any) string {
    11  	s := fmt.Sprintf("%T", v)
    12  	s = strings.TrimLeft(s, "*")
    13  
    14  	return s
    15  }
    16  
    17  // StructName name returns struct name in format [type name].
    18  // It ignores if the value is a pointer or not.
    19  func StructName(v any) string {
    20  	segments := strings.Split(fmt.Sprintf("%T", v), ".")
    21  
    22  	return segments[len(segments)-1]
    23  }
    24  
    25  type namedStruct interface {
    26  	Name() string
    27  }
    28  
    29  // NamedStruct returns the name from a message implementing the following interface:
    30  //
    31  //	type namedStruct interface {
    32  //		Name() string
    33  //	}
    34  //
    35  // It ignores if the value is a pointer or not.
    36  func NamedStruct(fallback func(v any) string) func(v any) string {
    37  	return func(v any) string {
    38  		if v, ok := v.(namedStruct); ok {
    39  			return v.Name()
    40  		}
    41  
    42  		return fallback(v)
    43  	}
    44  }