internal/wire: move field name into ProviderInput (#104)

Avoids having parallel arrays for conceptually related data.
This commit is contained in:
Ross Light
2019-01-23 11:34:39 -08:00
committed by GitHub
parent 9a7ae3ba6b
commit 3b0186f7df
2 changed files with 9 additions and 9 deletions

View File

@@ -179,8 +179,12 @@ dfs:
} }
index.Set(curr.t, given.Len()+len(calls)) index.Set(curr.t, given.Len()+len(calls))
kind := funcProviderCall kind := funcProviderCall
fieldNames := []string(nil)
if p.IsStruct { if p.IsStruct {
kind = structProvider kind = structProvider
for _, arg := range p.Args {
fieldNames = append(fieldNames, arg.FieldName)
}
} }
calls = append(calls, call{ calls = append(calls, call{
kind: kind, kind: kind,
@@ -188,7 +192,7 @@ dfs:
name: p.Name, name: p.Name,
args: args, args: args,
varargs: p.Varargs, varargs: p.Varargs,
fieldNames: p.Fields, fieldNames: fieldNames,
ins: ins, ins: ins,
out: curr.t, out: curr.t,
hasCleanup: p.HasCleanup, hasCleanup: p.HasCleanup,

View File

@@ -160,10 +160,6 @@ type Provider struct {
// Otherwise it's a function. // Otherwise it's a function.
IsStruct bool 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 // Out is the set of types this provider produces. It will always
// contain at least one type. // contain at least one type.
Out []types.Type Out []types.Type
@@ -181,7 +177,8 @@ type Provider struct {
type ProviderInput struct { type ProviderInput struct {
Type types.Type 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. // Value describes a value expression.
@@ -724,16 +721,15 @@ func processStructProvider(fset *token.FileSet, typeName *types.TypeName) (*Prov
Name: typeName.Name(), Name: typeName.Name(),
Pos: pos, Pos: pos,
Args: make([]ProviderInput, st.NumFields()), Args: make([]ProviderInput, st.NumFields()),
Fields: make([]string, st.NumFields()),
IsStruct: true, IsStruct: true,
Out: []types.Type{out, types.NewPointer(out)}, Out: []types.Type{out, types.NewPointer(out)},
} }
for i := 0; i < st.NumFields(); i++ { for i := 0; i < st.NumFields(); i++ {
f := st.Field(i) f := st.Field(i)
provider.Args[i] = ProviderInput{ provider.Args[i] = ProviderInput{
Type: f.Type(), Type: f.Type(),
FieldName: f.Name(),
} }
provider.Fields[i] = f.Name()
for j := 0; j < i; j++ { for j := 0; j < i; j++ {
if types.Identical(provider.Args[i].Type, provider.Args[j].Type) { 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)))} return nil, []error{notePosition(fset.Position(pos), fmt.Errorf("provider struct has multiple fields of type %s", types.TypeString(provider.Args[j].Type, nil)))}