Skip to content

Commit a86b32f

Browse files
committed
feat: support method provider
Change-Id: I04341c6de67ab2f75bf5da0957ac502f4f5b9ae8
1 parent d07cde0 commit a86b32f

File tree

6 files changed

+137
-7
lines changed

6 files changed

+137
-7
lines changed

cmd/wire/main_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"flag"
6+
"os"
7+
"path/filepath"
8+
"runtime"
9+
"testing"
10+
)
11+
12+
func TestMethodProvider(t *testing.T) {
13+
/*
14+
// Support type's method as provider, for example:
15+
16+
func InitDog() *animals.Dog {
17+
panic(wire.Build(
18+
animals.NewAnimals,
19+
(*animals.Animals).NewDog, // pointer receiver method
20+
))
21+
}
22+
23+
func InitCat() *animals.Cat {
24+
panic(wire.Build(
25+
wire.Value(animals.Animals{}),
26+
animals.Animals.NewCat, // struct receiver method
27+
))
28+
}
29+
*/
30+
_, b, _, _ := runtime.Caller(0)
31+
wireRepoPath := filepath.Dir(filepath.Dir(filepath.Dir(b)))
32+
_ = os.Chdir(filepath.Join(wireRepoPath, "tests", "method_provider"))
33+
cmd := &genCmd{}
34+
code := int(cmd.Execute(context.Background(), flag.CommandLine))
35+
if code != 0 {
36+
t.Fatal(code)
37+
}
38+
}

internal/wire/parse.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -659,14 +659,21 @@ func qualifiedIdentObject(info *types.Info, expr ast.Expr) types.Object {
659659
case *ast.Ident:
660660
return info.ObjectOf(expr)
661661
case *ast.SelectorExpr:
662-
pkgName, ok := expr.X.(*ast.Ident)
663-
if !ok {
664-
return nil
665-
}
666-
if _, ok := info.ObjectOf(pkgName).(*types.PkgName); !ok {
667-
return nil
662+
switch value := expr.X.(type) {
663+
case *ast.ParenExpr, *ast.SelectorExpr:
664+
if selection, ok := info.Selections[expr]; !ok {
665+
return nil
666+
} else {
667+
return selection.Obj()
668+
}
669+
case *ast.Ident:
670+
pkgName := value
671+
if _, ok := info.ObjectOf(pkgName).(*types.PkgName); !ok {
672+
return nil
673+
}
674+
return info.ObjectOf(expr.Sel)
668675
}
669-
return info.ObjectOf(expr.Sel)
676+
return nil
670677
default:
671678
return nil
672679
}
@@ -701,6 +708,17 @@ func processFuncProvider(fset *token.FileSet, fn *types.Func) (*Provider, []erro
701708
}
702709
}
703710
}
711+
if recv := sig.Recv(); recv != nil {
712+
switch typ := recv.Type().(type) {
713+
case *types.Pointer:
714+
elem := typ.Elem().(*types.Named)
715+
provider.Name = fmt.Sprintf("(*%s).%s", elem.Obj().Name(), fn.Name())
716+
provider.Args = append([]ProviderInput{{Type: typ}}, provider.Args...)
717+
case *types.Named:
718+
provider.Name = fmt.Sprintf("(%s).%s", typ.Obj().Name(), fn.Name())
719+
provider.Args = append([]ProviderInput{{Type: typ}}, provider.Args...)
720+
}
721+
}
704722
return provider, nil
705723
}
706724

internal/wire/wire.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,11 @@ func (g *gen) qualifiedID(pkgName, pkgPath, sym string) string {
523523
if name == "" {
524524
return sym
525525
}
526+
if strings.HasPrefix(sym, "(*") {
527+
return sym[:2] + name + "." + sym[2:]
528+
} else if strings.HasPrefix(sym, "(") {
529+
return sym[:1] + name + "." + sym[1:]
530+
}
526531
return name + "." + sym
527532
}
528533

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package animals
2+
3+
type Dog struct{}
4+
type Cat struct{}
5+
6+
type Animals struct{}
7+
8+
func (f *Animals) NewDog() *Dog {
9+
return &Dog{}
10+
}
11+
12+
func (Animals) NewCat() *Cat {
13+
return &Cat{}
14+
}
15+
16+
func NewAnimals() *Animals {
17+
return &Animals{}
18+
}

tests/method_provider/wire.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//+build wireinject
2+
//go:generate wire
3+
4+
package method_provider
5+
6+
import (
7+
"github.com/google/wire"
8+
"github.com/google/wire/tests/method_provider/animals"
9+
)
10+
11+
func InitDog() *animals.Dog {
12+
panic(wire.Build(
13+
animals.NewAnimals,
14+
(*animals.Animals).NewDog,
15+
))
16+
}
17+
18+
func InitCat() *animals.Cat {
19+
panic(wire.Build(
20+
wire.Value(animals.Animals{}),
21+
animals.Animals.NewCat,
22+
))
23+
}

tests/method_provider/wire_gen.go

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)