-
Notifications
You must be signed in to change notification settings - Fork 0
/
int64.go
57 lines (45 loc) · 938 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
package nullable
import (
"bytes"
"database/sql"
"encoding/json"
)
// Int64 is a nullable int64.
type Int64 struct {
sql.NullInt64
}
// NewInt64 returns a new Int64.
func NewInt64(i int64, valid bool) Int64 {
return Int64{
sql.NullInt64{
Int64: i,
Valid: valid,
},
}
}
// NewInt64FromPtr returns a new Int64 from a pointer.
func NewInt64FromPtr(i *int64) Int64 {
if i == nil {
return NewInt64(0, false)
}
return NewInt64(*i, true)
}
// MarshalJSON implements the json.Marshaler interface.
func (n Int64) MarshalJSON() ([]byte, error) {
if !n.Valid {
return []byte("null"), nil
}
return json.Marshal(n.Int64)
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (n *Int64) UnmarshalJSON(b []byte) error {
if bytes.Equal(b, []byte("null")) {
n.Int64, n.Valid = 0, false
return nil
}
if err := json.Unmarshal(b, &n.Int64); err != nil {
return err
}
n.Valid = true
return nil
}