-
Notifications
You must be signed in to change notification settings - Fork 493
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
Support for embedded struct type in resolver #338
Changes from 4 commits
f40cd36
d5b02b1
6110b37
8f68317
c94cde5
be8816b
3f7a8e9
943d21c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,7 +34,7 @@ type Field struct { | |
schema.Field | ||
TypeName string | ||
MethodIndex int | ||
FieldIndex int | ||
FieldIndex []int | ||
HasContext bool | ||
HasError bool | ||
ArgsPacker *packer.StructPacker | ||
|
@@ -43,7 +43,7 @@ type Field struct { | |
} | ||
|
||
func (f *Field) UseMethodResolver() bool { | ||
return f.FieldIndex == -1 | ||
return len(f.FieldIndex) == 0 | ||
} | ||
|
||
type TypeAssertion struct { | ||
|
@@ -228,13 +228,17 @@ func (b *execBuilder) makeObjectExec(typeName string, fields schema.FieldList, p | |
|
||
Fields := make(map[string]*Field) | ||
rt := unwrapPtr(resolverType) | ||
fieldsCount := getFieldCount(rt, map[string]int{}) | ||
for _, f := range fields { | ||
fieldIndex := -1 | ||
var fieldIndex []int | ||
methodIndex := findMethod(resolverType, f.Name) | ||
if b.schema.UseFieldResolvers && methodIndex == -1 { | ||
fieldIndex = findField(rt, f.Name) | ||
if fieldsCount[strings.ToLower(stripUnderscore(f.Name))] > 1 { | ||
return nil, fmt.Errorf("%s does not resolve %q: ambiguous field %q", resolverType, typeName, f.Name) | ||
} | ||
eloyekunle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
fieldIndex = findField(rt, f.Name, []int{}) | ||
} | ||
if methodIndex == -1 && fieldIndex == -1 { | ||
if methodIndex == -1 && len(fieldIndex) == 0 { | ||
hint := "" | ||
if findMethod(reflect.PtrTo(resolverType), f.Name) != -1 { | ||
hint = " (hint: the method exists on the pointer type)" | ||
|
@@ -247,7 +251,7 @@ func (b *execBuilder) makeObjectExec(typeName string, fields schema.FieldList, p | |
if methodIndex != -1 { | ||
m = resolverType.Method(methodIndex) | ||
} else { | ||
sf = rt.Field(fieldIndex) | ||
sf = rt.FieldByIndex(fieldIndex) | ||
} | ||
fe, err := b.makeFieldExec(typeName, f, m, sf, methodIndex, fieldIndex, methodHasReceiver) | ||
if err != nil { | ||
|
@@ -290,7 +294,7 @@ var contextType = reflect.TypeOf((*context.Context)(nil)).Elem() | |
var errorType = reflect.TypeOf((*error)(nil)).Elem() | ||
|
||
func (b *execBuilder) makeFieldExec(typeName string, f *schema.Field, m reflect.Method, sf reflect.StructField, | ||
methodIndex, fieldIndex int, methodHasReceiver bool) (*Field, error) { | ||
methodIndex int, fieldIndex []int, methodHasReceiver bool) (*Field, error) { | ||
|
||
var argsPacker *packer.StructPacker | ||
var hasError bool | ||
|
@@ -380,13 +384,46 @@ func findMethod(t reflect.Type, name string) int { | |
return -1 | ||
} | ||
|
||
func findField(t reflect.Type, name string) int { | ||
func findField(t reflect.Type, name string, index []int) []int { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why does this method need an integer array? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function builds the index sequence for |
||
for i := 0; i < t.NumField(); i++ { | ||
if strings.EqualFold(stripUnderscore(name), stripUnderscore(t.Field(i).Name)) { | ||
return i | ||
field := t.Field(i) | ||
|
||
if field.Type.Kind() == reflect.Struct && field.Anonymous { | ||
newIndex := findField(field.Type, name, []int{i}) | ||
if len(newIndex) > 1 { | ||
return append(index, newIndex...) | ||
eloyekunle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
if strings.EqualFold(stripUnderscore(name), stripUnderscore(field.Name)) { | ||
return append(index, i) | ||
} | ||
} | ||
return -1 | ||
|
||
return index | ||
} | ||
|
||
// getFieldCount helps resolve ambiguity when more than one embedded struct contains fields with the same name. | ||
eloyekunle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
func getFieldCount(t reflect.Type, count map[string]int) map[string]int { | ||
if t.Kind() != reflect.Struct { | ||
return nil | ||
} | ||
|
||
for i := 0; i < t.NumField(); i++ { | ||
field := t.Field(i) | ||
fieldName := strings.ToLower(stripUnderscore(field.Name)) | ||
|
||
if field.Type.Kind() == reflect.Struct && field.Anonymous { | ||
count = getFieldCount(field.Type, count) | ||
} else { | ||
if _, ok := count[fieldName]; !ok { | ||
count[fieldName] = 0 | ||
} | ||
count[fieldName]++ | ||
} | ||
} | ||
|
||
return count | ||
} | ||
|
||
func unwrapNonNull(t common.Type) (common.Type, bool) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why does the field index is an array? I don't get it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is an array because it is an index sequence for
Type.FieldByIndex
.It allows for several layers of anonymously-nested fields.