diff --git a/decimal.go b/decimal.go index 3942b3d..dc8ce45 100644 --- a/decimal.go +++ b/decimal.go @@ -579,7 +579,14 @@ func (d Decimal) MarshalJSON() ([]byte, error) { // Scan implements the sql.Scanner interface for database deserialization. func (d *Decimal) Scan(value interface{}) error { - // first try to see if the data is stored in database as a Numeric datatype + // check to see if we have a nil value, return a zero value and exp + if value == nil { + d.value = zeroInt + d.exp = 0 + return nil + } + + // try to see if the data is stored in database as a Numeric datatype switch v := value.(type) { case float64: diff --git a/decimal_test.go b/decimal_test.go index 718943a..6ff162e 100644 --- a/decimal_test.go +++ b/decimal_test.go @@ -1117,6 +1117,21 @@ func TestDecimal_Max(t *testing.T) { } } +func TestDecimal_ScanNil(t *testing.T) { + a := Decimal{} + expected := Decimal{ + value: zeroInt, + exp: 0, + } + err := a.Scan(nil) + if err != nil { + t.Error(err) + } + if !a.Equal(expected) { + t.Errorf("%s does not equal to %s", a, expected) + } +} + func TestDecimal_Scan(t *testing.T) { // test the Scan method that implements the // sql.Scanner interface