-
Notifications
You must be signed in to change notification settings - Fork 13
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
Factory function returning other di.Provide #51
Comments
I got it working by using
|
Another issue I found with using One useful addition would be to have an option that allows to pass the parent.
Happy to create a PR if you think that could work. On the same topic about parents, there is also an issue if you have a container with a parent and you try to invoke or provide something that needs to get the Example:
fails with error |
Hey @defval, any thoughts on this? I can probably cook some utility function, but I would like to know how you would solve this first. |
I did some thinking and maybe adding a
Thoughts? |
Created a PR to propose a solution |
Hello @matdurand, Sorry for the delayed reply. I've reviewed your suggestion of using a Additionally, I've been contemplating a type Backup struct{}
type Cfg struct {
BackupEnabled bool
}
func IsBackupEnabled(cfg *Cfg) (bool, error) {
return cfg.BackupEnabled, nil
}
func main() {
c, err := di.New(
di.ProvideValue(Cfg{BackupEnabled: true}),
di.ProvideValue(Backup{}, di.When(IsBackupEnabled))
)
if err != nil {
panic(err)
}
//...
} This method could offer a more streamlined approach to managing conditional dependencies. Currently, I'm unable to dedicate time for development and testing of this feature, but I'm open to contributions. I'm interested in your opinion: how do you see the |
Hey @defval, At first glance, I like the From an implementation standpoint, it seems obviously a bit more complex to implement There are just my initial thoughts. |
Sorry for the multiple questions. I did search through the previous issues, but could not find what I was looking for.
I'm trying to have a function that would return a variable number of dependencies to add to the DI container, but where the function also received some dependencies from previously declared
di.Provide
. I saw this answer which seems to hint that it's possible for adi.Provide
to returndi.Option
, but I cannot make it work.Here is an example:
The text was updated successfully, but these errors were encountered: