-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
161 lines (143 loc) · 3.45 KB
/
common.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
package uuid
import (
"crypto/rand"
"encoding/base64"
"encoding/binary"
"encoding/hex"
"errors"
"reflect"
"strings"
"time"
)
// UUID is a 128bit Universal Unique IDentifier.
type UUID [16]byte
func (uuid UUID) Version() (ret Version) {
return Version((binary.BigEndian.Uint64(uuid[:8]) & (1<<16 - 1)) >> 12)
}
func (uuid UUID) Short() (ret string) {
return hex.EncodeToString(uuid[:])
}
func (uuid UUID) String() (ret string) {
var buf [36]byte
hex.Encode(buf[:8], uuid[:4])
buf[8] = '-'
hex.Encode(buf[9:13], uuid[4:6])
buf[13] = '-'
hex.Encode(buf[14:18], uuid[6:8])
buf[18] = '-'
hex.Encode(buf[19:23], uuid[8:10])
buf[23] = '-'
hex.Encode(buf[24:], uuid[10:])
return string(buf[:])
}
func (uuid UUID) URN() (ret string) {
return "urn:uuid:" + uuid.String()
}
func (uuid UUID) Base64() (ret string) {
return base64.RawStdEncoding.EncodeToString(uuid[:])
}
func byteDecode(id []byte) (uuid UUID, err error) {
if id[8] != '-' || id[13] != '-' || id[18] != '-' || id[23] != '-' {
return UUID{}, errors.New("invalid UUID format")
}
var src [32]byte
copy(src[:8], id[:8])
copy(src[8:12], id[9:13])
copy(src[12:16], id[14:18])
copy(src[16:20], id[19:23])
copy(src[20:], id[24:36])
_, err = hex.Decode(uuid[:], src[:])
return
}
func Parse(in interface{}) (uuid UUID, err error) {
if id, ok := in.(string); ok {
switch len(id) {
case 36 - 4:
// short format
// xxxxxxxxxxxxxxxxxxNxxxxxxxxxxxxxxx
_, err = hex.Decode(uuid[:], []byte(id))
case 36:
// UUID format
// xxxxxxxxxx-xxxx-xxxx-Nxxx-xxxxxxxxxxxx
return byteDecode([]byte(id))
case 36 + 2:
// // Microsoft format -> undefined
// // {xxxxxxxxxx-xxxx-xxxx-Nxxx-xxxxxxxxxxxx}
if id[0] != '{' || id[37] != '}' {
return UUID{}, errors.New("invalid UUID format")
}
return byteDecode([]byte(id[1:37]))
case 36 + 9:
// URN format
// urn:uuid:xxxxxxxxxx-xxxx-xxxx-Nxxx-xxxxxxxxxxxx
if strings.ToLower(id[:9]) != "urn:uuid:" {
return UUID{}, errors.New("invalid UUID format")
}
return byteDecode([]byte(id[9:]))
default:
// base64 ?
if _, err = base64.RawStdEncoding.Decode(uuid[:], []byte(id)); err != nil {
if _, err = base64.StdEncoding.Decode(uuid[:], []byte(id)); err != nil {
return
}
}
}
} else {
if x, ok := in.([]uint8); ok {
copy(uuid[:], x)
} else {
err = errors.New("input value is an unsupported type")
}
}
return
}
// Version is used to represent the version of the UUID.
type Version byte
const (
UUIDv1 = Version(0b0001)
UUIDv2 = Version(0b0010)
UUIDv3 = Version(0b0011)
UUIDv4 = Version(0b0100)
UUIDv5 = Version(0b0101)
UUIDv6 = Version(0b0110)
UUIDv7 = Version(0b0111)
UUIDv8 = Version(0b1000)
)
// Variant is used to represent the variant of the UUID.
type Variant byte
const (
Backward = Variant(0b_0000)
RFC4122 = Variant(0b_0010)
Microsoft = Variant(0b_0110)
Future = Variant(0b_0111)
)
var reader = rand.Reader
type ID interface {
Convert(t time.Time) (ret UUID)
Generate() (ret UUID)
Decode(uuid UUID) (ret time.Time)
}
func New(version Version) (id ID) {
switch version {
case UUIDv6:
case UUIDv7:
id = &V7{v: version}
case UUIDv8:
}
return
}
func getMonotonicClock(t time.Time, max ...int) (ret uint64) {
const hasMonotonic = 1 << 63
var (
wall = reflect.ValueOf(t).FieldByName("wall").Uint()
ext = reflect.ValueOf(t).FieldByName("ext").Int()
)
if wall&hasMonotonic == 0 {
return 0
}
ret = uint64(ext)
if len(max) > 0 {
ret = ret & (1<<max[0] - 1)
}
return
}