From a2a185386fb42abb38236f3cc44cbfee076e42b2 Mon Sep 17 00:00:00 2001 From: qawatake <38106890+qawatake@users.noreply.github.com> Date: Sat, 23 Mar 2024 00:08:11 +0900 Subject: [PATCH] wire.FieldsOf * --- Makefile | 2 ++ _tutorial/main.go | 23 +++++++++++++++++++++++ _tutorial/wire.go | 4 ++++ _tutorial/wire_gen.go | 10 ++++++++++ internal/wire/parse.go | 33 ++++++++++++++++++++++++++++++--- 5 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..d8664d81 --- /dev/null +++ b/Makefile @@ -0,0 +1,2 @@ +wire: + go run ./cmd/wire ./_tutorial/ diff --git a/_tutorial/main.go b/_tutorial/main.go index b00e5b8a..cf974eca 100644 --- a/_tutorial/main.go +++ b/_tutorial/main.go @@ -20,6 +20,8 @@ import ( "fmt" "os" "time" + + "github.com/google/wire" ) // Message is what greeters will use to greet guests. @@ -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{} +} diff --git a/_tutorial/wire.go b/_tutorial/wire.go index 9f5b7475..dd12bac6 100644 --- a/_tutorial/wire.go +++ b/_tutorial/wire.go @@ -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)) +} diff --git a/_tutorial/wire_gen.go b/_tutorial/wire_gen.go index fe54ab77..dabd11fe 100644 --- a/_tutorial/wire_gen.go +++ b/_tutorial/wire_gen.go @@ -8,6 +8,8 @@ package main // Injectors from wire.go: +// InitializeEvent creates an Event. It will error if the Event is staffed with +// a grumpy greeter. func InitializeEvent(phrase string) (Event, error) { message := NewMessage(phrase) greeter := NewGreeter(message) @@ -17,3 +19,11 @@ func InitializeEvent(phrase string) (Event, error) { } return event, nil } + +func InitializeX(g God) A { + x := g.X + y := g.Y + z := g.Z + a := New(x, y, z) + return a +} diff --git a/internal/wire/parse.go b/internal/wire/parse.go index 93fbda85..849f4aa2 100644 --- a/internal/wire/parse.go +++ b/internal/wire/parse.go @@ -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 { @@ -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 }