-
Notifications
You must be signed in to change notification settings - Fork 294
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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
Impossible to inject struct that is part of a group by itself #1181
Comments
Hey @giovannizotta, thanks for the issue! Other maintainers please correct me if I'm wrong but: I think the problem is that from Fx's perspective, types annotated with group or name tags are considered their own distinct types. Looking at the logs from the example you gave,
The current code is providing the result of The way to get around this right now is to provide the same instance but both annotated w/ the group tag and unannotated. For example, by returning a result struct from the fetcher constructors like: type GameFetcherResult struct {
fx.Out
GameFetcher GameFetcher
Fetcher Fetcher `group:"fetchers"`
}
func newGameFetcher() GameFetcherResult {
res := &GameFetcherImpl{}
return GameFetcherResult{
GameFetcher: res,
Fetcher: res,
}
} (runnable/error-free example: https://go.dev/play/p/2Mh7-kVqP10) As a side note, this is somewhat related to these issues - #998, #1036 - in that Fx does not support a clean way to both name an instance and place it in a group, and be able to depend elsewhere on either the individual object or the group. There is discussion/WIP to allow annotating a type w/ both a name and a group tag, and be able to retrieve either the entire group, or one specific item (uber-go/dig#381). With this, you could annotate the result of newGameFetcher w/ something like:
and then be able to get both the group and the individual game fetcher. |
Thank you @JacobOaks the quick response and for the explanation!
This is indeed what I was missing. The rest all makes sense, thanks for the information, I'll stay on the lookout for a better way to achieve this in the future! |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
Describe the bug
When trying to inject a struct that is part of a group (tagged via
ResultTags
) singularily,fx
cannot find the requested dependency. Let's say we have two interfaces:GameFetcher
andUserFetcher
, both embedding theFetcher
interface.Both implementations are provided as part of the
fetchers
group, annotated with both interfaces:I am not sure whether this is a bug or whether I'm doing things wrong, but I'd appreciate some guidance.
To Reproduce
https://gist.github.com/giovannizotta/d74a5ccfa28208ad2f582590d10b605c
Output:
Expected behavior
I would expect the second invoked function to be injected with the
GameFetcher
, since fx provided it earlier:Additional context
I found it possible to achieve what I describe in another way, by injecting a slice of the specific elements
[]GameFetcher
:However, I find this undesirable because I know there will always be one element in the slice, even though I am forced to provide a slice of
GameFetcher
. It could very well be that I'm doing things wrong and I'm missing something, please let me know if there is a better way to do this!The text was updated successfully, but these errors were encountered: