wire icon indicating copy to clipboard operation
wire copied to clipboard

Calling wire.Build with a provider set from a structure variable (For modular buildings)

Open diegoortizmatajira opened this issue 3 years ago • 0 comments

Describe the bug

I am trying to build a module system to allow some packages to provide their ProviderSets, along with other package settings. This means having a structure to be filled by each package, then after processing and composing modules, the idea is to have a global module with an aggregation of the required Wire Providers, Bindings, etc.

In the end, instead of passing a single variable with the provider set, we pass a member from a structure variable to the wire.Build call.

Example:

wire.Build(module.ProviderSet)

instead of

wire.Build(providerSet)

But instead of getting the code generated as expected, I got

internal/wiring/wire.go|32 col 19| unknown pattern
internal/wiring/wire_gen.go|3| running "go": exit status 1

To Reproduce

The following code reproduces the issue

//go:build wireinject

package wiring

import (
	"github.com/google/wire"
)

// ClockProvider is a test struct
type ClockProvider struct{}

// This is a Provider function / Constuctor for ClockProvider
func NewProvider() *ClockProvider {
	return &ClockProvider{}
}

// This is a mock of a Module Structure that can
// hold a module settings, providers, default values, etc.
type Module struct {
	Set           wire.ProviderSet
	DefaultValues []string
}

// This is a sample module instance
var myModule = Module{
	Set:           wire.NewSet(NewProvider),
	DefaultValues: []string{"Value 1", "Value 2"},
}

// To be generated
func GetClockProvider() (*ClockProvider, error) {
	panic(wire.Build(myModule.Set))
}

Expected behavior

The generated code should be the same as the one providing a single variable.

Version

  • github.com/google/wire v0.5.0
  • go version go1.19.2 linux/amd64

diegoortizmatajira avatar Oct 25 '22 00:10 diegoortizmatajira