-
Notifications
You must be signed in to change notification settings - Fork 0
/
float.go
58 lines (51 loc) · 1.22 KB
/
float.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
package btrnl
import (
"database/sql"
"database/sql/driver"
"encoding/json"
)
// NullFloat64 represents a float64 that may be null.
// NullFloat64 implements the Scanner interface so
// it can be used as a scan destination, similar to NullString.
type NullFloat64 struct {
Float64 float64
Valid bool // Valid is true if Float64 is not NULL
}
// Scan implements the Scanner interface.
func (n *NullFloat64) Scan(value interface{}) error {
if value == nil {
n.Float64, n.Valid = 0, false
return nil
}
n.Valid = true
var nz sql.NullFloat64
err := nz.Scan(value)
if err != nil {
return err
}
n.Float64 = nz.Float64
return nil
}
// Value implements the driver Valuer interface.
func (n NullFloat64) Value() (driver.Value, error) {
if !n.Valid {
return nil, nil
}
return n.Float64, nil
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (n *NullFloat64) UnmarshalJSON(bytes []byte) error {
if string(bytes) == "null" {
n.Valid = false
return nil
}
n.Valid = true
return json.Unmarshal(bytes, &n.Float64)
}
// MarshalJSON implements the json.Marshaler interface.
func (n NullFloat64) MarshalJSON() ([]byte, error) {
if !n.Valid {
return []byte("null"), nil
}
return json.Marshal(n.Float64)
}