-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathint64.go
108 lines (93 loc) · 2.13 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package nullable
// Do not modify. Generated by nullable-generate.
import (
"bytes"
"database/sql"
"database/sql/driver"
"encoding/json"
)
// Int64 represents an int64 value that may be null.
// This type implements the Scanner interface so it
// can be used as a scan destination, similar to NullString.
// It also implements the necessary interfaces to serialize
// to and from JSON.
type Int64 struct {
Int64 int64
Valid bool
}
// Int64FromPtr returns a Int64 whose value matches ptr.
func Int64FromPtr(ptr *int64) Int64 {
var v Int64
return v.Assign(ptr)
}
// Assign the value of the pointer. If the pointer is nil,
// then then Valid is false, otherwise Valid is true.
func (n *Int64) Assign(ptr *int64) Int64 {
if ptr == nil {
n.Valid = false
n.Int64 = 0
} else {
n.Valid = true
n.Int64 = *ptr
}
return *n
}
// Ptr returns a pointer to int64. If Valid is false
// then the pointer is nil, otherwise it is non-nil.
func (n Int64) Ptr() *int64 {
if n.Valid {
v := n.Int64
return &v
}
return nil
}
// Normalized returns an Int64 that can be compared with
// another Int64 for equality.
func (n Int64) Normalized() Int64 {
if n.Valid {
return n
}
// If !Valid, then Int64 could be any value.
// Normalized value can be compared for equality.
return Int64{}
}
// Scan implements the sql.Scanner interface.
func (n *Int64) Scan(value interface{}) error {
var nt sql.NullInt64
err := nt.Scan(value)
if err != nil {
return err
}
n.Valid = nt.Valid
n.Int64 = nt.Int64
return nil
}
// Value implements the driver.Valuer interface.
func (n Int64) Value() (driver.Value, error) {
if !n.Valid {
return nil, nil
}
return n.Int64, nil
}
// MarshalJSON implements the json.Marshaler interface.
func (n Int64) MarshalJSON() ([]byte, error) {
if n.Valid {
return json.Marshal(n.Int64)
}
return []byte("null"), nil
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (n *Int64) UnmarshalJSON(p []byte) error {
if bytes.Equal(p, jsonNull) {
n.Int64 = 0
n.Valid = false
return nil
}
var v int64
if err := json.Unmarshal(p, &v); err != nil {
return err
}
n.Int64 = v
n.Valid = true
return nil
}