Compare commits

...

10 commits

Author SHA1 Message Date
piyongcai
3e98035865
Merge 6e8c4f56de into a1bdfc355e 2024-04-20 22:17:35 +08:00
Mateusz Woś
a1bdfc355e
Release version 1.4.0 (#365) 2024-04-12 16:19:23 +02:00
Mateusz Woś
275e48eaef
Add docs section with alternative libraries (#363) 2024-04-12 15:40:04 +02:00
Philip Dubé
dd603cbbbd
Support scanning uint64 (#364) 2024-04-12 15:35:56 +02:00
13981712066
6e8c4f56de Optimize the MarshalMsgand allocate the memory only once. 2021-12-06 11:35:43 +08:00
13981712066
c7eaaded9c fix UnmarshalMsg return value. Returns the origanal data if the error occurs. 2021-12-06 09:29:46 +08:00
13981712066
5f79fa610c Change MsgPack MarshalMsg and UnmarshalMsg function's return value to unnamed. 2021-12-06 09:22:32 +08:00
13981712066
4526b4a8bd Add Test Case for MsgPack Marshal and Unmarshal 2021-12-06 09:11:14 +08:00
13981712066
879e52d70a Add MsgPack Marshal and Unmarshal Test Case 2021-12-06 09:09:53 +08:00
13981712066
6ed1468fb5 Add MsgPack Marshal and Unmarshal function
Mote: Marshal max digits is 30. it decimal.IntPart large than 30,it will be lose extra digits.
2021-11-23 16:40:57 +08:00
6 changed files with 222 additions and 76 deletions

View file

@ -1,3 +1,30 @@
## Decimal v1.4.0
#### BREAKING
- Drop support for Go version older than 1.10 [#361](https://github.com/shopspring/decimal/pull/361)
#### FEATURES
- Add implementation of natural logarithm [#339](https://github.com/shopspring/decimal/pull/339) [#357](https://github.com/shopspring/decimal/pull/357)
- Add improved implementation of power operation [#358](https://github.com/shopspring/decimal/pull/358)
- Add Compare method which forwards calls to Cmp [#346](https://github.com/shopspring/decimal/pull/346)
- Add NewFromBigRat constructor [#288](https://github.com/shopspring/decimal/pull/288)
- Add NewFromUint64 constructor [#352](https://github.com/shopspring/decimal/pull/352)
#### ENHANCEMENTS
- Migrate to Github Actions [#245](https://github.com/shopspring/decimal/pull/245) [#340](https://github.com/shopspring/decimal/pull/340)
- Fix examples for RoundDown, RoundFloor, RoundUp, and RoundCeil [#285](https://github.com/shopspring/decimal/pull/285) [#328](https://github.com/shopspring/decimal/pull/328) [#341](https://github.com/shopspring/decimal/pull/341)
- Use Godoc standard to mark deprecated Equals and StringScaled methods [#342](https://github.com/shopspring/decimal/pull/342)
- Removed unnecessary min function for RescalePair method [#265](https://github.com/shopspring/decimal/pull/265)
- Avoid reallocation of initial slice in MarshalBinary (GobEncode) [#355](https://github.com/shopspring/decimal/pull/355)
- Optimize NumDigits method [#301](https://github.com/shopspring/decimal/pull/301) [#356](https://github.com/shopspring/decimal/pull/356)
- Optimize BigInt method [#359](https://github.com/shopspring/decimal/pull/359)
- Support scanning uint64 [#131](https://github.com/shopspring/decimal/pull/131) [#364](https://github.com/shopspring/decimal/pull/364)
- Add docs section with alternative libraries [#363](https://github.com/shopspring/decimal/pull/363)
#### BUGFIXES
- Fix incorrect calculation of decimal modulo [#258](https://github.com/shopspring/decimal/pull/258) [#317](https://github.com/shopspring/decimal/pull/317)
- Allocate new(big.Int) in Copy method to deeply clone it [#278](https://github.com/shopspring/decimal/pull/278)
- Fix overflow edge case in QuoRem method [#322](https://github.com/shopspring/decimal/pull/322)
## Decimal v1.3.1 ## Decimal v1.3.1
#### ENHANCEMENTS #### ENHANCEMENTS

View file

@ -24,6 +24,11 @@ Run `go get github.com/shopspring/decimal`
Decimal library requires Go version `>=1.10` Decimal library requires Go version `>=1.10`
## Documentation
http://godoc.org/github.com/shopspring/decimal
## Usage ## Usage
```go ```go
@ -59,9 +64,16 @@ func main() {
} }
``` ```
## Documentation ## Alternative libraries
http://godoc.org/github.com/shopspring/decimal When working with decimal numbers, you might face problems this library is not perfectly suited for.
Fortunately, thanks to the wonderful community we have a dozen other libraries that you can choose from.
Explore other alternatives to find the one that best fits your needs :)
* [cockroachdb/apd](https://github.com/cockroachdb/apd) - arbitrary precision, mutable and rich API similar to `big.Int`, more performant than this library
* [alpacahq/alpacadecimal](https://github.com/alpacahq/alpacadecimal) - high performance, low precision (12 digits), fully compatible API with this library
* [govalues/decimal](https://github.com/govalues/decimal) - high performance, zero-allocation, low precision (19 digits)
* [greatcloak/decimal](https://github.com/greatcloak/decimal) - fork focusing on billing and e-commerce web application related use cases, includes out-of-the-box BSON marshaling support
## FAQ ## FAQ

View file

@ -1847,6 +1847,11 @@ func (d *Decimal) Scan(value interface{}) error {
*d = New(v, 0) *d = New(v, 0)
return nil return nil
case uint64:
// while clickhouse may send 0 in db as uint64
*d = NewFromUint64(v)
return nil
default: default:
// default is trying to interpret value stored as string // default is trying to interpret value stored as string
str, err := unquoteIfQuoted(v) str, err := unquoteIfQuoted(v)

55
decimal_magpack_test.go Normal file
View file

@ -0,0 +1,55 @@
package decimal
import (
"testing"
)
func TestMsgPack(t *testing.T) {
for _, x := range testTable {
s := x.short
// limit to 31 digits
if len(s) > 31 {
s = s[:31]
if s[30] == '.' {
s = s[:30]
}
}
// Prepare Test Decimal Data
amount, err := NewFromString(s)
if err != nil {
t.Error(err)
}
s = amount.String()
// MarshalMsg
var b []byte
out, err := amount.MarshalMsg(b)
if err != nil {
t.Errorf("error marshalMsg %s: %v", s, err)
}
// check msg type
typ := out[0] & 0xe0
if typ != 0xa0 {
t.Errorf("error marshalMsg, expected type = %b, got %b", 0xa0, typ)
}
// check msg len
sz := int(out[0] & 0x1f)
if sz != len(s) {
t.Errorf("error marshalMsg, expected size = %d, got %d", len(s), sz)
}
// UnmarshalMsg
var unmarshalAmount Decimal
_, err = unmarshalAmount.UnmarshalMsg(out)
if err != nil {
t.Errorf("error unmarshalMsg %s: %v", s, err)
} else if !unmarshalAmount.Equal(amount) {
t.Errorf("expected %s, got %s (%s, %d)",
amount.String(), unmarshalAmount.String(),
unmarshalAmount.value.String(), unmarshalAmount.exp)
}
}
}

94
decimal_msgpack.go Normal file
View file

@ -0,0 +1,94 @@
package decimal
import (
"errors"
)
var (
errShortBytes = errors.New("msgp: too few bytes left to read object")
)
// MarshalMsg implements msgp.Marshaler
// Note: limit to 31 digits, if d.IntPart size large than 31, will be lose.
func (d Decimal) MarshalMsg(b []byte) ([]byte, error) {
o := require(b, d.Msgsize())
str := d.String()
sz := len(str)
// limit to 31 digits
// note, if d.IntPart size large than 3, will be lose.
if sz > 31 {
sz = 31
// if last char is '.' then limit to 30 digits
if str[30] == '.' {
sz = 30
}
str = str[:sz]
}
o, n := ensure(o, 1+sz)
o[n] = byte(0xa0 | sz)
n++
return o[:n+copy(o[n:], str)], nil
}
// UnmarshalMsg implements msgp.Unmarshaler
func (d *Decimal) UnmarshalMsg(b []byte) ([]byte, error) {
o, err := b, errShortBytes
l := len(b)
if l < 1 {
return o, err
}
sz := int(b[0] & 0x1f)
if len(b[1:]) < sz {
return o, err
}
if *d, err = NewFromString(string(b[1 : sz+1])); err == nil {
o = b[sz:]
}
return o, err
}
// Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message
func (d Decimal) Msgsize() int {
return 32
}
// Require ensures that cap(old)-len(old) >= extra.
func require(old []byte, extra int) []byte {
l := len(old)
c := cap(old)
r := l + extra
if c >= r {
return old
} else if l == 0 {
return make([]byte, 0, extra)
}
// the new size is the greater
// of double the old capacity
// and the sum of the old length
// and the number of new bytes
// necessary.
c <<= 1
if c < r {
c = r
}
n := make([]byte, l, c)
copy(n, old)
return n
}
// ensure 'sz' extra bytes in 'b' btw len(b) and cap(b)
func ensure(b []byte, sz int) ([]byte, int) {
l := len(b)
c := cap(b)
if c-l < sz {
o := make([]byte, (2*c)+sz) // exponential growth
n := copy(o, b)
return o[:n+sz], n
}
return b[:l+sz], l
}

View file

@ -2416,104 +2416,57 @@ func TestDecimal_Max(t *testing.T) {
} }
} }
func TestDecimal_Scan(t *testing.T) { func scanHelper(t *testing.T, dbval interface{}, expected Decimal) {
// test the Scan method that implements the t.Helper()
// 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
a := Decimal{} a := Decimal{}
dbvalue := 54.33 if err := a.Scan(dbval); err != nil {
expected := NewFromFloat(dbvalue)
err := a.Scan(dbvalue)
if err != nil {
// Scan failed... no need to test result value // Scan failed... no need to test result value
t.Errorf("a.Scan(54.33) failed with message: %s", err) t.Errorf("a.Scan(%v) failed with message: %s", dbval, err)
} else if !a.Equal(expected) {
} else {
// Scan succeeded... test resulting values // Scan succeeded... test resulting values
if !a.Equal(expected) {
t.Errorf("%s does not equal to %s", a, 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
// check different types received from various database drivers
dbvalue := 54.33
expected := NewFromFloat(dbvalue)
scanHelper(t, dbvalue, expected)
// apparently MySQL 5.7.16 and returns these as float32 so we need // apparently MySQL 5.7.16 and returns these as float32 so we need
// to handle these as well // to handle these as well
dbvalueFloat32 := float32(54.33) dbvalueFloat32 := float32(54.33)
expected = NewFromFloat(float64(dbvalueFloat32)) expected = NewFromFloat(float64(dbvalueFloat32))
scanHelper(t, dbvalueFloat32, expected)
err = a.Scan(dbvalueFloat32)
if err != nil {
// Scan failed... no need to test result value
t.Errorf("a.Scan(54.33) failed with message: %s", err)
} else {
// Scan succeeded... test resulting values
if !a.Equal(expected) {
t.Errorf("%s does not equal to %s", a, expected)
}
}
// at least SQLite returns an int64 when 0 is stored in the db // at least SQLite returns an int64 when 0 is stored in the db
// and you specified a numeric format on the schema // and you specified a numeric format on the schema
dbvalueInt := int64(0) dbvalueInt := int64(0)
expected = New(dbvalueInt, 0) expected = New(dbvalueInt, 0)
scanHelper(t, dbvalueInt, expected)
err = a.Scan(dbvalueInt) // also test uint64
if err != nil { dbvalueUint64 := uint64(2)
// Scan failed... no need to test result value expected = New(2, 0)
t.Errorf("a.Scan(0) failed with message: %s", err) scanHelper(t, dbvalueUint64, expected)
} else {
// Scan succeeded... test resulting values
if !a.Equal(expected) {
t.Errorf("%s does not equal to %s", a, expected)
}
}
// in case you specified a varchar in your SQL schema, // in case you specified a varchar in your SQL schema,
// the database driver will return byte slice []byte // the database driver may return either []byte or string
valueStr := "535.666" valueStr := "535.666"
dbvalueStr := []byte(valueStr) dbvalueStr := []byte(valueStr)
expected, err = NewFromString(valueStr) expected, err := NewFromString(valueStr)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
scanHelper(t, dbvalueStr, expected)
err = a.Scan(dbvalueStr) scanHelper(t, valueStr, expected)
if err != nil {
// Scan failed... no need to test result value
t.Errorf("a.Scan('535.666') failed with message: %s", err)
} else {
// Scan succeeded... test resulting values
if !a.Equal(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 succeeded... test resulting values
if !a.Equal(expected) {
t.Errorf("%s does not equal to %s", a, expected)
}
}
type foo struct{} type foo struct{}
a := Decimal{}
err = a.Scan(foo{}) err = a.Scan(foo{})
if err == nil { if err == nil {
t.Errorf("a.Scan(Foo{}) should have thrown an error but did not") t.Errorf("a.Scan(Foo{}) should have thrown an error but did not")