-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixed.go
96 lines (83 loc) · 2.14 KB
/
fixed.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
package btrnl
import (
"database/sql/driver"
"fmt"
"github.com/robaho/fixed"
)
// NullFixed represents a nullable decimal with compatibility for
// scanning null values from the database.
type NullFixed struct {
Fixed fixed.Fixed
Valid bool
}
// Scan implements the sql.Scanner interface for database deserialization.
func (d *NullFixed) Scan(value interface{}) error {
// first try to see if the data is stored in database as a Numeric datatype
d.Valid = true
switch v := value.(type) {
case float32:
d.Fixed = fixed.NewF(float64(v))
return nil
case float64:
// numeric in sqlite3 sends us float64
d.Fixed = fixed.NewF(v)
return nil
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.Fixed = fixed.NewI(v, 0)
return nil
default:
// default is trying to interpret value stored as string
str, err := unquoteIfQuoted(v)
if err != nil {
d.Valid = false
return err
}
d.Fixed, err = fixed.NewSErr(str)
if err != nil {
d.Valid = false
}
return err
}
}
// Value implements the driver.Valuer interface for database serialization.
func (d NullFixed) Value() (driver.Value, error) {
if !d.Valid {
return nil, nil
}
return d.Fixed.String(), nil
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (d *NullFixed) UnmarshalJSON(decimalBytes []byte) error {
if string(decimalBytes) == "null" {
d.Valid = false
return nil
}
d.Valid = true
return d.Fixed.UnmarshalJSON(decimalBytes)
}
// MarshalJSON implements the json.Marshaler interface.
func (d NullFixed) MarshalJSON() ([]byte, error) {
if !d.Valid {
return []byte("null"), nil
}
return d.Fixed.MarshalJSON()
}
func unquoteIfQuoted(value interface{}) (string, error) {
var bytes []byte
switch v := value.(type) {
case string:
bytes = []byte(v)
case []byte:
bytes = v
default:
return "", fmt.Errorf("Could not convert value '%+v' to byte array of type '%T'",
value, value)
}
// If the amount is quoted, strip the quotes
if len(bytes) > 2 && bytes[0] == '"' && bytes[len(bytes)-1] == '"' {
bytes = bytes[1 : len(bytes)-1]
}
return string(bytes), nil
}