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