-
-
Notifications
You must be signed in to change notification settings - Fork 57
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
Allow named structs to be embedded #303
Changes from 2 commits
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 |
---|---|---|
|
@@ -238,7 +238,7 @@ func getDocumentMeta(rt reflect.Type, skipAssoc bool) DocumentMeta { | |
typ = typ.Elem() | ||
} | ||
|
||
if typ.Kind() == reflect.Struct && sf.Anonymous { | ||
if typ.Kind() == reflect.Struct && isEmbedded(sf) { | ||
embedded := getDocumentMeta(typ, skipAssoc) | ||
embeddedName := "" | ||
if tagged { | ||
|
@@ -357,6 +357,21 @@ func fieldName(sf reflect.StructField) (string, bool) { | |
return snaker.CamelToSnake(sf.Name), false | ||
} | ||
|
||
func isEmbedded(sf reflect.StructField) bool { | ||
// anonymous structs are always embedded | ||
if sf.Anonymous { | ||
return true | ||
} | ||
// search for embedded tag | ||
tags := strings.Split(sf.Tag.Get("db"), ",") | ||
for i, tag := range tags { | ||
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. other codes uses but this is fine as well because it'll be cached anyway 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. If HasSuffix is ok, let's use it. |
||
if tag == "embedded" && i > 0 { | ||
dranikpg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func searchPrimary(rt reflect.Type) ([]string, [][]int) { | ||
if result, cached := primariesCache.Load(rt); cached { | ||
p := result.(primaryData) | ||
|
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.
Is this still true to provide backwards compatibility?
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.
anonymous struct is basically an embedded struct, so I think no problem