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

internal/wire: add method providers support #330

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 49 additions & 5 deletions internal/wire/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ type objectCache struct {

type objRef struct {
importPath string
recvType string
name string
}

Expand Down Expand Up @@ -493,6 +494,10 @@ func (oc *objectCache) get(obj types.Object) (val interface{}, errs []error) {
pkgPath := obj.Pkg().Path()
return oc.processExpr(oc.packages[pkgPath].TypesInfo, pkgPath, spec.Values[i], obj.Name())
case *types.Func:
sig := obj.Type().(*types.Signature)
if recv := sig.Recv(); recv != nil {
ref.recvType = recv.Type().String()
}
return processFuncProvider(oc.fset, obj)
default:
return nil, []error{fmt.Errorf("%v is not a provider or a provider set", obj)}
Expand Down Expand Up @@ -659,14 +664,42 @@ func qualifiedIdentObject(info *types.Info, expr ast.Expr) types.Object {
case *ast.Ident:
return info.ObjectOf(expr)
case *ast.SelectorExpr:
pkgName, ok := expr.X.(*ast.Ident)
if !ok {
return nil
x := astutil.Unparen(expr.X)
if star, ok := x.(*ast.StarExpr); ok {
x = star.X
}
if _, ok := info.ObjectOf(pkgName).(*types.PkgName); !ok {
switch x := x.(type) {
case *ast.Ident:
switch obj := info.ObjectOf(x); obj.(type) {
case *types.PkgName:
case *types.TypeName:
named, ok := obj.Type().(*types.Named)
if !ok {
return nil
}

t := named.Underlying()
if ptr, ok := t.(*types.Pointer); ok {
t = ptr.Elem()
}
default:
return nil
}

return info.ObjectOf(expr.Sel)
case *ast.SelectorExpr:
pkgName, ok := x.X.(*ast.Ident)
if !ok {
return nil
}
if _, ok := info.ObjectOf(pkgName).(*types.PkgName); !ok {
return nil
}
return info.ObjectOf(expr.Sel)
default:
return nil
}
return info.ObjectOf(expr.Sel)

default:
return nil
}
Expand All @@ -680,7 +713,17 @@ func processFuncProvider(fset *token.FileSet, fn *types.Func) (*Provider, []erro
if err != nil {
return nil, []error{notePosition(fset.Position(fpos), fmt.Errorf("wrong signature for provider %s: %v", fn.Name(), err))}
}

params := sig.Params()
if recv := sig.Recv(); recv != nil {
newParams := make([]*types.Var, params.Len()+1)
newParams[0] = recv
for i := 0; i < params.Len(); i++ {
newParams[i+1] = params.At(i)
}
params = types.NewTuple(newParams...)
}

provider := &Provider{
Pkg: fn.Pkg(),
Name: fn.Name(),
Expand All @@ -691,6 +734,7 @@ func processFuncProvider(fset *token.FileSet, fn *types.Func) (*Provider, []erro
HasCleanup: providerSig.cleanup,
HasErr: providerSig.err,
}

for i := 0; i < params.Len(); i++ {
provider.Args[i] = ProviderInput{
Type: params.At(i).Type(),
Expand Down
74 changes: 74 additions & 0 deletions internal/wire/testdata/MethodProvider/foo/foo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright 2018 The Wire Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"fmt"

"github.com/google/wire"
)

func main() {
fmt.Println(injectFooBar())
}

type (
Foo int
FooBar int
Baz int
)

var Set = wire.NewSet(
newFooProvider,
fooProvider.provideFoo,
newFooBarProvider,
(*fooBarProvider).provideFooBar,
newBazProvider,
bazProvider.provideBaz,
)

type fooProvider struct{}

func newFooProvider() fooProvider {
return fooProvider{}
}

func (f fooProvider) provideFoo() Foo {
return 40
}

type fooBarProvider struct{}

func newFooBarProvider() *fooBarProvider {
return &fooBarProvider{}
}

func (*fooBarProvider) provideFooBar(foo Foo) FooBar {
return FooBar(foo) + 1
}

type bazProvider interface {
provideBaz(f FooBar) Baz
}

func newBazProvider() bazProvider {
return bazProviderImpl{}
}

type bazProviderImpl struct{}

func (bazProviderImpl) provideBaz(fooBar FooBar) Baz {
return Baz(fooBar) + 1
}
27 changes: 27 additions & 0 deletions internal/wire/testdata/MethodProvider/foo/wire.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2018 The Wire Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build wireinject
// +build wireinject

package main

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

func injectFooBar() Baz {
wire.Build(Set)
return 0
}
1 change: 1 addition & 0 deletions internal/wire/testdata/MethodProvider/pkg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
example.com/foo
1 change: 1 addition & 0 deletions internal/wire/testdata/MethodProvider/want/program_out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
42
19 changes: 19 additions & 0 deletions internal/wire/testdata/MethodProvider/want/wire_gen.go

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