Skip to content
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

Multiple bindings for string #206

Closed
J7mbo opened this issue Aug 6, 2019 · 3 comments
Closed

Multiple bindings for string #206

J7mbo opened this issue Aug 6, 2019 · 3 comments

Comments

@J7mbo
Copy link

J7mbo commented Aug 6, 2019

Hi, I expect this to be a simple problem to solve.

I have two structs, one Controller and one Dependency, and a factory for each struct. Each factory function takes a single string as a parameter. I'm trying to initialise the first struct with the second struct as a dependency, both of which require a string.

package main

type Controller struct {
	controllerString string
}
func NewController(controllerString string) Controller {
	return Controller{controllerString: controllerString}
}

type Dependency struct {
	dependencyString string
}
func NewDependency(dependencyString string) Dependency {
	return Dependency{dependencyString: dependencyString}
}

func main() {
	initialiseController("str1", "str2")
}

Here's my wire.go.

func initialiseController(dependencyString string, controllerString string) Controller {
	wire.Build(NewDependency, NewController)
	return Controller{}
}

I am new to this library and I don't know how wire is working under the hood. Wouldn't Wire see two strings, reflect on NewDependency, inject the first one, and then the second with NewController?

What do I need to do to achieve what I require?

@Hartimer
Copy link

Hartimer commented Aug 6, 2019

Wire does not support multiple bindings of the same type, in this case string.

You'll have to create a type alias for at least one of those strings in order to disambiguate.

@vangent
Copy link
Contributor

vangent commented Aug 6, 2019

@vangent vangent closed this as completed Aug 6, 2019
@J7mbo
Copy link
Author

J7mbo commented Aug 6, 2019

Cheers guys!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants