Skip to content

Commit

Permalink
fix panic with zero value of types.NullDecimal (#1073)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenafamo authored Jan 28, 2022
1 parent e349d30 commit df45dec
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
10 changes: 7 additions & 3 deletions types/decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,13 @@ func randomDecimal(nextInt func() int64, fieldType string, shouldBeNull bool) *d
return random
}

func decimalValue(d *decimal.Big, canNil bool) (driver.Value, error) {
if !canNil && d == nil {
return nil, nil
func decimalValue(d *decimal.Big, canNull bool) (driver.Value, error) {
if d == nil {
if canNull {
return nil, nil
}

return "0", nil
}

if d.IsNaN(0) {
Expand Down
4 changes: 4 additions & 0 deletions types/decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ func TestNullDecimal_Value(t *testing.T) {
}
}

zero := NullDecimal{}
if _, err := zero.Value(); err != nil {
t.Error("zero value should not error")
}
infinity := NullDecimal{Big: new(decimal.Big).SetInf(true)}
if _, err := infinity.Value(); err == nil {
t.Error("infinity should not be passed into the database")
Expand Down

0 comments on commit df45dec

Please sign in to comment.