-
Notifications
You must be signed in to change notification settings - Fork 0
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
JTASK-863: fix bug in Option[T].Scan #2
Conversation
options.go
Outdated
// Convert between string and []byte | ||
dstType := reflect.TypeOf(dst.value) | ||
srcType := reflect.TypeOf(src) | ||
if isByteSlice(srcType) && dstType.Kind() == reflect.String { | ||
src = string(src.([]byte)) | ||
*dst = New(src.(T)) | ||
return nil | ||
} | ||
if srcType.Kind() == reflect.String && isByteSlice(dstType) { | ||
src = []byte(src.(string)) | ||
*dst = New(src.(T)) | ||
return nil | ||
} | ||
|
||
return fmt.Errorf("Option[%T].Scan: failed to convert value %#v to type %T", dst.value, src, dst.value) |
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 seems that the dst and src type mismatch problem is not limited to strings.
https://zenn.dev/methane/articles/2023-go-mysql-parse-numbers
If we wanted full SQL Driver support with Option type, the flow would be something like this
- if
src
isnil
,dst
becomesNone
- if
dst
type implements a Scanner interface, use it - if
dst
type is a primitive (or its defined type), use a huge switch statement to achieve the conversion, just as the sql package does. https://cs.opensource.google/go/go/+/master:src/database/sql/convert.go;l=211?q=convertAssign&ss=go%2Fgo
Or, we may be able to use sql.Null[T]
after releasing go 1.22.
golang/go#60370
For now, I decided to copy and use the standard Go library implementation. |
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.
LGTM
No description provided.