Skip to content

Commit cf9e73a

Browse files
theFongbojanz
authored andcommitted
Check if ok when casting Scan src to string
1 parent 1a41536 commit cf9e73a

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

amount.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,10 @@ func (a Amount) Value() (driver.Value, error) {
360360
// Allows scanning amounts from a PostgreSQL composite type.
361361
func (a *Amount) Scan(src interface{}) error {
362362
// Wire format: "(9.99,USD)".
363-
input := src.(string)
363+
input, ok := src.(string)
364+
if !ok {
365+
return fmt.Errorf("value is not a string: %v", src)
366+
}
364367
if len(input) == 0 {
365368
return nil
366369
}

amount_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -930,3 +930,17 @@ func TestAmount_Scan(t *testing.T) {
930930
})
931931
}
932932
}
933+
934+
func TestAmount_ScanNonString(t *testing.T) {
935+
var a currency.Amount
936+
err := a.Scan(123)
937+
938+
wantError := "value is not a string: 123"
939+
errStr := ""
940+
if err != nil {
941+
errStr = err.Error()
942+
}
943+
if errStr != wantError {
944+
t.Errorf("error: got %v, want %v", errStr, wantError)
945+
}
946+
}

0 commit comments

Comments
 (0)