From 25f639b9c81432878ce7c5ebf1d0dd122c1e9b67 Mon Sep 17 00:00:00 2001 From: Ulises Flynn Date: Mon, 6 Feb 2017 16:53:04 -0700 Subject: [PATCH] Added code to scan null values from the database --- decimal.go | 9 ++++++++- decimal_test.go | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) 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