-
Hello, First of all thank you for the great work! I have a question: The Decorate function doesn't work with a dependency group while it works with a dependency. Am I missing something? Here is the repro commit: https://github.com/ermanimer/common/tree/1bd9b2f20c5bb7eda17bf71b4305c7c090916fc3 It works with a dependency: // Package samplemiddleware ...
package samplemiddleware
import (
"log/slog"
"net/http"
"github.com/ermanimer/common/httpserver"
"go.uber.org/fx"
)
// Module ...
var Module = fx.Module(
"sample_middleware",
fx.Provide(
New,
),
fx.Decorate(
wrapLogger,
),
)
// Parameter ...
type Parameter struct {
fx.In
Logger *slog.Logger
}
// Result ...
type Result struct {
fx.Out
Middleware httpserver.Middleware `name:"sample_middleware"`
}
type middleware struct {
logger *slog.Logger
next httpserver.Route
}
// New ...
func New(parameter Parameter) Result {
var sampleMiddleware httpserver.Middleware = func(next httpserver.Route) httpserver.Route {
return &middleware{
logger: parameter.Logger,
next: next,
}
}
result := Result{
Middleware: sampleMiddleware,
}
return result
}
func wrapLogger(logger *slog.Logger) *slog.Logger {
return logger.With("module", "sample_middleware")
}
func (m *middleware) Method() string {
return m.next.Method()
}
func (m *middleware) Path() string {
return m.next.Path()
}
func (m *middleware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
m.logger.Info("sample middleware")
m.next.ServeHTTP(w, r)
} But it doesn't work with a dependency group: // Package sampleroute ...
package sampleroute
import (
"log/slog"
"net/http"
"github.com/ermanimer/common/httpserver"
"go.uber.org/fx"
)
// Module ...
var Module = fx.Module(
"sample_route",
fx.Provide(
New,
),
fx.Decorate(
wrapLogger,
),
)
// Parameter ...
type Parameter struct {
fx.In
Logger *slog.Logger
SampleMiddleware httpserver.Middleware `name:"sample_middleware"`
}
// Result ...
type Result struct {
fx.Out
Route httpserver.Route `group:"routes"`
}
type route struct {
logger *slog.Logger
}
// New ...
func New(parameter Parameter) Result {
var sampleRoute httpserver.Route = &route{
logger: parameter.Logger,
}
sampleRoute = parameter.SampleMiddleware(sampleRoute)
result := Result{
Route: sampleRoute,
}
return result
}
func wrapLogger(logger *slog.Logger) *slog.Logger {
return logger.With("module", "sample_middleware")
}
func (r *route) Method() string {
return http.MethodGet
}
func (r *route) Path() string {
return "/sample"
}
func (r *route) ServeHTTP(w http.ResponseWriter, _ *http.Request) {
r.logger.Info("sample route")
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte("sample route\n"))
if err != nil {
r.logger.Error("failed to write response", "error", err)
}
}
func (r *route) Logger() *slog.Logger {
return r.logger
} Any help is appreciated. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi there, thanks for bringing this up. This issue has been reported before (#1104). It's actually an issue in Dig - a dependency of Fx - that has been fixed (uber-go/dig#393), we just need to upgrade Fx to use the latest Dig version - I have a PR out for that now (#1138). One that merges, this issue should be solved. |
Beta Was this translation helpful? Give feedback.
Hi there, thanks for bringing this up. This issue has been reported before (#1104). It's actually an issue in Dig - a dependency of Fx - that has been fixed (uber-go/dig#393), we just need to upgrade Fx to use the latest Dig version - I have a PR out for that now (#1138). One that merges, this issue should be solved.