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

feat: support method provider #295

Closed
wants to merge 1 commit into from
Closed
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
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.