mirror of
https://github.com/shopspring/decimal.git
synced 2024-11-22 20:40:48 +01:00
Merge pull request #32 from craigjackson/null_decimal
Added NullDecimal. Changed Decimal to implement driver.Valuer. NOTE: This is potentially a breaking change as it reverts #16
This commit is contained in:
commit
c0815674f8
2 changed files with 160 additions and 12 deletions
30
decimal.go
30
decimal.go
|
@ -607,10 +607,7 @@ func (d *Decimal) Scan(value interface{}) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Value implements the driver.Valuer interface for database serialization.
|
// 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
|
return d.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -758,3 +755,28 @@ func unquoteIfQuoted(value interface{}) (string, error) {
|
||||||
}
|
}
|
||||||
return string(bytes), nil
|
return string(bytes), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NullDecimal represents a fixed-point decimal. It is immutable.
|
||||||
|
// number = value * 10 ^ exp
|
||||||
|
type NullDecimal struct {
|
||||||
|
Decimal Decimal
|
||||||
|
Valid bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// Scan implements the sql.Scanner interface for database deserialization.
|
||||||
|
func (d *NullDecimal) Scan(value interface{}) error {
|
||||||
|
if value == nil {
|
||||||
|
d.Valid = false
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
d.Valid = true
|
||||||
|
return d.Decimal.Scan(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Value implements the driver.Valuer interface for database serialization.
|
||||||
|
func (d NullDecimal) Value() (driver.Value, error) {
|
||||||
|
if !d.Valid {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
return d.Decimal.Value()
|
||||||
|
}
|
||||||
|
|
142
decimal_test.go
142
decimal_test.go
|
@ -1,6 +1,7 @@
|
||||||
package decimal
|
package decimal
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"database/sql/driver"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
"math"
|
"math"
|
||||||
|
@ -1228,19 +1229,16 @@ func TestDecimal_Scan(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDecimal_Value(t *testing.T) {
|
func TestDecimal_Value(t *testing.T) {
|
||||||
// check that nil is handled appropriately
|
// Make sure this does implement the database/sql's driver.Valuer interface
|
||||||
var decimalPtr *Decimal
|
var d Decimal
|
||||||
value, err := decimalPtr.Value()
|
if _, ok := interface{}(d).(driver.Valuer); !ok {
|
||||||
if err != nil {
|
t.Error("Decimal does not implement driver.Valuer")
|
||||||
t.Errorf("(*Decimal)(<nil>).Value() failed with message: %s", err)
|
|
||||||
} else if value != nil {
|
|
||||||
t.Errorf("%v is not nil", value)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// check that normal case is handled appropriately
|
// check that normal case is handled appropriately
|
||||||
a := New(1234, -2)
|
a := New(1234, -2)
|
||||||
expected := "12.34"
|
expected := "12.34"
|
||||||
value, err = a.Value()
|
value, err := a.Value()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Decimal(12.34).Value() failed with message: %s", err)
|
t.Errorf("Decimal(12.34).Value() failed with message: %s", err)
|
||||||
} else if value.(string) != expected {
|
} else if value.(string) != expected {
|
||||||
|
@ -1355,3 +1353,131 @@ func Benchmark_Cmp(b *testing.B) {
|
||||||
sort.Sort(decimals)
|
sort.Sort(decimals)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNullDecimal_Scan(t *testing.T) {
|
||||||
|
// test the Scan method that implements the
|
||||||
|
// sql.Scanner interface
|
||||||
|
// check for the for different type of values
|
||||||
|
// that are possible to be received from the database
|
||||||
|
// drivers
|
||||||
|
|
||||||
|
// in normal operations the db driver (sqlite at least)
|
||||||
|
// will return an int64 if you specified a numeric format
|
||||||
|
|
||||||
|
// Make sure handles nil values
|
||||||
|
a := NullDecimal{}
|
||||||
|
var dbvaluePtr interface{}
|
||||||
|
err := a.Scan(dbvaluePtr)
|
||||||
|
if err != nil {
|
||||||
|
// Scan failed... no need to test result value
|
||||||
|
t.Errorf("a.Scan(nil) failed with message: %s", err)
|
||||||
|
} else {
|
||||||
|
if a.Valid {
|
||||||
|
t.Errorf("%s is not null", a.Decimal)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
dbvalue := float64(54.33)
|
||||||
|
expected := NewFromFloat(dbvalue)
|
||||||
|
|
||||||
|
err = a.Scan(dbvalue)
|
||||||
|
if err != nil {
|
||||||
|
// Scan failed... no need to test result value
|
||||||
|
t.Errorf("a.Scan(54.33) failed with message: %s", err)
|
||||||
|
|
||||||
|
} else {
|
||||||
|
// Scan suceeded... test resulting values
|
||||||
|
if !a.Valid {
|
||||||
|
t.Errorf("%s is null", a.Decimal)
|
||||||
|
} else if !a.Decimal.Equals(expected) {
|
||||||
|
t.Errorf("%s does not equal to %s", a.Decimal, expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// at least SQLite returns an int64 when 0 is stored in the db
|
||||||
|
// and you specified a numeric format on the schema
|
||||||
|
dbvalueInt := int64(0)
|
||||||
|
expected = New(dbvalueInt, 0)
|
||||||
|
|
||||||
|
err = a.Scan(dbvalueInt)
|
||||||
|
if err != nil {
|
||||||
|
// Scan failed... no need to test result value
|
||||||
|
t.Errorf("a.Scan(0) failed with message: %s", err)
|
||||||
|
|
||||||
|
} else {
|
||||||
|
// Scan suceeded... test resulting values
|
||||||
|
if !a.Valid {
|
||||||
|
t.Errorf("%s is null", a.Decimal)
|
||||||
|
} else if !a.Decimal.Equals(expected) {
|
||||||
|
t.Errorf("%s does not equal to %s", a, expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// in case you specified a varchar in your SQL schema,
|
||||||
|
// the database driver will return byte slice []byte
|
||||||
|
valueStr := "535.666"
|
||||||
|
dbvalueStr := []byte(valueStr)
|
||||||
|
expected, err = NewFromString(valueStr)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = a.Scan(dbvalueStr)
|
||||||
|
if err != nil {
|
||||||
|
// Scan failed... no need to test result value
|
||||||
|
t.Errorf("a.Scan('535.666') failed with message: %s", err)
|
||||||
|
|
||||||
|
} else {
|
||||||
|
// Scan suceeded... test resulting values
|
||||||
|
if !a.Valid {
|
||||||
|
t.Errorf("%s is null", a.Decimal)
|
||||||
|
} else if !a.Decimal.Equals(expected) {
|
||||||
|
t.Errorf("%s does not equal to %s", a, expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// lib/pq can also return strings
|
||||||
|
expected, err = NewFromString(valueStr)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = a.Scan(valueStr)
|
||||||
|
if err != nil {
|
||||||
|
// Scan failed... no need to test result value
|
||||||
|
t.Errorf("a.Scan('535.666') failed with message: %s", err)
|
||||||
|
} else {
|
||||||
|
// Scan suceeded... test resulting values
|
||||||
|
if !a.Valid {
|
||||||
|
t.Errorf("%s is null", a.Decimal)
|
||||||
|
} else if !a.Decimal.Equals(expected) {
|
||||||
|
t.Errorf("%s does not equal to %s", a, expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNullDecimal_Value(t *testing.T) {
|
||||||
|
// Make sure this does implement the database/sql's driver.Valuer interface
|
||||||
|
var nullDecimal NullDecimal
|
||||||
|
if _, ok := interface{}(nullDecimal).(driver.Valuer); !ok {
|
||||||
|
t.Error("NullDecimal does not implement driver.Valuer")
|
||||||
|
}
|
||||||
|
|
||||||
|
// check that null is handled appropriately
|
||||||
|
value, err := nullDecimal.Value()
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("NullDecimal{}.Valid() failed with message: %s", err)
|
||||||
|
} else if value != nil {
|
||||||
|
t.Errorf("%v is not nil", value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// check that normal case is handled appropriately
|
||||||
|
a := NullDecimal{Decimal: New(1234, -2), Valid: true}
|
||||||
|
expected := "12.34"
|
||||||
|
value, err = a.Value()
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("NullDecimal(12.34).Value() failed with message: %s", err)
|
||||||
|
} else if value.(string) != expected {
|
||||||
|
t.Errorf("%s does not equal to %s", a, expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue