This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpreferences.go
346 lines (287 loc) · 7.15 KB
/
preferences.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
package uuid
import "fmt"
// ValueMode selects the output behavior of the SQL-oriented Value method.
type ValueMode byte
// ValueMode enum constants.
const (
_ ValueMode = iota
// Text: output is a string in the current TextMode.
Text
// Binary: output is a byte slice in the current BinaryMode.
Binary
)
func (vm ValueMode) asByte() byte {
if ch, found := vmMap[vm]; found {
return ch
}
return '!'
}
func (vm ValueMode) String() string {
return string(vm.asByte())
}
// BinaryMode selects the input/output format for parsing/producing binary represenatations.
type BinaryMode byte
// BinaryMode enum constants.
const (
_ BinaryMode = iota
// StandardOnly: produce RFC 4122 bytes, parse RFC 4122 bytes.
StandardOnly
// StandardFirst: produce RFC 4122 bytes, parse either byte ordering.
// Ambiguous inputs will be interpreted as RFC 4122.
StandardFirst
// DenseOnly: produce "dense" bytes, parse "dense" bytes.
DenseOnly
// DenseFirst: produce "dense" bytes, parse either byte ordering.
// Ambiguous inputs will be interpreted as "dense".
DenseFirst
)
func (bm BinaryMode) asByte() byte {
if ch, found := bmMap[bm]; found {
return ch
}
return '!'
}
func (bm BinaryMode) String() string {
return string(bm.asByte())
}
// TextMode selects the output format for producing textual representations.
type TextMode byte
// TextMode enum constants.
const (
_ TextMode = iota
// Dense: "@<base64>", e.g. "@EeiKtHe5nOqWqBheD61jNQ"
Dense
// Canonical: "8-4-4-4-12", e.g. "77b99cea-8ab4-11e8-96a8-185e0fad6335"
Canonical
// HashLike: raw hex digits, e.g. "77b99cea8ab411e896a8185e0fad6335"
HashLike
// Bracketed: "{8-4-4-4-12}", e.g. "{77b99cea-8ab4-11e8-96a8-185e0fad6335}"
Bracketed
// URN: URN format, e.g. "urn:uuid:77b99cea-8ab4-11e8-96a8-185e0fad6335"
URN
)
func (tm TextMode) asByte() byte {
if ch, found := tmMap[tm]; found {
return ch
}
return '!'
}
func (tm TextMode) String() string {
return string(tm.asByte())
}
// Preferences represents a combination of modes to apply to a UUID.
type Preferences struct {
Value ValueMode
Binary BinaryMode
Text TextMode
}
// Nil returns a Nil-valued UUID with these preferences.
func (pref Preferences) Nil() UUID {
var uuid UUID
uuid.SetPreferences(pref)
return uuid
}
// New returns a newly generated V1 UUID with these preferences.
func (pref Preferences) New() UUID {
var uuid UUID
uuid.SetPreferences(pref)
uuid.SetNew()
return uuid
}
// FromBytes attempts to parse a binary UUID representation using these preferences.
func (pref Preferences) FromBytes(in []byte) (UUID, error) {
var uuid UUID
uuid.SetPreferences(pref)
err := unmarshalBinary("Preferences", "FromBytes", uuid.a[:], in, uuid.getBits())
return uuid, err
}
// MustFromBytes parses a binary UUID representation using these preferences, or panics if it cannot.
func (pref Preferences) MustFromBytes(in []byte) UUID {
var uuid UUID
uuid.SetPreferences(pref)
err := unmarshalBinary("Preferences", "MustFromBytes", uuid.a[:], in, uuid.getBits())
if err != nil {
panic(err)
}
return uuid
}
// FromString attempts to parse a textual UUID representation with these preferences.
func (pref Preferences) FromString(in string) (UUID, error) {
var uuid UUID
uuid.SetPreferences(pref)
err := unmarshalText("Preferences", "FromString", uuid.a[:], []byte(in))
return uuid, err
}
// MustFromString parses a textual UUID representation with these preferences, or panics if it cannot.
func (pref Preferences) MustFromString(in string) UUID {
var uuid UUID
uuid.SetPreferences(pref)
err := unmarshalText("Preferences", "MustFromString", uuid.a[:], []byte(in))
if err != nil {
panic(err)
}
return uuid
}
func (pref Preferences) String() string {
var buf [3]byte
buf[0] = pref.Value.asByte()
buf[1] = pref.Binary.asByte()
buf[2] = pref.Text.asByte()
return string(buf[:])
}
func (pref Preferences) collapse(old bits) (x bits) {
x = bitValid
switch pref.Value {
case 0:
x |= old.just(bitValueIsBinary)
case Text:
// pass
case Binary:
x |= bitValueIsBinary
default:
panic(fmt.Errorf("unknown value ValueMode(%d)", pref.Value))
}
switch pref.Binary {
case 0:
x |= old.just(bitsBinary)
case StandardOnly:
// pass
case StandardFirst:
x |= bitBinaryIsLoose
case DenseOnly:
x |= bitBinaryIsDense
case DenseFirst:
x |= bitBinaryIsDense | bitBinaryIsLoose
default:
panic(fmt.Errorf("unknown value BinaryMode(%d)", pref.Binary))
}
switch pref.Text {
case 0:
x |= old.just(bitsText)
case Canonical:
x |= textModeCanonical
case HashLike:
x |= textModeHashLike
case Bracketed:
x |= textModeBracketed
case URN:
x |= textModeURN
case Dense:
x |= bitTextIsDense
default:
panic(fmt.Errorf("unknown value TextMode(%d)", pref.Text))
}
return
}
type bits byte
const (
bitValid bits = 0x80
bitValueIsBinary bits = 0x40
bitBinaryIsDense bits = 0x20
bitBinaryIsLoose bits = 0x10
bitTextIsDense bits = 0x08
bitTextIsModeX bits = 0x04
bitTextIsModeY bits = 0x02
bitReserved bits = 0x01
)
const (
bitsDefault = bitValid | bitBinaryIsDense | bitBinaryIsLoose | bitTextIsDense
bitsBinary = bitBinaryIsDense | bitBinaryIsLoose
bitsText = bitTextIsDense | bitTextIsModeX | bitTextIsModeY
bitsTextIsMode = bitTextIsModeX | bitTextIsModeY
)
const (
textModeCanonical bits = 0
textModeHashLike bits = bitTextIsModeX
textModeBracketed bits = bitTextIsModeY
textModeURN bits = bitTextIsModeX | bitTextIsModeY
)
func (x bits) String() string {
return x.expand().String()
}
func (x bits) GoString() string {
return fmt.Sprintf("%08b", byte(x))
}
func (x bits) just(y bits) bits {
return (x & y)
}
func (x bits) has(y bits) bool {
return (x & y) == y
}
func (x bits) defaulted() bits {
if x.has(bitValid) {
return x
}
return bitsDefault
}
func (x bits) expand() (pref Preferences) {
switch x.just(bitValueIsBinary) {
case 0:
pref.Value = Text
case bitValueIsBinary:
pref.Value = Binary
}
switch x.just(bitsBinary) {
case 0:
pref.Binary = StandardOnly
case bitBinaryIsLoose:
pref.Binary = StandardFirst
case bitBinaryIsDense:
pref.Binary = DenseOnly
case bitBinaryIsDense | bitBinaryIsLoose:
pref.Binary = DenseFirst
}
switch x.just(bitsText) {
case textModeCanonical:
pref.Text = Canonical
case textModeHashLike:
pref.Text = HashLike
case textModeBracketed:
pref.Text = Bracketed
case textModeURN:
pref.Text = URN
case bitTextIsDense | textModeCanonical:
fallthrough
case bitTextIsDense | textModeHashLike:
fallthrough
case bitTextIsDense | textModeBracketed:
fallthrough
case bitTextIsDense | textModeURN:
pref.Text = Dense
}
return
}
func (x bits) textLength() (used uint, peak uint) {
switch x.just(bitsText) {
case textModeCanonical:
return 36, 36
case textModeHashLike:
return 32, 32
case textModeBracketed:
return 38, 38
case textModeURN:
return 45, 45
default:
return 23, 25
}
}
var vmMap = map[ValueMode]byte{
0: '-',
Text: 'T',
Binary: 'B',
}
var bmMap = map[BinaryMode]byte{
0: '-',
StandardOnly: 'S',
StandardFirst: 's',
DenseOnly: 'D',
DenseFirst: 'd',
}
var tmMap = map[TextMode]byte{
0: '-',
Dense: 'D',
Canonical: 'C',
HashLike: 'H',
Bracketed: 'B',
URN: 'U',
}