forked from Rhymond/go-money
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
107 lines (89 loc) · 3.21 KB
/
db.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package money
import (
"database/sql/driver"
"fmt"
"strings"
"github.com/shopspring/decimal"
"github.com/spf13/cast"
)
var (
// DBMoneyValueSeparator is used to join together the Amount and Currency components of money.Money instances
// allowing them to be stored as strings (via the driver.Valuer interface) and unmarshalled as strings (via
// the sql.Scanner interface); set this value to use a different separator.
DBMoneyValueSeparator = DefaultDBMoneyValueSeparator
)
const (
// DefaultDBMoneyValueSeparator is the default value for DBMoneyValueSeparator; can be used to reset the
// active separator value
DefaultDBMoneyValueSeparator = "|"
)
// Value implements driver.Valuer to serialise a Money instance into a delimited string using the DBMoneyValueSeparator
// for example: "amount|currency_code"
func (m *Money) Value() (driver.Value, error) {
code := ""
if m.currency != nil {
code = m.currency.Code
}
fraction := int32(2)
if m.currency != nil {
fraction = m.currency.Fraction
}
return fmt.Sprintf("%."+cast.ToString(fraction)+"f%s%s", m.Amount(), DBMoneyValueSeparator, code), nil
}
// Scan implements sql.Scanner to deserialize a Money instance from a DBMoneyValueSeparator-separated string
// for example: "amount|currency_code"
func (m *Money) Scan(src interface{}) error {
var amount Amount
currency := &Currency{}
// let's support string and int64
switch src := src.(type) {
case string:
parts := strings.Split(src, DBMoneyValueSeparator)
if len(parts) != 2 || parts[0] == "" {
return fmt.Errorf("%#v is not valid to scan into Money; update your query to return a money.DBMoneyValueSeparator-separated pair of \"amount%scurrency_code\"", src, DBMoneyValueSeparator)
}
_amount := cast.ToFloat64(parts[0])
amount = decimal.NewFromFloat(_amount)
if amount.IsZero() && parts[1] == "" {
*m = Money{
amount: amount,
}
return nil
}
if parts[1] == "" {
return fmt.Errorf("%#v is not valid to scan into Money; update your query to return a money.DBMoneyValueSeparator-separated pair of \"amount%scurrency_code\"", src, DBMoneyValueSeparator)
}
if err := currency.Scan(parts[1]); err != nil {
return fmt.Errorf("scanning %#v into a Currency: %v", parts[1], err)
}
default:
return fmt.Errorf("don't know how to scan %T into Money; update your query to return a money.DBMoneyValueSeparator-separated pair of \"amount%scurrency_code\"", src, DBMoneyValueSeparator)
}
// allocate new Money with the scanned amount and currency
*m = Money{
amount: amount,
currency: currency,
}
return nil
}
// Value implements driver.Valuer to serialize a Currency code into a string for saving to a database
func (c Currency) Value() (driver.Value, error) {
return c.Code, nil
}
// Scan implements sql.Scanner to deserialize a Currency from a string value read from a database
func (c *Currency) Scan(src interface{}) error {
var val *Currency
// let's support string only
switch src := src.(type) {
case string:
val = GetCurrency(src)
default:
return fmt.Errorf("%T is not a supported type for a Currency (store the Currency.Code value as a string only)", src)
}
if val == nil {
return fmt.Errorf("GetCurrency(%#v) returned nil", src)
}
// copy the value
*c = *val
return nil
}