-
Notifications
You must be signed in to change notification settings - Fork 207
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
Add API to invoke named values #181
Comments
Has this been implemented? Are there any docs on how to use this? It would be good to be able to use the named invocation without the use of param structs. |
It was merged in #233, but 1.8.0 has not been released |
cannot use dig.Name("myExporter1") (type dig.ProvideOption) as type dig.InvokeOption in argument to container.Invoke: |
The types There is no corresponding inlined Invoke option, meaning you have to use The rationale here is that Whenever you're writing Invoke code, the thought here is that you are writing something new and not attempting to use an existing signature or a function (mostly since returns are completely discarded). To put what I just said in code, lets assume you have two clashing constructors (in this example returning a package main
import (
"fmt"
"go.uber.org/dig"
)
func username() string {
return "foo"
}
func password() string {
return "bar"
}
func main() {
c := dig.New()
c.Provide(username, dig.Name("username"))
c.Provide(password, dig.Name("password"))
err := c.Invoke(func(p struct {
dig.In
U string `name:"username"`
P string `name:"password"`
}) {
fmt.Println("user >>>", p.U)
fmt.Println("pwd >>>", p.P)
})
if err != nil {
panic(err)
}
} If you have ideas of a shorthand Perhaps |
I think dig.Name should be both an Invoke and Provide option, I had a look at the code but it's too much for me to change right now. Doing a tech test. Decided to use https://github.com/sarulabs/di instead, they are doing named invocation nicely. |
addesses uber-go#181
I'd like to do something like these
The text was updated successfully, but these errors were encountered: