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

Injecting the subpackage into the parent package results in the error "no provider found for invalid type". #396

Open
OutUniverse opened this issue Nov 1, 2023 · 2 comments

Comments

@OutUniverse
Copy link

The a.go could not injector the b.go interface, but the d.go is ok.

The directory struct like this:

services
- a.go
- c.go
inpack
- b.go
testinpack
- d.go

Here is the code:
a.go:

package services

import (
"github.com/google/wire"
"test.com/services/inpack"
)

var TestAServiceSet = wire.NewSet(wire.Struct(new(TestAServiceDI), "*"), NewTestAService, NewITestAService)

type TestAServiceDI struct {
bservice inpack.ITestBService
}

type TestAService struct {
serviceDI TestAServiceDI
}

func NewTestAService(serviceDI TestAServiceDI) *TestAService {
return &TestAService{serviceDI}
}

type ITestAService interface {
}

func NewITestAService(service *TestAService) ITestAService {
return service
}

c.go:

package services

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

var TestCServiceSet = wire.NewSet(wire.Struct(new(TestCServiceDI), "*"), NewTestCService, NewITestCService)

type TestCServiceDI struct {
}

type TestCService struct {
serviceDI TestCServiceDI
}

func NewTestCService(serviceDI TestCServiceDI) *TestCService {
return &TestCService{serviceDI}
}

type ITestCService interface {
}

func NewITestCService(service *TestCService) ITestCService {
return service
}

b.go:

package inpack

import (
"test.com/services"

"github.com/google/wire"
)

var TestBServiceSet = wire.NewSet(wire.Struct(new(TestBServiceDI), "*"), NewTestBService, NewITestBService)

type TestBServiceDI struct {
c services.ITestCService
}

type TestBService struct {
serviceDI TestBServiceDI
}

func NewTestBService(serviceDI TestBServiceDI) *TestBService {
return &TestBService{serviceDI}
}

type ITestBService interface {
}

func NewITestBService(service *TestBService) ITestBService {
return service
}

d.go:

package testinpack

import (
"github.com/google/wire"
"test.com/services/inpack"
)

var TestDServiceSet = wire.NewSet(wire.Struct(new(TestDServiceDI), "*"), NewTestDService, NewITestDService)

type TestDServiceDI struct {
bservice inpack.ITestBService
}

type TestDService struct {
serviceDI TestDServiceDI
}

func NewTestDService(serviceDI TestDServiceDI) *TestDService {
return &TestDService{serviceDI}
}

type ITestDService interface {
}

func NewITestDService(service *TestDService) ITestDService {
return service
}

@OutUniverse
Copy link
Author

Here is the error:
error

@ProgrammingMuffin
Copy link

Have you tried passing NewITestBService provider to TestAServiceSet? It cannot find this specific provider. Here is an example I tried which works:

inner/inner.go:

package inner

type TextValue struct {
	Value string
}

main.go:

//go:build wireinject
// +build wireinject

package main

import (
	"fmt"
	"wiretest/inner"

	"github.com/google/wire"
)

type Message struct {
	Text inner.TextValue
}

func ProvideString() inner.TextValue {
	return inner.TextValue{Value: "hi"}
}

func InitializeEvent() Message {
	wire.Build(wire.NewSet(wire.Struct(new(Message), "Text"), ProvideString))
	return Message{}
}

func main() {
	m := InitializeEvent()
	fmt.Println("message is: ", m.Text.Value)
}

Please correct me if I am not understanding the issue correctly.

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