-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathint64.go
87 lines (75 loc) · 1.61 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
77
78
79
80
81
82
83
84
85
86
87
package nulltype
import (
"database/sql"
"database/sql/driver"
"encoding/json"
"fmt"
)
// NullInt64 is null friendly type for int64.
type NullInt64 struct {
i sql.NullInt64
}
// NullInt64Of return NullInt64 that the value is set.
func NullInt64Of(value int64) NullInt64 {
var s NullInt64
s.Set(value)
return s
}
// Valid return the value is valid. If true, it is not null value.
func (i *NullInt64) Valid() bool {
return i.i.Valid
}
// Int64Value return the value.
func (i *NullInt64) Int64Value() int64 {
return i.i.Int64
}
// Reset set nil to the value.
func (i *NullInt64) Reset() {
i.i.Int64 = 0
i.i.Valid = false
}
// Set set the value.
func (i *NullInt64) Set(value int64) *NullInt64 {
i.i.Valid = true
i.i.Int64 = value
return i
}
// Scan is a method for database/sql.
func (i *NullInt64) Scan(value interface{}) error {
return i.i.Scan(value)
}
// String return string indicated the value.
func (i NullInt64) String() string {
if !i.i.Valid {
return ""
}
return fmt.Sprint(i.i.Int64)
}
// MarshalJSON encode the value to JSON.
func (i NullInt64) MarshalJSON() ([]byte, error) {
if !i.i.Valid {
return []byte("null"), nil
}
return json.Marshal(i.i.Int64)
}
// UnmarshalJSON decode data to the value.
func (i *NullInt64) UnmarshalJSON(data []byte) error {
var value *int64
if err := json.Unmarshal(data, &value); err != nil {
return err
}
i.i.Valid = value != nil
if value == nil {
i.i.Int64 = 0
} else {
i.i.Int64 = *value
}
return nil
}
// Value implement driver.Valuer.
func (i NullInt64) Value() (driver.Value, error) {
if !i.Valid() {
return nil, nil
}
return i.i.Int64, nil
}