-
Notifications
You must be signed in to change notification settings - Fork 1
/
uuid.go
188 lines (161 loc) · 3.73 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
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
package uuid
import (
"database/sql/driver"
"encoding/hex"
"fmt"
"github.com/gofrs/uuid/v5"
)
// UUIDSize is the size of the uuid
const UUIDSize = 16
var byteGroups = []int{8, 4, 4, 4, 12}
// OrderedUUID is an UUIDv1 with some bit shifted for database index performance
type UUID struct {
Bytes [UUIDSize]byte
Valid bool
}
func NewV1() UUID {
v1, err := uuid.NewV1()
if err != nil {
return UUID{
Valid: false,
}
}
u := UUID{
Valid: true,
}
copy(u.Bytes[0:], v1[0:])
return u
}
func NewV4() UUID {
v4, err := uuid.NewV4()
if err != nil {
return UUID{
Valid: false,
}
}
u := UUID{
Valid: true,
}
copy(u.Bytes[0:], v4[0:])
return u
}
// String returns the string representation of the UUID
func (u *UUID) String() string {
buf := make([]byte, 36)
hex.Encode(buf[0:8], u.Bytes[0:4])
buf[8] = '-'
hex.Encode(buf[9:13], u.Bytes[4:6])
buf[13] = '-'
hex.Encode(buf[14:18], u.Bytes[6:8])
buf[18] = '-'
hex.Encode(buf[19:23], u.Bytes[8:10])
buf[23] = '-'
hex.Encode(buf[24:], u.Bytes[10:])
return string(buf)
}
// Value implements the driver.Valuer interface.
func (u UUID) Value() (driver.Value, error) {
if !u.Valid {
return nil, nil
}
v := u.Bytes[0:16]
return v, nil
}
// Scan implements the sql.Scanner interface.
func (u *UUID) Scan(src interface{}) error {
u.Valid = false
if src == nil {
return nil
}
switch src := src.(type) {
case []byte:
if len(src) != UUIDSize {
return fmt.Errorf("uuid: UUID must be exactly 16 bytes long, got %d bytes", len(src))
}
copy(u.Bytes[0:], src[0:])
u.Valid = true
return nil
}
return fmt.Errorf("uuid: cannot convert %T to UUID", src)
}
// UnmarshalText implements the encoding.TextUnmarshaler interface.
// Following formats are supported:
//
// "6ba7b810-9dad-11d1-80b4-00c04fd430c8",
// "6ba7b8109dad11d180b400c04fd430c8"
//
// Supported UUID text representation follows:
//
// uuid := canonical | hashlike
// plain := canonical | hashlike
// canonical := 4hexoct '-' 2hexoct '-' 2hexoct '-' 6hexoct
// hashlike := 12hexoct
func (u *UUID) UnmarshalText(text []byte) (err error) {
switch len(text) {
case 0:
u.Valid = false
u.Bytes = [16]byte{}
return nil
case 32:
return u.decodeHashLike(text)
case 36:
return u.decodeCanonical(text)
default:
return fmt.Errorf("uuid: incorrect UUID length: %s", text)
}
}
// decodeCanonical decodes UUID string in format
// "6ba7b810-9dad-11d1-80b4-00c04fd430c8".
func (u *UUID) decodeCanonical(t []byte) (err error) {
if t[8] != '-' || t[13] != '-' || t[18] != '-' || t[23] != '-' {
return fmt.Errorf("uuid: incorrect UUID format %s", t)
}
src := t[:]
dst := u.Bytes[:]
for i, byteGroup := range byteGroups {
if i > 0 {
src = src[1:] // skip dash
}
_, err = hex.Decode(dst[:byteGroup/2], src[:byteGroup])
if err != nil {
return
}
src = src[byteGroup:]
dst = dst[byteGroup/2:]
}
u.Valid = true
return
}
// decodeHashLike decodes UUID string in format
// "6ba7b8109dad11d180b400c04fd430c8".
func (u *UUID) decodeHashLike(t []byte) (err error) {
src := t[:]
dst := u.Bytes[:]
if _, err = hex.Decode(dst, src); err != nil {
return err
}
u.Valid = true
return
}
// MarshalText implements the encoding.TextMarshaler interface.
// The encoding is the same as returned by String.
func (u UUID) MarshalText() (text []byte, err error) {
if !u.Valid {
return nil, nil
}
text = []byte(u.String())
return
}
// FromString returns an OrderedUUID parsed from string input.
// Input is expected in a form accepted by UnmarshalText.
func FromString(input string) (u UUID, err error) {
err = u.UnmarshalText([]byte(input))
return
}
// FromBytes returns an OrderedUUID parsed from the byte array.
func FromBytes(input [16]byte) UUID {
u := UUID{}
copy(u.Bytes[0:], input[0:])
u.Valid = true
return u
}