mirror of
https://github.com/shopspring/decimal.git
synced 2024-11-22 20:40:48 +01:00
Better implementatin with switch and support for 0
This commit is contained in:
parent
c4b5af094a
commit
9e9cba8a85
1 changed files with 20 additions and 11 deletions
21
decimal.go
21
decimal.go
|
@ -471,20 +471,29 @@ func (d Decimal) MarshalJSON() ([]byte, error) {
|
||||||
func (d *Decimal) Scan(value interface{}) error {
|
func (d *Decimal) Scan(value interface{}) error {
|
||||||
var err error
|
var err error
|
||||||
// first try to see if the data is stored in database as a Numeric datatype
|
// first try to see if the data is stored in database as a Numeric datatype
|
||||||
if val, ok := value.(float64); ok {
|
switch value.(type) {
|
||||||
*d = NewFromFloat(val)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// then try to see if it is stored as a string
|
case float64:
|
||||||
|
// numeric in sqlite3 sends us float64
|
||||||
|
*d = NewFromFloat(value.(float64))
|
||||||
|
return err
|
||||||
|
|
||||||
|
case int64:
|
||||||
|
// at least in sqlite3 when the value is 0 in db, the data is sent
|
||||||
|
// to us as an int64 instead of a float64 ...
|
||||||
|
*d = NewFromFloat(float64(value.(int64)))
|
||||||
|
return err
|
||||||
|
|
||||||
|
default:
|
||||||
|
// default is trying to interpret value stored as string
|
||||||
str, err := unquoteIfQuoted(value)
|
str, err := unquoteIfQuoted(value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
*d, err = NewFromString(str)
|
*d, err = NewFromString(str)
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 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) {
|
||||||
|
|
Loading…
Reference in a new issue