Added code to scan null values from the database

This commit is contained in:
Ulises Flynn 2017-02-06 16:53:04 -07:00
parent 5471f2c322
commit 25f639b9c8
2 changed files with 23 additions and 1 deletions

View file

@ -579,7 +579,14 @@ func (d Decimal) MarshalJSON() ([]byte, error) {
// Scan implements the sql.Scanner interface for database deserialization. // Scan implements the sql.Scanner interface for database deserialization.
func (d *Decimal) Scan(value interface{}) error { 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) { switch v := value.(type) {
case float64: case float64:

View file

@ -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) { func TestDecimal_Scan(t *testing.T) {
// test the Scan method that implements the // test the Scan method that implements the
// sql.Scanner interface // sql.Scanner interface