Update reflectx to allow for optional nested structs#950
Update reflectx to allow for optional nested structs#950kszafran wants to merge 8 commits intojmoiron:masterfrom
Conversation
|
Note that this is a minor breaking change. If someone scans into a struct that has a struct pointer field and all the fields in the nested struct can accept NULL values, this would change the observed behavior. For example: type Inner struct {
A *string
B *int64
}
type Outer struct {
Inner *Inner
}Previously, when NULL was scanned into the fields of the nested struct, the nested struct would still be present ( Moreover, if someone has custom type JSON json.RawMessage
func (j *JSON) Scan(src any) error {
if src == nil {
*j = "{}"
return
}
...
}
type Outer struct {
Inner struct {
Stuff JSON
Thing string
}
}
Thing string
Stuff JSONthen it would be called (assuming we're scanning non-NULL into While there are different ways we could go about it, I'm thinking the safest approach would be to make this feature opt-in, e.g. with an type Outer struct {
Inner *Inner `db:inner,omitempty`
}If there's no Let me know what you think, and I could update this PR. |
|
BTW, note that this feature makes it possible to have optional nested structs without the use of pointers, too. For example: type Inner struct {
A string
B int64
}
type Outer struct {
Inner Inner
}Normally, if I used a LEFT JOIN and |
7b63394 to
26b1bb1
Compare
Based on #847 and #900, but simplified and fully compatible with the standard library scanning behavior.
Compared to previous PRs:
convertAssignfrom the standard library (which was done in a way that introduced incompatibilities), I used//go:linknameto call the original function directly. Normally, this isn't recommended. However, Go authors recognize that some libraries do that, and marked selected functions to avoid breaking changes, includingconvertAssign. See https://go.dev/issue/67401.ObjectContextwith a basicfunctype.All in all, the changes are now much more succinct, which I hope will help get this PR reviewed and merged.