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

wire.NewSet support generics #381

Open
OldSmokeGun opened this issue Mar 14, 2023 · 1 comment
Open

wire.NewSet support generics #381

OldSmokeGun opened this issue Mar 14, 2023 · 1 comment

Comments

@OldSmokeGun
Copy link

Describe the bug

Wire doesn't support generics when I pass a generic function in wire.NewSet

To Reproduce

Pass generic function in wire.NewSet, like this:

wire.NewSet(
	db.New[config.DBConfigDefault, config.DBInstanceDefault],
	ent.New,
	redis.New,
	discovery.New,
	casbin.New,
	grpc.New,
	uid.New,
)

or

var ProviderSet = wire.NewSet(
	GetApp,
	GetHTTP,
	GetGRPC,
	GetDB[DBKeyDefault, DBConfigDefault],
	GetRedis,
	GetTrace,
	GetDiscovery,
	GetServices,
	GetJWT,
	GetCasbin,
)

Then, wire reports an error like this:

$ wire
wire: /example/internal/config/config.go:16:2: unknown pattern
wire: /example/internal/app/app.go:27:3: unknown pattern
wire: /example/internal/config/config.go:16:2: unknown pattern
wire: /example/internal/app/app.go:27:3: unknown pattern
wire: /example/internal/app/app.go:27:3: unknown pattern
wire: /example/internal/command: generate failed
wire: at least one generate failure

Expected behavior

can generate code correctly

Version

github.com/google/wire v0.5.1-0.20220620021424-0675cdc9191c

Additional context

Here is my code:

func GetDB[K DBKeyGetter, V DBConfigGroup]() (V, error) {
	c, err := getEntry(config.DB)
	if err != nil {
		return V{}, err
	}
	var k K
	return V(c[k.GetName()]), nil
}
func New[C config.DBConfigGroup, D config.DBInstanceGroup](conf C, logger *slog.Logger) (db D, cleanup func(), err error) {
        // code
}
@Hamper
Copy link

Hamper commented Apr 3, 2024

You can use workaround like this

wire.NewSet(
	newDb,
	ent.New,
	redis.New,
	discovery.New,
	casbin.New,
	grpc.New,
	uid.New,
)

func newDb(conf config.DBConfigDefault, logger *slog.Logger) (db config.DBInstanceDefault, cleanup func(), err error) {
	return db.New[config.DBConfigDefault, config.DBInstanceDefault]
}

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

2 participants