google Wire - how to have multiple object returns from Initialize method in wire.go

773 views
Skip to first unread message

alexs...@gmail.com

unread,
Jun 30, 2020, 3:50:58 PM6/30/20
to go-cloud
The code in :

wire-0.4.0/internal/wire/parse.go:731 gives the error message "second return type is %s; must be func()"

if there are >3 returns then this fails with "too many return values".



I want to initialise multiple services from this call - why is this problematic?

(I also have some legacy code that works by side-effect and have had to create a fake object just to allow a no-return provider to be called - would be good if wire could be more flexible and give a warning for bad practice rather than a hard failure here)

wire.go:
func Initialise() (thingA, thingB, thingC, thingD error) {

wire.Build(thingAProvider, thingBProvider, thingCProvider, thingDProvider)

return thingA{}, thingB{}, thingC{}, thingD{}, nill
}

gives error:

"too many return values"

Thinking on this there is a workaround - will test tomorrow  - creating many Initialise() methods - but now my code has to call lots of generated methods rather than a single call - also if we have to create one-for-one unless the dependency graph is deep this what's the gain over manual implementation? I'm using wire to eliminate boilerplate.


Only been using this for 1 day so this could all be wrong.

Thanks for taking a look at this,

Alex

Robert van Gent

unread,
Jun 30, 2020, 4:06:03 PM6/30/20
to go-cloud
This behavior is by design. If you want to initialize multiple services in a provider function, create a struct with fields for the services and have the provider function return that.

alexs...@gmail.com

unread,
Jul 1, 2020, 1:16:29 PM7/1/20
to go-cloud
Thanks for the reply - will do.
Reply all
Reply to author
Forward
0 new messages