-
Notifications
You must be signed in to change notification settings - Fork 1
/
nullableid.go
375 lines (333 loc) · 9.15 KB
/
nullableid.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package uu
import (
"bytes"
"database/sql"
"database/sql/driver"
"encoding/json"
"fmt"
"io"
"reflect"
"strings"
)
var IDNull NullableID
// NullableID is a UUID where the Nil UUID "00000000-0000-0000-0000-000000000000"
// is interpreted as the null values for SQL and JSON.
type NullableID [16]byte
// NullableIDFromString parses a string as NullableID.
// The Nil UUID "00000000-0000-0000-0000-000000000000"
// is interpreted as NULL.
func NullableIDFromString(s string) (NullableID, error) {
id, err := IDFromString(s)
if err != nil {
return IDNull, err
}
return NullableID(id), nil
}
// NullableIDFromStringOrNull parses a string as UUID,
// or returns IDNull in case of a parsing error.
// The Nil UUID "00000000-0000-0000-0000-000000000000"
// is interpreted as NULL.
func NullableIDFromStringOrNull(s string) NullableID {
id, err := IDFromString(s)
if err != nil {
return IDNull
}
return NullableID(id)
}
// NullableIDFromBytes parses a byte slice as UUID.
// The Nil UUID "00000000-0000-0000-0000-000000000000"
// is interpreted as NULL.
func NullableIDFromBytes(s []byte) (NullableID, error) {
id, err := IDFromBytes(s)
if err != nil {
return IDNull, err
}
return NullableID(id), nil
}
// NullableIDFromPtr returns the passed ID as NullableID
// if the ptr is not nil, or returns IDNull in case of a nil ptr.
// The Nil UUID "00000000-0000-0000-0000-000000000000"
// is interpreted as NULL.
func NullableIDFromPtr(ptr *ID) NullableID {
if ptr == nil {
return IDNull
}
return NullableID(*ptr)
}
// NullableIDFromAny converts val to an ID or returns an error
// if the conversion is not possible or the ID is not valid.
// Returns IDNull and no error when val is nil.
// The Nil UUID "00000000-0000-0000-0000-000000000000"
// is interpreted as NULL.
func NullableIDFromAny(val any) (NullableID, error) {
switch x := val.(type) {
case string:
return NullableIDFromString(x)
case []byte:
return NullableIDFromBytes(x)
case ID:
return NullableID(x), NullableID(x).Validate()
case NullableID:
return x, x.Validate()
case [16]byte:
return NullableID(x), NullableID(x).Validate()
case nil:
return IDNull, nil
default:
return IDNull, fmt.Errorf("uu.NullableIDFromAny type not supported: %T", val)
}
}
// NullableIDMust converts val to an ID or panics
// if that's not possible or the ID is not valid.
// Supported types are string, []byte, [16]byte,
// ID, NullableID, and nil.
// The Nil UUID "00000000-0000-0000-0000-000000000000"
// is interpreted as NULL.
func NullableIDMust[T IDSource](val T) NullableID {
switch x := any(val).(type) {
case string:
id, err := NullableIDFromString(x)
if err != nil {
panic(err)
}
return id
case []byte:
id, err := NullableIDFromBytes(x)
if err != nil {
panic(err)
}
return id
case ID:
if err := x.Validate(); err != nil {
panic(err)
}
return x.Nullable()
case NullableID:
if err := x.Validate(); err != nil {
panic(err)
}
return x
case [16]byte:
if err := NullableID(x).Validate(); err != nil {
panic(err)
}
return NullableID(x)
default:
panic(fmt.Errorf("uu.NullableIDMust type not supported: %T", val))
}
}
// Version returns algorithm version used to generate UUID.
func (n NullableID) Version() uint {
return ID(n).Version()
}
// Variant returns an ID layout variant or IDVariantInvalid if unknown.
func (n NullableID) Variant() uint {
return ID(n).Variant()
}
// Valid returns if Variant and Version of this UUID are supported.
// A Nil UUID is also valid.
func (n NullableID) Valid() bool {
return n == IDNull || ID(n).Valid()
}
// Validate returns an error if the Variant and Version of this UUID are not supported.
// A Nil UUID is also valid.
func (n NullableID) Validate() error {
if n == IDNull {
return nil
}
if v := n.Version(); v < 1 || v > 5 {
return ErrInvalidVersion(v)
}
if n.Variant() == IDVariantInvalid {
return ErrInvalidVariant
}
return nil
}
// Set sets an ID for this NullableID
func (n *NullableID) Set(id ID) {
*n = NullableID(id)
}
// SetNull sets the NullableID to null
func (n *NullableID) SetNull() {
*n = IDNull
}
// Get returns the non nullable ID value
// or panics if the NullableID is null.
// Note: check with IsNull before using Get!
func (n NullableID) Get() ID {
if n.IsNull() {
panic("NULL uu.ID")
}
return ID(n)
}
// GetOr returns the non nullable ID value
// or defaultID if the NullableID is null.
func (n NullableID) GetOr(defaultID ID) ID {
if n.IsNull() {
return defaultID
}
return ID(n)
}
// GetOrNil returns the non nullable ID value
// or the Nil UUID if n is null.
// Use Get to ensure getting a non Nil UUID or panic.
func (n NullableID) GetOrNil() ID {
return ID(n)
}
// PrettyPrint the NullableID in the format xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx or as NULL.
// Implements the pretty.Printable interface.
func (n NullableID) PrettyPrint(w io.Writer) {
w.Write([]byte(n.StringOr("NULL"))) //#nosec G104 -- go-pretty does not check write errors
}
// GoString returns a pseudo Go literal for the ID in the format:
//
// uu.NullableIDMust(`xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx`)
func (n NullableID) GoString() string {
if n.IsNull() {
return "uu.NullableIDMust(nil)"
}
return "uu.NullableIDMust(`" + n.String() + "`)"
}
// Hex returns the hex representation without dashes of the UUID
// The returned string is always 32 characters long.
func (n NullableID) Hex() string {
return ID(n).Hex()
}
// Base64 returns the unpadded base64 URL encoding of the UUID.
// The returned string is always 22 characters long.
func (n NullableID) Base64() string {
return ID(n).Base64()
}
// IsNull returns true if the NullableID is null.
// IsNull implements the nullable.Nullable interface.
func (n NullableID) IsNull() bool {
return n == IDNull
}
// IsNotNull returns true if the NullableID is not null.
func (n NullableID) IsNotNull() bool {
return n != IDNull
}
// String returns the ID as string or "NULL"
func (n NullableID) String() string {
return n.StringOr("NULL")
}
// StringUpper returns the upper case version
// of the canonical string format, or "NULL".
func (n NullableID) StringUpper() string {
return strings.ToUpper(n.String())
}
// StringOr returns the ID as string or the passed nullStr
func (n NullableID) StringOr(nullStr string) string {
if n.IsNull() {
return nullStr
}
return ID(n).String()
}
// StringBytes returns the canonical string representation of the UUID as byte slice:
// xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
func (n NullableID) StringBytes() []byte {
return ID(n).StringBytes()
}
// Ptr returns a pointer to this NullableID's value, or a nil pointer if this NullableID is null.
func (n NullableID) Ptr() *ID {
if n == IDNull {
return nil
}
return (*ID)(&n)
}
// Value implements the driver.Valuer interface.
func (n NullableID) Value() (driver.Value, error) {
if n == IDNull {
return nil, nil
}
// Delegate to ID Value function
return ID(n).Value()
}
// Scan implements the sql.Scanner interface.
func (n *NullableID) Scan(src any) error {
if src == nil {
*n = IDNull
return nil
}
// Delegate to ID.Scan function
return (*ID)(n).Scan(src)
}
// UnmarshalJSON implements json.Unmarshaler.
// It supports string and null input. Blank string input does not produce a null ID.
// It also supports unmarshalling a sql.NullString.
func (n *NullableID) UnmarshalJSON(data []byte) error {
// TODO optimize
var v any
err := json.Unmarshal(data, &v)
if err != nil {
return err
}
switch x := v.(type) {
case string:
id, err := IDFromString(x)
if err != nil {
return err
}
*n = NullableID(id)
return err
case map[string]any:
var ns sql.NullString
err = json.Unmarshal(data, &ns)
if err != nil || !ns.Valid {
return err
}
id, err := IDFromString(ns.String)
if err != nil {
return err
}
*n = NullableID(id)
return err
case nil:
*n = IDNull
return nil
default:
return fmt.Errorf("cannot UnmarshalJSON(%s) as uu.NullableID", reflect.TypeOf(v))
}
}
// MarshalJSON implements json.Marshaler.
func (n NullableID) MarshalJSON() ([]byte, error) {
if n == IDNull {
return []byte("null"), nil
}
b := make([]byte, 1, 38)
b[0] = '"'
b = append(b, n.StringBytes()...)
b = append(b, '"')
return b, nil
}
// MarshalText implements encoding.TextMarshaler.
// It will encode a blank string when this String is null.
func (n NullableID) MarshalText() ([]byte, error) {
if n == IDNull {
return []byte{}, nil
}
return ID(n).MarshalText()
}
// UnmarshalText implements encoding.TextUnmarshaler.
// It will unmarshal to a null String if the input is a blank string.
func (n *NullableID) UnmarshalText(text []byte) (err error) {
if len(text) == 0 {
*n = IDNull
return nil
}
return (*ID)(n).UnmarshalText(text)
}
// MarshalBinary implements the encoding.BinaryMarshaler interface.
func (n NullableID) MarshalBinary() (data []byte, err error) {
return ID(n).MarshalBinary()
}
// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
// It will return error if the slice isn't 16 bytes long,
// but does not check the validity of the UUID.
func (n *NullableID) UnmarshalBinary(data []byte) (err error) {
return (*ID)(n).UnmarshalBinary(data)
}
// NullableIDCompare returns bytes.Compare result of a and b.
func NullableIDCompare(a, b NullableID) int {
return bytes.Compare(a[:], b[:])
}