-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgt_null_uint.go
255 lines (212 loc) · 5.18 KB
/
gt_null_uint.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package gt
import (
"database/sql/driver"
"encoding/json"
"strconv"
)
/*
Shortcut: parses successfully or panics. Should be used only in root scope. When
error handling is relevant, use `.Parse`.
*/
func ParseNullUint(src string) (val NullUint) {
try(val.Parse(src))
return
}
/*
Variant of `uint64` where zero value is considered empty in text, and null in
JSON and SQL. Use this for fields where 0 is not allowed, such as primary and
foreign keys, or unique bigserials.
Unlike `uint64`, encoding/decoding is not always reversible:
JSON 0 → Go 0 → JSON null
SQL 0 → Go 0 → SQL null
In your data model, positive numeric fields should be either:
* Non-nullable; zero value = 0; use `uint64`.
* Nullable; zero value = `null`; 0 is not allowed; use `gt.NullUint`.
Avoid `*uintN`.
*/
type NullUint uint64
var (
_ = Encodable(NullUint(0))
_ = Decodable((*NullUint)(nil))
)
// Implement `gt.Zeroable`. Equivalent to `reflect.ValueOf(self).IsZero()`.
func (self NullUint) IsZero() bool { return self == 0 }
// Implement `gt.Nullable`. True if zero.
func (self NullUint) IsNull() bool { return self.IsZero() }
// Implement `gt.PtrGetter`, returning `*uint64`.
func (self *NullUint) GetPtr() any { return (*uint64)(self) }
// Implement `gt.Getter`. If zero, returns `nil`, otherwise returns `uint64`.
func (self NullUint) Get() any {
if self.IsNull() {
return nil
}
return uint64(self)
}
// Implement `gt.Setter`, using `.Scan`. Panics on error.
func (self *NullUint) Set(src any) { try(self.Scan(src)) }
// Implement `gt.Zeroer`, zeroing the receiver.
func (self *NullUint) Zero() {
if self != nil {
*self = 0
}
}
/*
Implement `fmt.Stringer`. If zero, returns an empty string. Otherwise formats
using `strconv.FormatUint`.
*/
func (self NullUint) String() string {
if self.IsNull() {
return ``
}
return strconv.FormatUint(uint64(self), 10)
}
/*
Implement `gt.Parser`. If the input is empty, zeroes the receiver. Otherwise
parses the input using `strconv.ParseUint`.
*/
func (self *NullUint) Parse(src string) error {
if len(src) == 0 {
self.Zero()
return nil
}
val, err := strconv.ParseUint(src, 10, 64)
if err != nil {
return err
}
*self = NullUint(val)
return nil
}
// Implement `gt.AppenderTo`, using the same representation as `.String`.
func (self NullUint) AppendTo(buf []byte) []byte {
if self.IsNull() {
return buf
}
return strconv.AppendUint(buf, uint64(self), 10)
}
/*
Implement `encoding.TextMarhaler`. If zero, returns nil. Otherwise returns the
same representation as `.String`.
*/
func (self NullUint) MarshalText() ([]byte, error) {
if self.IsNull() {
return nil, nil
}
return self.AppendTo(nil), nil
}
// Implement `encoding.TextUnmarshaler`, using the same algorithm as `.Parse`.
func (self *NullUint) UnmarshalText(src []byte) error {
return self.Parse(bytesString(src))
}
/*
Implement `json.Marshaler`. If zero, returns bytes representing `null`.
Otherwise uses the default `json.Marshal` behavior for `uint64`.
*/
func (self NullUint) MarshalJSON() ([]byte, error) {
if self.IsNull() {
return bytesNull, nil
}
return json.Marshal(self.Get())
}
/*
Implement `json.Unmarshaler`. If the input is empty or represents JSON `null`,
zeroes the receiver. Otherwise uses the default `json.Unmarshal` behavior
for `*uint64`.
*/
func (self *NullUint) UnmarshalJSON(src []byte) error {
if isJsonEmpty(src) {
self.Zero()
return nil
}
return json.Unmarshal(src, self.GetPtr())
}
// Implement `driver.Valuer`, using `.Get`.
func (self NullUint) Value() (driver.Value, error) {
return self.Get(), nil
}
/*
Implement `sql.Scanner`, converting an arbitrary input to `gt.NullUint` and
modifying the receiver. Acceptable inputs:
* `nil` -> use `.Zero`
* `string` -> use `.Parse`
* `[]byte` -> use `.UnmarshalText`
* `uintN` -> convert and assign
* `*uintN` -> use `.Zero` or convert and assign
* `NullUint` -> assign
* `gt.Getter` -> scan underlying value
TODO also support signed ints.
*/
func (self *NullUint) Scan(src any) error {
switch src := src.(type) {
case nil:
self.Zero()
return nil
case string:
return self.Parse(src)
case []byte:
return self.UnmarshalText(src)
case uint:
*self = NullUint(src)
return nil
case *uint:
if src == nil {
self.Zero()
} else {
*self = NullUint(*src)
}
return nil
case uint8:
*self = NullUint(src)
return nil
case *uint8:
if src == nil {
self.Zero()
} else {
*self = NullUint(*src)
}
return nil
case uint16:
*self = NullUint(src)
return nil
case *uint16:
if src == nil {
self.Zero()
} else {
*self = NullUint(*src)
}
return nil
case uint32:
*self = NullUint(src)
return nil
case *uint32:
if src == nil {
self.Zero()
} else {
*self = NullUint(*src)
}
return nil
case uint64:
*self = NullUint(src)
return nil
case *uint64:
if src == nil {
self.Zero()
} else {
*self = NullUint(*src)
}
return nil
case NullUint:
*self = src
return nil
default:
val, ok := get(src)
if ok {
return self.Scan(val)
}
return errScanType(self, src)
}
}
/*
Free cast to the underlying `uint64`. Sometimes handy when this type is embedded
in a struct.
*/
func (self NullUint) Uint64() uint64 { return uint64(self) }