Giter Site home page Giter Site logo

Comments (1)

JacobOaks avatar JacobOaks commented on August 19, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.