Skip to content

Commit

Permalink
Check for embedded pointer when finding field on struct
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathew Byrne committed Oct 1, 2018
1 parent f302b40 commit caa474c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 36 deletions.
23 changes: 5 additions & 18 deletions codegen/testserver/generated.go

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

9 changes: 0 additions & 9 deletions codegen/testserver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ import (

type Resolver struct{}

func (r *Resolver) EmbeddedPointer() EmbeddedPointerResolver {
return &embeddedPointerResolver{r}
}
func (r *Resolver) ForcedResolver() ForcedResolverResolver {
return &forcedResolverResolver{r}
}
Expand All @@ -24,12 +21,6 @@ func (r *Resolver) Subscription() SubscriptionResolver {
return &subscriptionResolver{r}
}

type embeddedPointerResolver struct{ *Resolver }

func (r *embeddedPointerResolver) Title(ctx context.Context, obj *EmbeddedPointerModel) (*string, error) {
panic("not implemented")
}

type forcedResolverResolver struct{ *Resolver }

func (r *forcedResolverResolver) Field(ctx context.Context, obj *ForcedResolver) (*Circle, error) {
Expand Down
17 changes: 8 additions & 9 deletions codegen/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,16 @@ func findField(typ *types.Struct, name, structTag string) (*types.Var, error) {
}

if field.Anonymous() {
if named, ok := field.Type().(*types.Struct); ok {
f, err := findField(named, name, structTag)
if err != nil && !strings.HasPrefix(err.Error(), "no field named") {
return nil, err
}
if f != nil && foundField == nil {
foundField = f
}

fieldType := field.Type()

if ptr, ok := fieldType.(*types.Pointer); ok {
fieldType = ptr.Elem()
}

if named, ok := field.Type().Underlying().(*types.Struct); ok {
// Type.Underlying() returns itself for all types except types.Named, where it returns a struct type.
// It should be safe to always call.
if named, ok := fieldType.Underlying().(*types.Struct); ok {
f, err := findField(named, name, structTag)
if err != nil && !strings.HasPrefix(err.Error(), "no field named") {
return nil, err
Expand Down

0 comments on commit caa474c

Please sign in to comment.