forked from guregu/null
-
Notifications
You must be signed in to change notification settings - Fork 0
/
int.go
237 lines (214 loc) · 4.97 KB
/
int.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package null
import (
"bytes"
"database/sql"
"encoding/json"
"fmt"
"strconv"
"unsafe"
"github.com/mailru/easyjson/jlexer"
"github.com/mailru/easyjson/jwriter"
)
// Int is an nullable int64.
// It does not consider zero values to be null.
// It will decode to null, not zero, if null.
type Int struct {
sql.NullInt64
}
// NewInt creates a new Int
func NewInt[T ~int64 | ~int](i T, valid bool) Int {
return Int{
NullInt64: sql.NullInt64{
Int64: int64(i),
Valid: valid,
},
}
}
// I creates a new Int that will always be valid.
func I[T ~int64 | ~int](i T) Int {
return IntFrom(i)
}
// IntFrom creates a new Int that will always be valid.
func IntFrom[T ~int64 | ~int](i T) Int {
return NewInt(i, true)
}
// IntFromPtr creates a new Int that be null if i is nil.
func IntFromPtr[T ~int64 | ~int](i *T) Int {
if i == nil {
return NewInt(0, false)
}
return NewInt(*i, true)
}
// ValueOrZero returns the inner value if valid, otherwise zero.
func (i Int) ValueOrZero() int64 {
if !i.Valid {
return 0
}
return i.Int64
}
// UnmarshalJSON implements json.Unmarshaler.
// It supports number, string, and null input.
// 0 will not be considered a null Int.
func (i *Int) UnmarshalJSON(data []byte) error {
if bytes.Equal(data, nullLiteral) || len(data) == 0 {
i.Valid = false
return nil
}
if data[0] == '{' {
// Try the struct form of Int.
type basicInt Int
var ii basicInt
if json.Unmarshal(data, &ii) == nil {
*i = Int(ii)
return nil
}
// Try a string version
var si struct {
Int64 string
Valid bool
}
if err := json.Unmarshal(data, &si); err != nil {
return err
}
i.Valid = si.Valid
if !si.Valid {
return nil
}
var err error
i.Int64, err = strconv.ParseInt(si.Int64, 10, 64)
i.Valid = (err == nil)
return err
}
if data[0] == '"' {
data = bytes.Trim(data, `"`)
}
var err error
i.Int64, err = strconv.ParseInt(*(*string)(unsafe.Pointer(&data)), 10, 64)
i.Valid = (err == nil)
return err
}
// UnmarshalEasyJSON is an easy-JSON specific decoder, that should be more efficient than the standard one.
func (i *Int) UnmarshalEasyJSON(w *jlexer.Lexer) {
if w.IsNull() {
w.Skip()
i.Valid = false
return
}
if w.IsDelim('{') {
w.Skip()
for w.Ok() && !w.IsDelim('}') {
key := w.UnsafeString()
w.WantColon()
if w.IsNull() {
w.Skip()
w.WantComma()
continue
}
switch key {
case "int64", "Int64":
// Read int from raw.
data := w.Raw()
if data[0] == '"' {
data = data[1 : len(data)-1]
}
v, err := strconv.ParseInt(*(*string)(unsafe.Pointer(&data)), 10, 64)
if err != nil {
w.AddError(&jlexer.LexerError{
Reason: err.Error(),
Data: string(data),
})
i.Int64 = 0
i.Valid = false
return
}
i.Int64 = v
i.Valid = true
case "valid", "Valid":
i.Valid = w.Bool()
}
w.WantComma()
}
return
}
// Read int from raw.
data := w.Raw()
if data[0] == '"' {
data = data[1 : len(data)-1]
}
v, err := strconv.ParseInt(*(*string)(unsafe.Pointer(&data)), 10, 64)
if err != nil {
w.AddError(&jlexer.LexerError{
Reason: err.Error(),
Data: string(data),
})
i.Int64 = 0
i.Valid = false
return
}
i.Int64 = v
i.Valid = true
}
// UnmarshalText implements encoding.TextUnmarshaler.
// It will unmarshal to a null Int if the input is blank.
// It will return an error if the input is not an integer, blank, or "null".
func (i *Int) UnmarshalText(text []byte) error {
str := string(text)
if str == "" || str == "null" {
i.Valid = false
return nil
}
var err error
i.Int64, err = strconv.ParseInt(string(text), 10, 64)
if err != nil {
return fmt.Errorf("null: couldn't unmarshal text: %w", err)
}
i.Valid = true
return nil
}
// MarshalJSON implements json.Marshaler.
// It will encode null if this Int is null.
func (i Int) MarshalJSON() ([]byte, error) {
if !i.Valid {
return nullLiteral, nil
}
return []byte(strconv.FormatInt(i.Int64, 10)), nil
}
func (i Int) MarshalEasyJSON(w *jwriter.Writer) {
if !i.Valid {
w.RawString("null")
return
}
w.Int64(i.Int64)
}
// MarshalText implements encoding.TextMarshaler.
// It will encode a blank string if this Int is null.
func (i Int) MarshalText() ([]byte, error) {
if !i.Valid {
return []byte{}, nil
}
return []byte(strconv.FormatInt(i.Int64, 10)), nil
}
// SetValid changes this Int's value and also sets it to be non-null.
func (i *Int) SetValid(n int64) {
i.Int64 = n
i.Valid = true
}
// Ptr returns a pointer to this Int's value, or a nil pointer if this Int is null.
func (i Int) Ptr() *int64 {
if !i.Valid {
return nil
}
return &i.Int64
}
// IsZero returns true for invalid Ints, for future omitempty support (Go 1.4?)
// A non-null Int with a 0 value will not be considered zero.
func (i Int) IsZero() bool {
return !i.Valid
}
func (i Int) IsDefined() bool {
return !i.IsZero()
}
// Equal returns true if both ints have the same value or are both null.
func (i Int) Equal(other Int) bool {
return i.Valid == other.Valid && (!i.Valid || i.Int64 == other.Int64)
}