-
Notifications
You must be signed in to change notification settings - Fork 48
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 embedded structs #17
base: master
Are you sure you want to change the base?
Conversation
None of the tests fail on master for me. Can you paste me the test output?
I somewhat hastily accepted a patch for embedded structs on sqlx in the past, so I want to accept this but I want to be ab it more diligent about it first. |
Thanks for reviewing this. It doesn't address any of those 3 bullet points you raised; good catches. I'll revise the patch. BTW, the master test failure (which, to be clear, seems unrelated to this) is:
The correct value is written to the MySQL table, but it fails to be scanned into the time value correctly. My |
Ah, it's failing for MySQL only. I believe this is related to not having the |
It should be possible to support this a little more easily now that github.com/jmoiron/sqlx/reflectx exists. Part of the design of that was to allow reuse here. I'll look into this sometime later this week when I have time. |
hi, is there any progress on this? |
I just noticed the following: If your autoIncr Id isn't the first Field in the struct, you get the following panic: You can trigger it really simple by changing the test: diff --git a/modl_test.go b/modl_test.go
index 52e5694..e670815 100644
--- a/modl_test.go
+++ b/modl_test.go
@@ -66,8 +66,8 @@ type WithStringPk struct {
type CustomStringType string
type WithEmbeddedStruct struct {
- Id int64
Names
+ Id int64
}
|
Fixes issue reported by @cryptix at #17 (comment).
OK, I've rebased the patch against current master and addressed @cryptix's issue. Responses below to the points @jmoiron raised. Also, @jmoiron, let me know if you think the reflection code could be replaced by reflectx. are we skipping unexported fields? (f.PkgPath != "")No. It seems useful and consistent with expectations to be able to have: type myEmbeddedStruct struct { B string }
type myTableMappedStruct struct {
ID int
myEmbeddedStruct
} where what about structs which implement Scanner?encoding/json and Go convention would imply that we should map an embedded struct implementing Scanner to a single column (whereas if it did not implement Scanner it would map to one column per field). This sounds right to me. It definitely would deserve a mention in the final docs for struct embedding. what about name shadowing? this is DFS and Go's access rules are BFSIs there a way to use reflectx to make proper name shadowing easier to implement? I could implement it here and write tests for it, but if there is an easy way to use reflectx to do it, or if you plan to add such functionality to reflectx, I don't want to duplicate it here. |
Reflectx has had some time in production now that it is in charge of doing field maps for sqlx. I believe it skips unexported embedded fields, which looks like it may be a mistake. I think this should be easily changeable, as changing this couldn't interfere with working code. It does however properly handle the tree search and it implements the fastest possible attribute access code (recursively fetching fields by index rather than doing one fetch by name is much faster). I suspect that reflectx will make this simpler, but that I might have to tweak a thing or two with it. It would be a good second system to run with it on. |
+1. @jmoiron happy to help wherever we need it. would you like me to take a look at sqlx and implement new reflect logic to recursively map structs? |
Adds support for working with embedded structs.
For example, consider the
WithEmbeddedStruct
type (which is part of the associated test):It maps to a DB table with
id
,firstname
, andlastname
columns. (The embedded struct is flattened, as is the default behavior in most Go encoders (incl.encoding/json
) and gorp). Structs of type*WithEmbeddedStruct
can beInsert
ed orSelect
ed into, and the embedded struct fields will be written to.The tests pass on MySQL, PostgreSQL, and SQLite3. (Incidentally, TestWithTime fails for me on master and with this PR; I think that's an sqlx issue.)