Skip to content

Commit

Permalink
Allow generic named types to be directly used as a provider (#22)
Browse files Browse the repository at this point in the history
* Allow generic named types to be directly used as a provider

* Exclude testdata/ from coverage check

---------

Co-authored-by: Chris Brand <[email protected]>
  • Loading branch information
arcturial and Chris Brand authored Oct 14, 2024
1 parent 1fbc32c commit 4a41c43
Show file tree
Hide file tree
Showing 18 changed files with 112 additions and 32 deletions.
9 changes: 8 additions & 1 deletion internal/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,6 @@ func makeTypeRef(pkgCache *PkgCache, typ types.Type) (string, error) {
}

res := typ.String()

for _, pkg := range pkgs {
if pkg.Path() == pkgCache.In {
res = strings.ReplaceAll(res, pkg.Path()+".", "")
Expand Down Expand Up @@ -554,6 +553,14 @@ func getTypePkgs(tl ...types.Type) ([]*types.Package, error) {
if pkg != nil {
pl = []*types.Package{pkg}
}

for i := 0; i < t.TypeArgs().Len(); i++ {
generics, err := getTypePkgs(t.TypeArgs().At(i))
if err != nil {
return nil, err
}
pl = append(pl, generics...)
}
case *types.Map:
pl, err = getTypePkgs(t.Elem(), t.Key())
case *types.Signature:
Expand Down
3 changes: 2 additions & 1 deletion internal/testdata/dev_exchange_graph.golden
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Set[10]: (inline)
Set[11]: (inline)
Set[1]: var example/exchange/state.ChanProvider
Func[1]: func example/exchange/state.NewModelChan() chan<- example/exchange.Model
Set[9]: var example/backends/providers.WeldDev
Expand All @@ -14,3 +14,4 @@ Set[10]: (inline)
Func[1]: func example/external/mail.New(opts ...example/external/mail.option) (*example/external/mail.Mailer, error)
Func[1]: func example/external/mail/mail.New() (*example/external/mail/mail.MailerLegacy, error)
Func[1]: func example/external/versioned.New() *example/external/versioned/v1.Service
Func[1]: func example/exchange/state.NewGenericStringType() example/exchange/ops.TestFunc[example/exchange.Model, string]
1 change: 1 addition & 0 deletions internal/testdata/dev_exchange_selected.golden
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Func[1]: func example/exchange/db.Connect() (*example/exchange/db.ExchangeDB, error)
Func[1]: func example/external/versioned.New() *example/external/versioned/v1.Service
Func[1]: func example/exchange/state.NewModelChan() chan<- example/exchange.Model
Func[1]: func example/exchange/state.NewGenericStringType() example/exchange/ops.TestFunc[example/exchange.Model, string]
Func[1]: func example/identity/email/client/dev.Make(b example/identity/email/client/logical.Backends) (example/identity/email.Client, error)
Func[1]: func example/identity/users/client/dev.Make(b example/identity/users/client/logical.Backends) (example/identity/users.Client, error)
Func[1]: func example/identity/email/db.Connect() (*example/identity/email/db.EmailDB, error)
Expand Down
2 changes: 1 addition & 1 deletion internal/testdata/dev_exchange_specBack.golden
Original file line number Diff line number Diff line change
@@ -1 +1 @@
example/exchange/ops.Backends[5]: *example/exchange/db.ExchangeDB, *example/external/versioned/v1.Service, chan<- example/exchange.Model, example/identity/email.Client, example/identity/users.Client
example/exchange/ops.Backends[6]: *example/exchange/db.ExchangeDB, *example/external/versioned/v1.Service, chan<- example/exchange.Model, example/exchange/ops.TestFunc[example/exchange.Model, string], example/identity/email.Client, example/identity/users.Client
10 changes: 10 additions & 0 deletions internal/testdata/dev_exchange_tpldata.golden
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ deps:
returnserr: true
params: []
errwrapmsg: exchange db connect
- type: exchange_ops.TestFunc[exchange.Model, string]
var: genericStringFunc
getter: GenericStringFunc
isduplicate: false
provider:
funcpkg: exchange_state
funcname: NewGenericStringType
returnserr: false
params: []
errwrapmsg: exchange state new generic string type
- type: chan<- exchange.Model
var: modelChan
getter: ModelChan
Expand Down
21 changes: 14 additions & 7 deletions internal/testdata/dev_exchange_weldoutput.golden
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ func MakeBackends() (exchange_ops.Backends, error) {
return nil, errors.Wrap(err, "exchange db connect")
}

b.genericStringFunc = exchange_state.NewGenericStringType()

b.modelChan = exchange_state.NewModelChan()

b.users, err = users_client_dev.Make(&b)
Expand All @@ -60,13 +62,14 @@ func MakeBackends() (exchange_ops.Backends, error) {
}

type backendsImpl struct {
email email.Client
emailDB *email_db.EmailDB
exchangeDB *exchange_db.ExchangeDB
modelChan chan<- exchange.Model
users users.Client
usersDB *users_db.UsersDB
versioned *versioned_v1.Service
email email.Client
emailDB *email_db.EmailDB
exchangeDB *exchange_db.ExchangeDB
genericStringFunc exchange_ops.TestFunc[exchange.Model, string]
modelChan chan<- exchange.Model
users users.Client
usersDB *users_db.UsersDB
versioned *versioned_v1.Service
}

func (b *backendsImpl) Email() email.Client {
Expand All @@ -81,6 +84,10 @@ func (b *backendsImpl) ExchangeDB() *exchange_db.ExchangeDB {
return b.exchangeDB
}

func (b *backendsImpl) GenericStringFunc() exchange_ops.TestFunc[exchange.Model, string] {
return b.genericStringFunc
}

func (b *backendsImpl) ModelChan() chan<- exchange.Model {
return b.modelChan
}
Expand Down
5 changes: 5 additions & 0 deletions internal/testdata/example/exchange/ops/backends.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ import (
"example/identity/users"
)

type TestFunc[T any, C any] func(a T, c C) string

type GenericStringType = TestFunc[exchange.Model, string]

type Backends interface {
GenericStringFunc() GenericStringType
ExchangeDB() *db.ExchangeDB
Email() email.Client
Users() users.Client
Expand Down
8 changes: 8 additions & 0 deletions internal/testdata/example/exchange/state/adhoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package state

import (
"example/exchange"
"fmt"

exchange_ops "example/exchange/ops"
"github.com/luno/weld"
)

Expand All @@ -11,3 +13,9 @@ var ChanProvider = weld.NewSet(NewModelChan)
func NewModelChan() chan<- exchange.Model {
return make(chan<- exchange.Model)
}

func NewGenericStringType() exchange_ops.GenericStringType {
return func(a exchange.Model, b string) string {
return fmt.Sprintf("%s%s", a, b)
}
}
6 changes: 5 additions & 1 deletion internal/testdata/example/exchange/state/devstate/weld.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import (
//go:generate weld

var _ = weld.NewSpec(
weld.NewSet(state.ChanProvider, providers.WeldDev),
weld.NewSet(
state.ChanProvider,
providers.WeldDev,
state.NewGenericStringType,
),
weld.Existing(new(exchange_ops.Backends)),
)
21 changes: 14 additions & 7 deletions internal/testdata/example/exchange/state/devstate/weld_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion internal/testdata/example/exchange/state/weld.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import (
// Note that github.com/luno/weld/internal/gen_test.go generates this specific state_gen.go as well.

var _ = weld.NewSpec(
weld.NewSet(ChanProvider, providers.WeldProd),
weld.NewSet(
ChanProvider,
providers.WeldProd,
NewGenericStringType,
),
weld.Existing(new(exchange_ops.Backends)),
)
17 changes: 12 additions & 5 deletions internal/testdata/example/exchange/state/weld_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion internal/testdata/exchange_graph.golden
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Set[13]: (inline)
Set[14]: (inline)
Set[1]: var example/exchange/state.ChanProvider
Func[1]: func example/exchange/state.NewModelChan() chan<- example/exchange.Model
Set[12]: var example/backends/providers.WeldProd
Expand All @@ -20,3 +20,4 @@ Set[13]: (inline)
Func[1]: func example/external/mail.New(opts ...example/external/mail.option) (*example/external/mail.Mailer, error)
Func[1]: func example/external/mail/mail.New() (*example/external/mail/mail.MailerLegacy, error)
Func[1]: func example/external/versioned.New() *example/external/versioned/v1.Service
Func[1]: func example/exchange/state.NewGenericStringType() example/exchange/ops.TestFunc[example/exchange.Model, string]
1 change: 1 addition & 0 deletions internal/testdata/exchange_selected.golden
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Func[1]: func example/exchange/db.Connect() (*example/exchange/db.ExchangeDB, error)
Func[1]: func example/external/versioned.New() *example/external/versioned/v1.Service
Func[1]: func example/exchange/state.NewModelChan() chan<- example/exchange.Model
Func[1]: func example/exchange/state.NewGenericStringType() example/exchange/ops.TestFunc[example/exchange.Model, string]
Bind[1]: example/identity/email.Client(*example/identity/email/client/grpc.client)
Bind[1]: example/identity/users.Client(*example/identity/users/client/grpc.client)
Func[1]: func example/identity/email/client/grpc.New() (*example/identity/email/client/grpc.client, error)
Expand Down
2 changes: 1 addition & 1 deletion internal/testdata/exchange_specBack.golden
Original file line number Diff line number Diff line change
@@ -1 +1 @@
example/exchange/ops.Backends[5]: *example/exchange/db.ExchangeDB, *example/external/versioned/v1.Service, chan<- example/exchange.Model, example/identity/email.Client, example/identity/users.Client
example/exchange/ops.Backends[6]: *example/exchange/db.ExchangeDB, *example/external/versioned/v1.Service, chan<- example/exchange.Model, example/exchange/ops.TestFunc[example/exchange.Model, string], example/identity/email.Client, example/identity/users.Client
10 changes: 10 additions & 0 deletions internal/testdata/exchange_tpldata.golden
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ deps:
returnserr: true
params: []
errwrapmsg: exchange db connect
- type: exchange_ops.TestFunc[exchange.Model, string]
var: genericStringFunc
getter: GenericStringFunc
isduplicate: false
provider:
funcpkg: ""
funcname: NewGenericStringType
returnserr: false
params: []
errwrapmsg: exchange state new generic string type
- type: chan<- exchange.Model
var: modelChan
getter: ModelChan
Expand Down
17 changes: 12 additions & 5 deletions internal/testdata/exchange_weldoutput.golden
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ func MakeBackends() (exchange_ops.Backends, error) {
return nil, errors.Wrap(err, "exchange db connect")
}

b.genericStringFunc = NewGenericStringType()

b.modelChan = NewModelChan()

b.users, err = users_client_grpc.New()
Expand All @@ -47,11 +49,12 @@ func MakeBackends() (exchange_ops.Backends, error) {
}

type backendsImpl struct {
email email.Client
exchangeDB *exchange_db.ExchangeDB
modelChan chan<- exchange.Model
users users.Client
versioned *versioned_v1.Service
email email.Client
exchangeDB *exchange_db.ExchangeDB
genericStringFunc exchange_ops.TestFunc[exchange.Model, string]
modelChan chan<- exchange.Model
users users.Client
versioned *versioned_v1.Service
}

func (b *backendsImpl) Email() email.Client {
Expand All @@ -62,6 +65,10 @@ func (b *backendsImpl) ExchangeDB() *exchange_db.ExchangeDB {
return b.exchangeDB
}

func (b *backendsImpl) GenericStringFunc() exchange_ops.TestFunc[exchange.Model, string] {
return b.genericStringFunc
}

func (b *backendsImpl) ModelChan() chan<- exchange.Model {
return b.modelChan
}
Expand Down
2 changes: 1 addition & 1 deletion sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ sonar.projectName = weld
sonar.links.scm = https://github.com/luno/weld

sonar.sources = .
sonar.exclusions=**/*_test.go, **/*pb.go
sonar.exclusions=**/*_test.go, **/*pb.go, internal/testdata/**
sonar.go.coverage.reportPaths = coverage.out
sonar.go.tests.reportPaths = sonar-report.json
sonar.tests = .
Expand Down

0 comments on commit 4a41c43

Please sign in to comment.