Skip to content

Commit

Permalink
feat: support method provider
Browse files Browse the repository at this point in the history
Change-Id: I04341c6de67ab2f75bf5da0957ac502f4f5b9ae8
  • Loading branch information
sin5th committed May 31, 2021
1 parent d07cde0 commit a86b32f
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 7 deletions.
38 changes: 38 additions & 0 deletions cmd/wire/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"context"
"flag"
"os"
"path/filepath"
"runtime"
"testing"
)

func TestMethodProvider(t *testing.T) {
/*
// Support type's method as provider, for example:
func InitDog() *animals.Dog {
panic(wire.Build(
animals.NewAnimals,
(*animals.Animals).NewDog, // pointer receiver method
))
}
func InitCat() *animals.Cat {
panic(wire.Build(
wire.Value(animals.Animals{}),
animals.Animals.NewCat, // struct receiver method
))
}
*/
_, b, _, _ := runtime.Caller(0)
wireRepoPath := filepath.Dir(filepath.Dir(filepath.Dir(b)))
_ = os.Chdir(filepath.Join(wireRepoPath, "tests", "method_provider"))
cmd := &genCmd{}
code := int(cmd.Execute(context.Background(), flag.CommandLine))
if code != 0 {
t.Fatal(code)
}
}
32 changes: 25 additions & 7 deletions internal/wire/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,14 +659,21 @@ 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
}
if _, ok := info.ObjectOf(pkgName).(*types.PkgName); !ok {
return nil
switch value := expr.X.(type) {
case *ast.ParenExpr, *ast.SelectorExpr:
if selection, ok := info.Selections[expr]; !ok {
return nil
} else {
return selection.Obj()
}
case *ast.Ident:
pkgName := value
if _, ok := info.ObjectOf(pkgName).(*types.PkgName); !ok {
return nil
}
return info.ObjectOf(expr.Sel)
}
return info.ObjectOf(expr.Sel)
return nil
default:
return nil
}
Expand Down Expand Up @@ -701,6 +708,17 @@ func processFuncProvider(fset *token.FileSet, fn *types.Func) (*Provider, []erro
}
}
}
if recv := sig.Recv(); recv != nil {
switch typ := recv.Type().(type) {
case *types.Pointer:
elem := typ.Elem().(*types.Named)
provider.Name = fmt.Sprintf("(*%s).%s", elem.Obj().Name(), fn.Name())
provider.Args = append([]ProviderInput{{Type: typ}}, provider.Args...)
case *types.Named:
provider.Name = fmt.Sprintf("(%s).%s", typ.Obj().Name(), fn.Name())
provider.Args = append([]ProviderInput{{Type: typ}}, provider.Args...)
}
}
return provider, nil
}

Expand Down
5 changes: 5 additions & 0 deletions internal/wire/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,11 @@ func (g *gen) qualifiedID(pkgName, pkgPath, sym string) string {
if name == "" {
return sym
}
if strings.HasPrefix(sym, "(*") {
return sym[:2] + name + "." + sym[2:]
} else if strings.HasPrefix(sym, "(") {
return sym[:1] + name + "." + sym[1:]
}
return name + "." + sym
}

Expand Down
18 changes: 18 additions & 0 deletions tests/method_provider/animals/animals.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package animals

type Dog struct{}
type Cat struct{}

type Animals struct{}

func (f *Animals) NewDog() *Dog {
return &Dog{}
}

func (Animals) NewCat() *Cat {
return &Cat{}
}

func NewAnimals() *Animals {
return &Animals{}
}
23 changes: 23 additions & 0 deletions tests/method_provider/wire.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//+build wireinject
//go:generate wire

package method_provider

import (
"github.com/google/wire"
"github.com/google/wire/tests/method_provider/animals"
)

func InitDog() *animals.Dog {
panic(wire.Build(
animals.NewAnimals,
(*animals.Animals).NewDog,
))
}

func InitCat() *animals.Cat {
panic(wire.Build(
wire.Value(animals.Animals{}),
animals.Animals.NewCat,
))
}
28 changes: 28 additions & 0 deletions tests/method_provider/wire_gen.go

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

0 comments on commit a86b32f

Please sign in to comment.