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

Check if ok when casting Scan src to string #33

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion amount.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,10 @@ func (a Amount) Value() (driver.Value, error) {
// Allows scanning amounts from a PostgreSQL composite type.
func (a *Amount) Scan(src interface{}) error {
// Wire format: "(9.99,USD)".
input := src.(string)
input, ok := src.(string)
if !ok {
return fmt.Errorf("invalid src: %v", src)
Copy link
Owner

Choose a reason for hiding this comment

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

This error does not tell us why the value is invalid.

What if we did this instead:

return fmt.Errorf("value is not a string: %v", src)

}
if len(input) == 0 {
return nil
}
Expand Down
8 changes: 8 additions & 0 deletions amount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -930,3 +930,11 @@ func TestAmount_Scan(t *testing.T) {
})
}
}

func TestAmount_ScanNonString(t *testing.T) {
var a currency.Amount
err := a.Scan(123)
if err == nil {
t.Errorf("error expected")
}
}
Loading