From 3b0186f7df332d6418a53649069ac78f775fb163 Mon Sep 17 00:00:00 2001 From: Ross Light Date: Wed, 23 Jan 2019 11:34:39 -0800 Subject: [PATCH] internal/wire: move field name into ProviderInput (#104) Avoids having parallel arrays for conceptually related data. --- internal/wire/analyze.go | 6 +++++- internal/wire/parse.go | 12 ++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/internal/wire/analyze.go b/internal/wire/analyze.go index c3ca1cb..11d1189 100644 --- a/internal/wire/analyze.go +++ b/internal/wire/analyze.go @@ -179,8 +179,12 @@ dfs: } index.Set(curr.t, given.Len()+len(calls)) kind := funcProviderCall + fieldNames := []string(nil) if p.IsStruct { kind = structProvider + for _, arg := range p.Args { + fieldNames = append(fieldNames, arg.FieldName) + } } calls = append(calls, call{ kind: kind, @@ -188,7 +192,7 @@ dfs: name: p.Name, args: args, varargs: p.Varargs, - fieldNames: p.Fields, + fieldNames: fieldNames, ins: ins, out: curr.t, hasCleanup: p.HasCleanup, diff --git a/internal/wire/parse.go b/internal/wire/parse.go index a2abf89..2054631 100644 --- a/internal/wire/parse.go +++ b/internal/wire/parse.go @@ -160,10 +160,6 @@ type Provider struct { // Otherwise it's a function. IsStruct bool - // Fields lists the field names to populate. This will map 1:1 with - // elements in Args. - Fields []string - // Out is the set of types this provider produces. It will always // contain at least one type. Out []types.Type @@ -181,7 +177,8 @@ type Provider struct { type ProviderInput struct { Type types.Type - // TODO(light): Move field name into this struct. + // If the provider is a struct, FieldName will be the field name to set. + FieldName string } // Value describes a value expression. @@ -724,16 +721,15 @@ func processStructProvider(fset *token.FileSet, typeName *types.TypeName) (*Prov Name: typeName.Name(), Pos: pos, Args: make([]ProviderInput, st.NumFields()), - Fields: make([]string, st.NumFields()), IsStruct: true, Out: []types.Type{out, types.NewPointer(out)}, } for i := 0; i < st.NumFields(); i++ { f := st.Field(i) provider.Args[i] = ProviderInput{ - Type: f.Type(), + Type: f.Type(), + FieldName: f.Name(), } - provider.Fields[i] = f.Name() for j := 0; j < i; j++ { if types.Identical(provider.Args[i].Type, provider.Args[j].Type) { return nil, []error{notePosition(fset.Position(pos), fmt.Errorf("provider struct has multiple fields of type %s", types.TypeString(provider.Args[j].Type, nil)))}