Skip to content

Commit

Permalink
Check if ok when casting Scan src to string
Browse files Browse the repository at this point in the history
  • Loading branch information
theFong authored and bojanz committed Apr 19, 2024
1 parent 1a41536 commit cf9e73a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
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("value is not a string: %v", src)
}
if len(input) == 0 {
return nil
}
Expand Down
14 changes: 14 additions & 0 deletions amount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -930,3 +930,17 @@ func TestAmount_Scan(t *testing.T) {
})
}
}

func TestAmount_ScanNonString(t *testing.T) {
var a currency.Amount
err := a.Scan(123)

wantError := "value is not a string: 123"
errStr := ""
if err != nil {
errStr = err.Error()
}
if errStr != wantError {
t.Errorf("error: got %v, want %v", errStr, wantError)
}
}

0 comments on commit cf9e73a

Please sign in to comment.