-
Notifications
You must be signed in to change notification settings - Fork 290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow multiple annotations per function #899
Comments
Hello @sywhang, Sorry to dig up an old issue, for me it's very relevant and I'd like to support the idea with an additional use case. The way I write a service in Go is by defining an interface that declares the core "business" methods, then have a structure implement it. Most of the time, this structure also has To date, the best way to do this with Fx (v1.21) is as follows: func provideService(lc fx.Lifecycle) Service {
s := NewService() // NewService returns a pointer to the structure type, not the interface, which allows us to access the Init and Shutdown methods.
lc.Append(fx.StartStopHook(s.Init, s.Shutdown))
return s
}
var Module = fx.Module("service", fx.Provide(provideService)) Or: var Module = fx.Module("service",
fx.Provide(
fx.Annotate(NewService, fx.OnStart((*ServiceImpl).Init), fx.OnStop((*ServiceImpl).Shutdown)),
func(s *ServiceImpl) Service { return s },
),
) If we could annotate an annotated function, I think we could rewrite the above code as follows: var _ Service = (*ServiceImpl)(nil) // Interface guard to ensure that the call to fx.As can be satisfied
var Module = fx.Module("service",
fx.Provide(
fx.Annotate(
fx.Annotate(NewService, fx.OnStart((*ServiceImpl).Init), fx.OnStop((*ServiceImpl).Shutdown)),
fx.As(new(Service)),
),
),
) Also note that merging the two calls to
Let me know if you're still interested in solving this issue, I can take a closer look if necessary. |
hey @vallahaye, sorry for the delayed response - I haven't been too active on GitHub lately so apologies for the delay. Yup, I am aware of fx.Self that was recently added. @JacobOaks who authored that PR works with me at Uber and we did look at your comment above during triaging, which inspired him to do that work :-) . We'll be tagging a release shortly that releases that feature. Meanwhile, allowing multiple annotations per function is still an issue we need to address and that may take a while more since the change involved is quite a bit more work. HTH - thanks! |
With the new
OnStart
annotations being added in #895, some annotations actually might make sense for multiple annotations to take place for the same function.For instance, the following nested annotation would allow us to specify OnStart hooks using annotations without being confusing about the order in which the hooks are executed:
The text was updated successfully, but these errors were encountered: