Skip to content

Consider a mechanism to permit automatically binding a type to interfaces it implements #242

Open

Description

Is your feature request related to a problem? Please describe.

If I am wiring a complex application which uses interfaces to describe dependencies, it is cumbersome to have to provide wire.Bind to describe mappings from interface to implementation even when the method names are clearly designed to be unambiguous.

If I have a LoginController which needs a TryLogin(u, p string) error method, there is only going to be one type in the system which implements such a call and it will be available via some function in wire.Build(...). I don't see why we need a wire.Bind to indicate what implements that TryLogin method when there is only one.

Describe the solution you'd like

For interfaces and implementations that are unambiguous (there is only one concrete type from the provider list that matches the interface being resolved), a wire.Bind should not be needed. I understand the use of wire.Bind to clarify ambiguous matches but requiring it in all cases adds tedium for no benefit.

Describe alternatives you've considered

I don't see any alternative other than writing out wire.Bind for each mapping, which is the tedium I'm trying to avoid.

Additional context

The Go language itself resolves interfaces based solely on method signatures, not an explicit implements mechanism - I don't see why wire wouldn't follow the same behavior. wire.Bind can still be used to resolve ambiguous cases.

Example

Basically, I feel like this example should just work:

type LoginStore interface {
	TryLogin(u, p string) error
}

type LoginCtrl struct {
	LoginStore LoginStore
}

func NewLoginCtrl(store LoginStore) *LoginCtrl { return &LoginCtrl{LoginStore: store} }

type UserDataStore struct{}

// TryLogin makes UserDataStore implement LoginStore
func (uds *UserDataStore) TryLogin(u, p string) error { panic("TODO") }

func NewUserDataStore() *UserDataStore { return &UserDataStore{} }

// ... {
	wire.Build(NewUserDataStore, NewLoginCtrl)
// }

If there is agreement on the feature I can dig into the code and start seeing what it would take to put together a PR.

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestneeds infoFurther discussion or clarification is necessary

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions