From 7d6f8ed4b4d5e64eb98dca853aa58fef9aee8784 Mon Sep 17 00:00:00 2001 From: Cody Ley-Han Date: Fri, 24 Aug 2018 14:28:40 -0700 Subject: [PATCH] fixes case where embeded structs would cause no field to be found --- codegen/util.go | 4 ++-- codegen/util_test.go | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/codegen/util.go b/codegen/util.go index a1681911013..1849f100bb1 100644 --- a/codegen/util.go +++ b/codegen/util.go @@ -134,7 +134,7 @@ 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 { + if err != nil && !strings.HasPrefix(err.Error(), "no field named") { return nil, err } if f != nil && foundField == nil { @@ -144,7 +144,7 @@ func findField(typ *types.Struct, name, structTag string) (*types.Var, error) { if named, ok := field.Type().Underlying().(*types.Struct); ok { f, err := findField(named, name, structTag) - if err != nil { + if err != nil && !strings.HasPrefix(err.Error(), "no field named") { return nil, err } if f != nil && foundField == nil { diff --git a/codegen/util_test.go b/codegen/util_test.go index 0c6935cc3d9..aedfda04694 100644 --- a/codegen/util_test.go +++ b/codegen/util_test.go @@ -38,6 +38,10 @@ type Amb struct { Bar string ` + "`" + `gqlgen:"foo"` + "`" + ` Foo int ` + "`" + `gqlgen:"foo"` + "`" + ` } +type Embed struct { + Std + Test string +} ` scope, err := parseScope(input, "test") require.NoError(t, err) @@ -46,6 +50,7 @@ type Amb struct { anon := scope.Lookup("Anon").Type().Underlying().(*types.Struct) tags := scope.Lookup("Tags").Type().Underlying().(*types.Struct) amb := scope.Lookup("Amb").Type().Underlying().(*types.Struct) + embed := scope.Lookup("Embed").Type().Underlying().(*types.Struct) tests := []struct { Name string @@ -61,6 +66,7 @@ type Amb struct { {"Picks field with tag over field name when passed a tag", tags, "foo", "gqlgen", "Bar", false}, {"Errors when ambigious", amb, "foo", "gqlgen", "", true}, {"Finds a field that is in embedded struct", anon, "bar", "", "Bar", false}, + {"Finds field that is not in embedded struct", embed, "test", "", "Test", false}, } for _, tt := range tests {