-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuuid.go
74 lines (58 loc) · 2.08 KB
/
uuid.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
package schema
import "github.com/gofrs/uuid"
type (
// UUID is an array type to represent the value of a UUID, as defined in RFC-4122.
UUID = uuid.UUID
// NullUUID can be used with the standard sql package to represent a
// UUID value that can be NULL in the database.
NullUUID = uuid.NullUUID
)
var (
// NewUUIDV1 returns a UUID based on the current timestamp and MAC address.
NewUUIDV1 = uuid.NewV1
// NewUUIDV3 returns a UUID based on the MD5 hash of the namespace UUID and name.
NewUUIDV3 = uuid.NewV3
// NewUUIDV4 returns a randomly generated UUID.
NewUUIDV4 = uuid.NewV4
// NewUUIDV5 returns a UUID based on SHA-1 hash of the namespace UUID and name.
NewUUIDV5 = uuid.NewV5
)
var (
// UUIDNil is the nil UUID, as specified in RFC-4122, that has all 128 bits set to
// zero.
UUIDNil = uuid.Nil
// UUIDFromBytes returns a UUID generated from the raw byte slice input.
// It will return an error if the slice isn't 16 bytes long.
UUIDFromBytes = uuid.FromBytes
// UUIDFromBytesOrNil returns a UUID generated from the raw byte slice input.
// Same behavior as FromBytes(), but returns uuid.Nil instead of an error.
UUIDFromBytesOrNil = uuid.FromBytesOrNil
// UUIDFromString returns a UUID parsed from the input string.
// Input is expected in a form accepted by UnmarshalText.
UUIDFromString = uuid.FromString
// UUIDFromStringOrNil returns a UUID parsed from the input string.
// Same behavior as FromString(), but returns uuid.Nil instead of an error.
UUIDFromStringOrNil = uuid.FromStringOrNil
)
// NewUUID creates a UUID
func NewUUID() UUID {
return uuid.Must(uuid.NewV4())
}
// NewNullUUID creates a new nullable uuid
func NewNullUUID(uuid UUID, valid bool) NullUUID {
return NullUUID{
UUID: uuid,
Valid: valid,
}
}
// NullUUIDFrom creates a new nullable UUID that will always be valid.
func NullUUIDFrom(uuid UUID) NullUUID {
return NewNullUUID(uuid, true)
}
// NullUUIDFromPtr creates a new nullable uuid that be null if i is nil.
func NullUUIDFromPtr(uuid *UUID) NullUUID {
if uuid == nil {
return NewNullUUID(UUIDNil, false)
}
return NewNullUUID(*uuid, true)
}