Skip to content
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

properly handle nil value in Scan #139

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

lanre-ade
Copy link

@lanre-ade lanre-ade commented Mar 3, 2019

The current implementation breaks expected behaviour of sql scanner when scanning into a pointer. Scanning a NULL column into a pointer d *Decimal returns an error instead of the pointer being set to nil

@lanre-ade lanre-ade changed the title Update decimal.go properly handle nil in scan Mar 4, 2019
@lanre-ade lanre-ade changed the title properly handle nil in scan properly handle nil value in Scan Mar 4, 2019
Copy link
Member

@njason njason left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add at least one test that covers this new case

@@ -970,6 +970,11 @@ func (d Decimal) MarshalBinary() (data []byte, err error) {

// Scan implements the sql.Scanner interface for database deserialization.
func (d *Decimal) Scan(value interface{}) error {
if value == nil {
d = nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
d = nil
*d = nil

Copy link
Contributor

@serprex serprex Apr 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Except this won't work because *d expects a Decimal

In recent versions of go you can use sql.Null[decimal.Decimal]

@serprex serprex mentioned this pull request Apr 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants