-
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
Conversation
What happens if there is more than one embedded struct with the same field? |
@pavelnikolov I have added a method, |
@pavelnikolov Please take another look. |
for _, f := range fields { | ||
fieldIndex := -1 | ||
var fieldIndex []int |
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.
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
Why does this method need an integer array? , index []int
?
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.
This function builds the index sequence for Type.FieldByIndex
recursively, returning the sequence if found and an empty array otherwise.
I've also added a unit test for the feature here. |
Name string // ambiguous | ||
} | ||
|
||
func TestPanicAmbiguity(t *testing.T) { |
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.
Can you, please, move ambiguousResolver
and University
inside of the test (e.i. TestPanicAmbiguity
)?
type Timestamps struct { | ||
CreatedAt string | ||
UpdatedAt string | ||
} |
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.
Same here. Can you, please, move the definition of these structs inside of the test TestEmbeddedStruct
?
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.
Please, move the struct definitions inside of the corresponding tests and I'll be happy to merge this PR.
Actually, I'll leave it like this for consistency. Refactoring should be in a separate PR. |
Fixes #331
Updated example in
./example/social/social.go
to demonstrate how it works.Follow up to: #282
Ref: #28