Skip to content

Commit

Permalink
Merge pull request 99designs#358 from 99designs/fix-embedded-pointer
Browse files Browse the repository at this point in the history
Fix Embedded Pointer
  • Loading branch information
Mathew Byrne authored Oct 2, 2018
2 parents cbefd17 + 3db30da commit 4e0f99a
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 9 deletions.
91 changes: 91 additions & 0 deletions codegen/testserver/generated.go

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

2 changes: 2 additions & 0 deletions codegen/testserver/gqlgen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ models:
friends: { resolver: true }
Error:
model: "github.com/99designs/gqlgen/codegen/testserver.Error"
EmbeddedPointer:
model: "github.com/99designs/gqlgen/codegen/testserver.EmbeddedPointerModel"
9 changes: 9 additions & 0 deletions codegen/testserver/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,12 @@ func (Error) ErrorOnNonRequiredField() (string, error) {
func (Error) NilOnRequiredField() *string {
return nil
}

type EmbeddedPointerModel struct {
*EmbeddedPointer
ID string
}

type EmbeddedPointer struct {
Title string
}
5 changes: 5 additions & 0 deletions codegen/testserver/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,8 @@ union ShapeUnion = Circle | Rectangle
type ForcedResolver {
field: Circle
}

type EmbeddedPointer {
ID: String
Title: String
}
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 4e0f99a

Please sign in to comment.