Adding capability to auto-bind a type to interfaces it implements #285
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Implements #242.
I think motivation for this was covered well by @bradleypeabody in the linked issue, so I won't go into detail here, but I'll copy over a statement I liked:
Summary
Public API
wire.AutoBind()
and correspondingAutoBinding
type.Internals
AutoBinding
type to theinternal/wire
package that records a concrete type and position of its token.AutoBinding
fields added to theProviderSet
,providerSetSrc
, andProviderType
structs.AutoBinding
s.processAutoBind()
to handle the parsing(*ProviderSet).For()
.This makes it easy to give explicit
wire.Bind()
s precedence. If a type is not found in theproviderMap
, and it is an interface, the []AutoBindings list is checked for a type that implements the interface. If one is found, it is added to theproviderMap
andsrcMap
to avoid future searches for the same type. If no such type is found, behavior is as before.Tests
InterfaceAutoBinding
showing automated binding ofBar -> Fooer
.InterfaceExplicitBindingPreferredOverAutoBinding
, which is fairly self-descriptive.I've tried out my fork on a project that was already using wire, and so far it is working rather nicely. It has made the
wire.Build()
s a bit nicer I think - especially for cases where one concrete type implements 2 or more interfaces.Coupling is reduced too, since the implementation and the interface don't need to directly know about each other. Usage seems more "Go-ish" from my (obviously a bit biased) point of view.
Any feedback/comments/concerns is appreciated! This was my first time reading through wire's code (though I've used it a lot), so I'll be happy to make any changes necessary if there are concerns around the structure of the implementation.