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

Interface regression #1009

Merged
merged 2 commits into from
Feb 5, 2020
Merged
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
23 changes: 5 additions & 18 deletions codegen/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,25 +196,12 @@ func (b *builder) bindField(obj *Object, f *Field) (errret error) {
}
}

func (b *builder) findBindTarget(in types.Type, name string) (types.Object, error) {
switch t := in.(type) {
case *types.Named:
if _, ok := t.Underlying().(*types.Interface); ok {
return nil, errors.New("can't bind to an interface at root")
}
case *types.Interface:
return nil, errors.New("can't bind to an interface at root")
}

return b.findBindTargetRecur(in, name)
}

// findBindTargetRecur attempts to match the name to a field or method on a Type
// findBindTarget attempts to match the name to a field or method on a Type
// with the following priorites:
// 1. Any Fields with a struct tag (see config.StructTag). Errors if more than one match is found
// 2. Any method or field with a matching name. Errors if more than one match is found
// 3. Same logic again for embedded fields
func (b *builder) findBindTargetRecur(t types.Type, name string) (types.Object, error) {
func (b *builder) findBindTarget(t types.Type, name string) (types.Object, error) {
// NOTE: a struct tag will override both methods and fields
// Bind to struct tag
found, err := b.findBindStructTagTarget(t, name)
Expand Down Expand Up @@ -366,7 +353,7 @@ func (b *builder) findBindStructEmbedsTarget(strukt *types.Struct, name string)
fieldType = ptr.Elem()
}

f, err := b.findBindTargetRecur(fieldType, name)
f, err := b.findBindTarget(fieldType, name)
if err != nil {
return nil, err
}
Expand All @@ -388,7 +375,7 @@ func (b *builder) findBindInterfaceEmbedsTarget(iface *types.Interface, name str
for i := 0; i < iface.NumEmbeddeds(); i++ {
embeddedType := iface.EmbeddedType(i)

f, err := b.findBindTargetRecur(embeddedType, name)
f, err := b.findBindTarget(embeddedType, name)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -481,7 +468,7 @@ func (f *Field) ShortResolverDeclaration() string {
res := "(ctx context.Context"

if !f.Object.Root {
res += fmt.Sprintf(", obj *%s", templates.CurrentImports.LookupType(f.Object.Type))
res += fmt.Sprintf(", obj %s", templates.CurrentImports.LookupType(f.Object.Reference()))
}
for _, arg := range f.Args {
res += fmt.Sprintf(", %s %s", arg.VarName, templates.CurrentImports.LookupType(arg.TypeReference.GO))
Expand Down
7 changes: 6 additions & 1 deletion codegen/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,12 @@ func (b *builder) buildObject(typ *ast.Definition) (*Object, error) {
}

func (o *Object) Reference() types.Type {
switch o.Type.(type) {
switch v := o.Type.(type) {
case *types.Named:
_, isInterface := v.Underlying().(*types.Interface)
if isInterface {
return o.Type
}
case *types.Pointer, *types.Slice, *types.Map:
return o.Type
}
Expand Down
Loading