Comments (1)
Hey @Dan6erbond thanks for the issue.
With the way that ResultTags
works, the code you showed is applying the router
name to both the http.Handler
and the chi.Router
. Named types are considered entirely separate from their unnamed counterparts from Fx's perspective. That's why you see Fx complaining about not having an unnamed http.Handler
:
missing dependencies <...> missing type: http.Handler
.
For now, you'll have to workaround this by either doing two returns and annotating them independently like:
func NewRouter() (chi.Router, http.Handler) {
/* logic to create the router */
return router, router
}
fx.Provide(
fx.Annotate(
NewRouter,
fx.ResultTags(`name:"router"`),
)
)
Or if you can't change NewRouter
, you can sort of manually annotate using "constructors" to add the 2nd representation.
fx.Provide(
NewRouter, // let's say this returns a *routerImpl
func(r *routerImpl) http.Handler { return r },
fx.Annotate(
func(r *routerImpl) chi.Router { return r },
fx.ResultTags(`name:"router"`),
),
)
This does unfortunately expose the underlying type to the container. If you can provide the exact definitions of chiRouter
and the return types of NewRouter
I can help you with an example to avoid this if that's an isue.
This is admittedly not very ergonomic and is an unfortunate consequence of how annotations work, which is why #899 is on our radar. Nested annotations would help a little bit allowing you to do something like this to keep the handler unnamed but the router named:
fx.Provide(
fx.Annotate(
fx.Annotate(
NewRouter,
fx.As(new(http.Handler)),
),
fx.As(new(chi.Router)),
fx.ResultTags(`name:"router"`),
)
from fx.
Related Issues (20)
- https://github.com/benbjohnson/clock is EOL/archived. HOT 1
- fx.Decorate does not add a missing object to the module HOT 3
- When combining fx.As to annotate return type as interface with optional parameters nil value check is not working as expected HOT 1
- Startup order isn't preserved when using modules HOT 3
- Example test failing in master branch when port 8080 is already in use (89f4a90) HOT 3
- fxevent.Logger: Silence until there's an error
- Slog adapter for fx HOT 4
- Release method HOT 10
- fxtest.Lifecycle: Enforce timeout
- Impossible to inject struct that is part of a group by itself HOT 2
- Support Multiple ParamTags? HOT 1
- signalReceivers should call `Signal.Stop(recv.signals)` when stopped
- Release v1.22.0
- Private supplies HOT 1
- Support multiple ResultTags annotations
- Opt-out of signal handling HOT 12
- Add constructor runtime to fxevent.Run events
- Deadlock in `signalReceivers.Stop` HOT 6
- Decorate does not work if inside fx.Module and Populate is called from outside Module HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fx.