8000 Support filed name `"*"` for wire.FieldsOf by qawatake · Pull Request #2 · qawatake/wire · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Support filed name "*" for wire.FieldsOf #2

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

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
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
wire:
go run ./cmd/wire ./_tutorial/
23 changes: 23 additions & 0 deletions _tutorial/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"fmt"
"os"
"time"

"github.com/google/wire"
)

// Message is what greeters will use to greet guests.
Expand Down Expand Up @@ -81,3 +83,24 @@ func main() {
}
e.Start()
}

type X int
type Y int
type Z int

type A struct{}

type God struct {
X X
Y Y
Z Z
}

var set = wire.NewSet(
New,
wire.FieldsOf(new(God), "*"),
)

func New(X, Y, Z) A {
return A{}
}
4 changes: 4 additions & 0 deletions _tutorial/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ func InitializeEvent(phrase string) (Event, error) {
wire.Build(NewEvent, NewGreeter, NewMessage)
return Event{}, nil
}

func InitializeX(g God) A {
panic(wire.Build(set))
}
10 changes: 10 additions & 0 deletions _tutorial/wire_gen.go

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

33 changes: 30 additions & 3 deletions internal/wire/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -1038,6 +1038,33 @@ func processFieldsOf(fset *token.FileSet, info *types.Info, call *ast.CallExpr)
}

fields := make([]*Field, 0, len(call.Args)-1)
startExists := func() bool {
for i := 1; i < len(call.Args); i++ {
if b, ok := call.Args[i].(*ast.BasicLit); ok && b.Value == `"*"` {
return true
}
}
return false
}()
if startExists {
for i := 0; i < struc.NumFields(); i++ {
v := struc.Field(i)
out := []types.Type{v.Type()}
if isPtrToStruct {
// If the field is from a pointer to a struct, then
// wire.Fields also provides a pointer to the field.
out = append(out, types.NewPointer(v.Type()))
}
fields = append(fields, &Field{
Parent: structPtr.Elem(),
Name: v.Name(),
Pkg: v.Pkg(),
Pos: v.Pos(),
Out: out,
})
}
return fields, nil
}
for i := 1; i < len(call.Args); i++ {
v, err := checkField(call.Args[i], struc)
if err != nil {
Expand Down Expand Up @@ -1173,9 +1200,9 @@ func (pt ProvidedType) IsNil() bool {
//
// - For a function provider, this is the first return value type.
// - For a struct provider, this is either the struct type or the pointer type
// whose element type is the struct type.
// - For a value, this is the type of the expression.
// - For an argument, this is the type of the argument.
// whose element type is the struct type.
// - For a value, this is the type of the expression.
// - For an argument, this is the type of the argument.
func (pt ProvidedType) Type() types.Type {
return pt.t
}
Expand Down
0