-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Update reflectx to allow for optional nested structs #950
base: master
Are you sure you want to change the base?
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 JSON then 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:
convertAssign
from the standard library (which was done in a way that introduced incompatibilities), I used//go:linkname
to 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.ObjectContext
with a basicfunc
type.All in all, the changes are now much more succinct, which I hope will help get this PR reviewed and merged.