-
Notifications
You must be signed in to change notification settings - Fork 0
/
int64.go
65 lines (57 loc) · 1005 Bytes
/
int64.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
package pgnull
import (
"database/sql"
"database/sql/driver"
"encoding/json"
"strconv"
)
type NullInt sql.NullInt64
func (i NullInt) MarshalJSON() ([]byte, error) {
if i.Valid {
return json.Marshal(i.Int64)
}
return json.Marshal(nil)
}
func (i *NullInt) UnmarshalJSON(bt []byte) error {
xyz := string(bt)
if xyz == "null" {
i.Int64 = 0
i.Valid = false
return nil
}
v, err := strconv.Atoi(xyz)
if err != nil {
i.Int64 = 0
i.Valid = false
return err
}
i.Int64 = int64(v)
i.Valid = true
return nil
}
func (i *NullInt) Scan(value interface{}) error {
switch v := value.(type) {
case int64:
i.Int64 = v
i.Valid = true
}
return nil
}
func (i NullInt) Value() (driver.Value, error) {
if !i.Valid {
return nil, nil
}
return i.Int64, nil
}
func NewNullInt(a int) NullInt {
return NullInt{int64(a), true}
}
func NullIntIsEqual(a, b NullInt) bool {
if !a.Valid && !b.Valid {
return true
}
if a.Valid != b.Valid {
return false
}
return a.Int64 == b.Int64
}