-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathint.go
54 lines (47 loc) · 860 Bytes
/
int.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 nullable
import (
"database/sql"
"encoding/json"
)
type Int64 struct {
sql.NullInt64
}
func (ni Int64) MarshalJSON() ([]byte, error) {
if !ni.Valid {
return []byte("null"), nil
}
return json.Marshal(ni.Int64)
}
func (ni *Int64) UnmarshalJSON(data []byte) error {
if string(data) == "null" {
ni.Int64 = 0
ni.Valid = false
return nil
}
err := json.Unmarshal(data, &ni.Int64)
if err == nil {
ni.Valid = true
}
return err
}
type Float64 struct {
sql.NullFloat64
}
func (nf Float64) MarshalJSON() ([]byte, error) {
if !nf.Valid {
return []byte("null"), nil
}
return json.Marshal(nf.Float64)
}
func (nf *Float64) UnmarshalJSON(data []byte) error {
if string(data) == "null" {
nf.Float64 = 0
nf.Valid = false
return nil
}
err := json.Unmarshal(data, &nf.Float64)
if err == nil {
nf.Valid = true
}
return err
}