-
Notifications
You must be signed in to change notification settings - Fork 86
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 custom types in query result scanning #1599
base: master
Are you sure you want to change the base?
Changes from all 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 |
---|---|---|
@@ -1,5 +1,12 @@ | ||
package value | ||
|
||
import ( | ||
"database/sql" | ||
"database/sql/driver" | ||
|
||
"github.com/google/uuid" | ||
) | ||
|
||
func CastTo(v Value, dst interface{}) error { | ||
if dst == nil { | ||
return errNilDestination | ||
|
@@ -10,5 +17,28 @@ func CastTo(v Value, dst interface{}) error { | |
return nil | ||
} | ||
|
||
if _, ok := dst.(*uuid.UUID); ok { | ||
return v.castTo(dst) | ||
} | ||
|
||
if scanner, has := dst.(sql.Scanner); has { | ||
dv := new(driver.Value) | ||
|
||
err := v.castTo(dv) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return scanner.Scan(*dv) | ||
} | ||
|
||
if scanner, has := dst.(Scanner); has { | ||
return scanner.UnmarshalYDBValue(v) | ||
} | ||
|
||
return v.castTo(dst) | ||
} | ||
|
||
type Scanner interface { | ||
UnmarshalYDBValue(value Value) error | ||
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. YDB has scanner interface 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. Thats very cool interface to use! But now there is only one implementation How do you think, maybe better to create adapter for value.Value which implements scanner.RawValue? UPD: i think for library would be better if we won't create new interface |
||
} |
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.
I think special ydb scanner should be preferred over more common sql scanner.