-
Notifications
You must be signed in to change notification settings - Fork 0
/
uuid.go
307 lines (259 loc) · 6.89 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
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
// Copyright (c) 2012 The gocql Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
// The uuid package can be used to generate and parse universally unique
// identifiers, a standardized format in the form of a 128 bit number.
//
// http://tools.ietf.org/html/rfc4122
//
package uuid
import (
"database/sql/driver"
"errors"
"fmt"
"strings"
"sync/atomic"
"time"
"github.com/bww/go-util/v1/rand"
"github.com/bww/go-util/v1/sort"
)
type UUID [16]byte
var Zero UUID = [16]byte{}
var clockSeq uint32
const (
VariantNCSCompat = 0
VariantIETF = 2
VariantMicrosoft = 6
VariantFuture = 7
)
func init() {
var clockSeqRand [2]byte
rand.ReadRandom(clockSeqRand[:])
clockSeq = uint32(clockSeqRand[1])<<8 | uint32(clockSeqRand[0])
}
func New() UUID {
return Random()
}
func Parse(input string) (UUID, error) {
var u UUID
j := 0
for _, r := range input {
switch {
case r == '-' && j&1 == 0:
continue
case r >= '0' && r <= '9' && j < 32:
u[j/2] |= byte(r-'0') << uint(4-j&1*4)
case r >= 'a' && r <= 'f' && j < 32:
u[j/2] |= byte(r-'a'+10) << uint(4-j&1*4)
case r >= 'A' && r <= 'F' && j < 32:
u[j/2] |= byte(r-'A'+10) << uint(4-j&1*4)
default:
return UUID{}, fmt.Errorf("invalid UUID %q", input)
}
j += 1
}
if j != 32 {
return UUID{}, fmt.Errorf("invalid UUID %q", input)
}
return u, nil
}
func FromBytes(input []byte) (UUID, error) {
var u UUID
if len(input) != 16 {
return u, errors.New("UUIDs must be exactly 16 bytes long")
}
copy(u[:], input)
return u, nil
}
func Random() UUID {
var u UUID
rand.ReadRandom(u[:])
u[6] &= 0x0F // clear version
u[6] |= 0x40 // set version to 4 (random uuid)
u[8] &= 0x3F // clear variant
u[8] |= 0x80 // set to IETF variant
return u
}
var timeBase = time.Date(1582, time.October, 15, 0, 0, 0, 0, time.UTC).Unix()
func Time() UUID {
return FromTime(time.Now())
}
// UUIDBaseFromTime generates a new time based UUID (version 1) in the same
// manner as UUIDFromTime with the exception that the clock sequence component
// is zeroed. This allows for the construction of a time UUID that represents
// the lower bound of any other UUID that could be generated for the same time.
func BaseFromTime(aTime time.Time) UUID {
var u UUID
utcTime := aTime.In(time.UTC)
t := uint64(utcTime.Unix()-timeBase)*10000000 + uint64(utcTime.Nanosecond()/100)
u[0], u[1], u[2], u[3] = byte(t>>24), byte(t>>16), byte(t>>8), byte(t)
u[4], u[5] = byte(t>>40), byte(t>>32)
u[6], u[7] = byte(t>>56)&0x0F, byte(t>>48)
u[8] = byte(0)
u[9] = byte(0)
copy(u[10:], rand.HardwareAddr())
u[6] |= 0x10 // set version to 1 (time based uuid)
u[8] &= 0x3F // clear variant
u[8] |= 0x80 // set to IETF variant
return u
}
// UUIDFromTime generates a new time based UUID (version 1) as described in
// RFC 4122. This UUID contains the MAC address of the node that generated
// the UUID, the given timestamp and a sequence number.
func FromTime(aTime time.Time) UUID {
var u UUID
utcTime := aTime.In(time.UTC)
t := uint64(utcTime.Unix()-timeBase)*10000000 + uint64(utcTime.Nanosecond()/100)
u[0], u[1], u[2], u[3] = byte(t>>24), byte(t>>16), byte(t>>8), byte(t)
u[4], u[5] = byte(t>>40), byte(t>>32)
u[6], u[7] = byte(t>>56)&0x0F, byte(t>>48)
clock := atomic.AddUint32(&clockSeq, 1)
u[8] = byte(clock >> 8)
u[9] = byte(clock)
copy(u[10:], rand.HardwareAddr())
u[6] |= 0x10 // set version to 1 (time based uuid)
u[8] &= 0x3F // clear variant
u[8] |= 0x80 // set to IETF variant
return u
}
// Does this UUID represent the zero value
func (u UUID) IsZero() bool {
return u == Zero
}
// String returns the UUID in it's canonical form, a 32 digit hexadecimal
// number in the form of xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.
func (u UUID) String() string {
var offsets = [...]int{0, 2, 4, 6, 9, 11, 14, 16, 19, 21, 24, 26, 28, 30, 32, 34}
const hexString = "0123456789abcdef"
r := make([]byte, 36)
for i, b := range u {
r[offsets[i]] = hexString[b>>4]
r[offsets[i]+1] = hexString[b&0xF]
}
r[8], r[13], r[18], r[23] = '-', '-', '-', '-'
return string(r)
}
// Lexically compare this UUID to another UUID
func (u UUID) Compare(c sort.Comparable) int {
z := c.(UUID)
for i := 0; i < len(u); i++ {
a, b := u[i], z[i]
if a != b {
return int(a) - int(b)
}
}
return 0
}
// Bytes returns the raw byte slice for this UUID. A UUID is always 128 bits (16 bytes) long.
func (u UUID) Bytes() []byte {
return u[:]
}
// Variant returns the variant of this UUID. This package will only generate
// UUIDs in the IETF variant.
func (u UUID) Variant() int {
x := u[8]
if x&0x80 == 0 {
return VariantNCSCompat
} else if x&0x40 == 0 {
return VariantIETF
} else if x&0x20 == 0 {
return VariantMicrosoft
} else {
return VariantFuture
}
}
// Version extracts the version of this UUID variant. The RFC 4122 describes
// five kinds of UUIDs.
func (u UUID) Version() int {
return int(u[6] & 0xF0 >> 4)
}
// Node extracts the MAC address of the node who generated this UUID. It will
// return nil if the UUID is not a time based UUID (version 1).
func (u UUID) Node() []byte {
if u.Version() != 1 {
return nil
} else {
return u[10:]
}
}
// Timestamp extracts the timestamp information from a time based UUID
// (version 1).
func (u UUID) Timestamp() int64 {
if u.Version() != 1 {
return 0
}
return int64(uint64(u[0])<<24|uint64(u[1])<<16|uint64(u[2])<<8|uint64(u[3])) +
int64(uint64(u[4])<<40|uint64(u[5])<<32) +
int64(uint64(u[6]&0x0F)<<56|uint64(u[7])<<48)
}
// Time is like Timestamp, except that it returns a time.Time.
func (u UUID) Time() time.Time {
if u.Version() != 1 {
return time.Time{}
}
t := u.Timestamp()
sec := t / 1e7
nsec := (t % 1e7) * 100
return time.Unix(sec+timeBase, nsec).UTC()
}
func (u UUID) MarshalBinary() ([]byte, error) {
return u[:], nil
}
func (u *UUID) UnmarshalBinary(data []byte) error {
v, err := FromBytes(data)
if err != nil {
return err
}
*u = v
return nil
}
func (u UUID) MarshalText() ([]byte, error) {
return []byte(u.String()), nil
}
func (u *UUID) UnmarshalText(data []byte) error {
v, err := Parse(string(data))
if err != nil {
return err
}
*u = v
return nil
}
func (u UUID) MarshalJSON() ([]byte, error) {
if u == Zero {
return []byte("null"), nil
} else {
return []byte(`"` + u.String() + `"`), nil
}
}
func (u *UUID) UnmarshalJSON(data []byte) error {
raw := string(data)
if raw == "null" || raw == "0" {
copy(u[:], Zero[:])
return nil
}
str := strings.Trim(raw, `"`)
if len(str) > 36 {
return fmt.Errorf("Invalid JSON UUID: %s", str)
}
res, err := Parse(str)
if err == nil {
copy(u[:], res[:])
}
return err
}
func (v UUID) Value() (driver.Value, error) {
return v.String(), nil
}
func (v *UUID) Scan(src interface{}) error {
var err error
switch c := src.(type) {
case []byte:
*v, err = Parse(string(c))
case string:
*v, err = Parse(c)
default:
err = fmt.Errorf("Unsupported type: %T", src)
}
return err
}