Skip to content

Commit 2042e33

Browse files
authored
fix(fxhttpserver): Fix HTTP handlers/middlewares dependency injection signature (#362)
1 parent 50dbd83 commit 2042e33

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

fxhttpserver/reflect.go

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
1030
func 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 "".
1537
func 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.

fxhttpserver/reflect_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ func TestGetType(t *testing.T) {
1515
target any
1616
expected string
1717
}{
18+
{nil, ""},
1819
{123, "int"},
1920
{"test", "string"},
2021
{echo.MiddlewareFunc(func(next echo.HandlerFunc) echo.HandlerFunc {
2122
return next
22-
}), "echo.MiddlewareFunc"},
23+
}), "github.com/labstack/echo/v4.MiddlewareFunc"},
2324
}
2425

2526
for _, tt := range tests {
@@ -41,8 +42,12 @@ func TestGetReturnType(t *testing.T) {
4142
target any
4243
expected string
4344
}{
45+
{nil, ""},
4446
{func() string { return "test" }, "string"},
4547
{func() int { return 123 }, "int"},
48+
{echo.MiddlewareFunc(func(next echo.HandlerFunc) echo.HandlerFunc {
49+
return next
50+
}), "github.com/labstack/echo/v4.HandlerFunc"},
4651
}
4752

4853
for _, tt := range tests {

0 commit comments

Comments
 (0)