-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
int64.go
76 lines (67 loc) · 1.65 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package nulls
import (
"database/sql"
"database/sql/driver"
"encoding/json"
"strconv"
)
// Int64 replaces sql.Int64 with an implementation
// that supports proper JSON encoding/decoding.
type Int64 sql.NullInt64
// Interface implements the nullable interface. It returns nil if
// the int64 is not valid, otherwise it returns the int64 value.
func (ns Int64) Interface() interface{} {
if !ns.Valid {
return nil
}
return ns.Int64
}
// NewInt64 returns a new, properly instantiated
// Int64 object.
func NewInt64(i int64) Int64 {
return Int64{Int64: i, Valid: true}
}
// Scan implements the Scanner interface.
func (ns *Int64) Scan(value interface{}) error {
n := sql.NullInt64{Int64: ns.Int64}
err := n.Scan(value)
ns.Int64, ns.Valid = n.Int64, n.Valid
return err
}
// Value implements the driver Valuer interface.
func (ns Int64) Value() (driver.Value, error) {
if !ns.Valid {
return nil, nil
}
return ns.Int64, nil
}
// MarshalJSON marshals the underlying value to a
// proper JSON representation.
func (ns Int64) MarshalJSON() ([]byte, error) {
if ns.Valid {
return json.Marshal(ns.Int64)
}
return json.Marshal(nil)
}
// UnmarshalJSON will unmarshal a JSON value into
// the propert representation of that value.
func (ns *Int64) UnmarshalJSON(text []byte) error {
t := string(text)
ns.Valid = true
if t == "null" {
ns.Valid = false
return nil
}
i, err := strconv.ParseInt(t, 10, 64)
if err != nil {
ns.Valid = false
return err
}
ns.Int64 = i
return nil
}
// UnmarshalText will unmarshal text value into
// the propert representation of that value.
func (ns *Int64) UnmarshalText(text []byte) error {
return ns.UnmarshalJSON(text)
}