forked from Rhymond/go-money
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
54 lines (49 loc) · 1.08 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
package money
import (
"database/sql/driver"
"encoding/json"
"errors"
)
// Scan is an implementation the database/sql scanner interface
func (c *Currency) Scan(value interface{}) error {
if value == nil {
return nil
}
data, ok := value.(string)
if !ok {
return errors.New("type assertion .(string) failed.")
}
*c = *newCurrency(data).get()
return nil
}
// Value is an implementation of driver.Value
func (c *Currency) Value() (driver.Value, error) {
return c.Code, nil
}
// Scan is an implementation the database/sql scanner interface
func (m *Money) Scan(value interface{}) error {
if value == nil {
return nil
}
data, ok := value.([]byte)
if !ok {
return errors.New("type assertion .([]byte) failed.")
}
nm := &Money{}
err := json.Unmarshal(data, &nm)
if nm == nil || err != nil {
return err
}
if nm.Currency_ == nil || nm.Currency_.Code == "" {
return errors.New("invalid currency")
}
*m = *nm
return nil
}
// Value is an implementation of driver.Value
func (m *Money) Value() (driver.Value, error) {
if m == nil {
return nil, nil
}
return json.Marshal(m)
}