-
Notifications
You must be signed in to change notification settings - Fork 0
/
decimal.go
81 lines (60 loc) · 1.54 KB
/
decimal.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package repos
import (
"github.com/cockroachdb/apd/v3"
)
// Decimal is an exact decimal type that knows
// how to marshal/unmarshal from JSON and DB.
//
// It supports arbitrary decimal precision. Implemented on top of cockroachdb's Decimal,
// with additions to support gob encode/decode.
type Decimal struct {
*apd.NullDecimal
}
func NewDecimal(coef int64, exponent int32) *Decimal {
return &Decimal{NullDecimal: &apd.NullDecimal{Decimal: *apd.New(coef, exponent), Valid: true}}
}
func (d Decimal) MarshalJSON() ([]byte, error) {
return d.Decimal.MarshalText()
}
func (d *Decimal) UnmarshalJSON(data []byte) error {
return d.UnmarshalText(data)
}
func (d *Decimal) UnmarshalText(data []byte) error {
if d == nil || data == nil {
d.NullDecimal = &apd.NullDecimal{}
return nil
}
if d.NullDecimal == nil {
b := apd.New(0, 2)
if err := b.UnmarshalText(data); err != nil {
return err
}
d.NullDecimal = &apd.NullDecimal{Decimal: *b, Valid: true}
return nil
}
if err := d.Decimal.UnmarshalText(data); err != nil {
return err
}
d.Valid = true
return nil
}
func (d *Decimal) GobDecode(data []byte) error {
return d.UnmarshalText(data)
}
func (d Decimal) GobEncode() ([]byte, error) {
if d.NullDecimal == nil || !d.NullDecimal.Valid {
return []byte{}, nil
}
return d.Decimal.MarshalText()
}
func (d *Decimal) Scan(value interface{}) error {
if d.NullDecimal == nil {
b := &apd.NullDecimal{}
if err := b.Scan(value); err != nil {
return err
}
d.NullDecimal = b
return nil
}
return d.NullDecimal.Scan(value)
}