-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfloat64.go
74 lines (66 loc) · 1.21 KB
/
float64.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
package pgnull
import (
"database/sql"
"database/sql/driver"
"encoding/json"
"strconv"
)
type NullFloat sql.NullFloat64
func (f NullFloat) MarshalJSON() ([]byte, error) {
if f.Valid {
return json.Marshal(f.Float64)
}
return json.Marshal(nil)
}
func (f *NullFloat) UnmarshalJSON(bt []byte) error {
xyz := string(bt)
if xyz == "null" {
f.Float64 = 0
f.Valid = false
return nil
}
v, err := strconv.ParseFloat(xyz, 64)
if err != nil {
f.Float64 = 0
f.Valid = false
return err
}
f.Float64 = v
f.Valid = true
return nil
}
func (f *NullFloat) Scan(value interface{}) error {
switch v := value.(type) {
case float64:
f.Float64 = v
f.Valid = true
case float32:
f.Float64 = float64(v)
f.Valid = true
case int64:
f.Float64 = float64(v)
f.Valid = true
case int32:
f.Float64 = float64(v)
f.Valid = true
case int:
f.Float64 = float64(v)
f.Valid = true
}
return nil
}
func (f NullFloat) Value() (driver.Value, error) {
if !f.Valid {
return nil, nil
}
return f.Float64, nil
}
func NewNullFloat(a float64) NullFloat {
return NullFloat{a, true}
}
func NullFloatIsEqual(a, b NullFloat) bool {
if !a.Valid || !b.Valid {
return a.Valid == b.Valid
}
return a.Float64 == b.Float64
}