Skip to content

Commit df45dec

Browse files
authored
fix panic with zero value of types.NullDecimal (#1073)
1 parent e349d30 commit df45dec

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

types/decimal.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,13 @@ func randomDecimal(nextInt func() int64, fieldType string, shouldBeNull bool) *d
167167
return random
168168
}
169169

170-
func decimalValue(d *decimal.Big, canNil bool) (driver.Value, error) {
171-
if !canNil && d == nil {
172-
return nil, nil
170+
func decimalValue(d *decimal.Big, canNull bool) (driver.Value, error) {
171+
if d == nil {
172+
if canNull {
173+
return nil, nil
174+
}
175+
176+
return "0", nil
173177
}
174178

175179
if d.IsNaN(0) {

types/decimal_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ func TestNullDecimal_Value(t *testing.T) {
105105
}
106106
}
107107

108+
zero := NullDecimal{}
109+
if _, err := zero.Value(); err != nil {
110+
t.Error("zero value should not error")
111+
}
108112
infinity := NullDecimal{Big: new(decimal.Big).SetInf(true)}
109113
if _, err := infinity.Value(); err == nil {
110114
t.Error("infinity should not be passed into the database")

0 commit comments

Comments
 (0)