-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathint64.go
61 lines (54 loc) · 1.16 KB
/
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
package nulltypes
import (
"database/sql/driver"
"encoding/json"
)
// NullInt64 is a wrapper around int64
type NullInt64 struct {
Int64 int64
Valid bool
}
// Int64 method to get NullInt64 object from int64
func Int64(Int64 int64) NullInt64 {
return NullInt64{Int64, true}
}
// MarshalJSON method is called by json.Marshal,
// whenever it is of type NullInt64
func (ni NullInt64) MarshalJSON() ([]byte, error) {
if !ni.Valid {
return json.Marshal(nil)
}
return json.Marshal(ni.Int64)
}
// UnmarshalJSON method is called by json.Unmarshal,
// whenever it is of type NullInt64
func (ni *NullInt64) UnmarshalJSON(b []byte) error {
var i *int64
if err := json.Unmarshal(b, &i); err != nil {
return err
}
if i != nil {
ni.Valid = true
ni.Int64 = *i
} else {
ni.Valid = false
}
return nil
}
// Scan satisfies the sql.scanner interface
func (ni *NullInt64) Scan(value interface{}) error {
rt, ok := value.(int64)
if ok {
*ni = NullInt64{rt, true}
} else {
*ni = NullInt64{rt, false}
}
return nil
}
// Value satisfies the driver.Value interface
func (ni NullInt64) Value() (driver.Value, error) {
if ni.Valid {
return ni.Int64, nil
}
return nil, nil
}