@@ -4,12 +4,39 @@ import (
4
4
"reflect"
5
5
)
6
6
7
- // GetType returns the type of a target.
7
+ // fullTypeID builds a stable identifier for a type in the form "<pkgpath>.<TypeName>".
8
+ func fullTypeID (t reflect.Type ) string {
9
+ if t == nil {
10
+ return ""
11
+ }
12
+
13
+ // Unwrap pointers to get the underlying named type (if any).
14
+ for t .Kind () == reflect .Pointer {
15
+ t = t .Elem ()
16
+ }
17
+
18
+ // For named types, PkgPath() + Name() gives a unique and stable identity.
19
+ if t .Name () != "" && t .PkgPath () != "" {
20
+ return t .PkgPath () + "." + t .Name ()
21
+ }
22
+
23
+ // Fallback for non-named kinds (slices, maps, func, etc.).
24
+ return t .String ()
25
+ }
26
+
27
+ // GetType returns a stable identifier for the given target’s type.
8
28
func GetType (target any ) string {
9
- return reflect .TypeOf (target ). String ( )
29
+ return fullTypeID ( reflect .TypeOf (target ))
10
30
}
11
31
12
- // GetReturnType returns the return type of a target.
32
+ // GetReturnType returns a stable identifier for the return type of constructor-like target.
33
+ // If a target is a function, we examine its first return value (index 0), unwrap pointers, and
34
+ // build an identifier for that named type. For non-function or empty-return cases, we return "".
13
35
func GetReturnType (target any ) string {
14
- return reflect .TypeOf (target ).Out (0 ).String ()
36
+ t := reflect .TypeOf (target )
37
+ if t == nil || t .Kind () != reflect .Func || t .NumOut () == 0 {
38
+ return ""
39
+ }
40
+
41
+ return fullTypeID (t .Out (0 ))
15
42
}
0 commit comments