From 54efccb61ffe0a31b6a1908bdc8a35f491da01ea Mon Sep 17 00:00:00 2001 From: Tom Linford Date: Wed, 23 Dec 2015 16:56:58 -0800 Subject: [PATCH] Make Value take a pointer receiver --- decimal.go | 5 ++++- decimal_test.go | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/decimal.go b/decimal.go index cbbce7b..b2fc0c9 100644 --- a/decimal.go +++ b/decimal.go @@ -495,7 +495,10 @@ func (d *Decimal) Scan(value interface{}) error { } // Value implements the driver.Valuer interface for database serialization. -func (d Decimal) Value() (driver.Value, error) { +func (d *Decimal) Value() (driver.Value, error) { + if d == nil { + return nil, nil + } return d.String(), nil } diff --git a/decimal_test.go b/decimal_test.go index e560ffb..016e57b 100644 --- a/decimal_test.go +++ b/decimal_test.go @@ -874,6 +874,27 @@ func TestDecimal_Scan(t *testing.T) { } } +func TestDecimal_Value(t *testing.T) { + // check that nil is handled appropriately + var decimalPtr *Decimal + value, err := decimalPtr.Value() + if err != nil { + t.Errorf("(*Decimal)() failed with message: %s", err) + } else if value != nil { + t.Errorf("%v is not nil", value) + } + + // check that normal case is handled appropriately + a := New(1234, 2) + expected := "123400" + value, err = a.Value() + if err != nil { + t.Errorf("Decimal(123400).Value() failed with message: %s", err) + } else if value.(string) != expected { + t.Errorf("%s does not equal to %s", a, expected) + } +} + // old tests after this line func TestDecimal_Scale(t *testing.T) {