Skip to content

Commit cd53251

Browse files
committed
Merge pull request #24 from bmatsuo/bmatsuo/array-conversions
Array conversions
2 parents 0f1a469 + be63595 commit cd53251

File tree

2 files changed

+98
-1
lines changed

2 files changed

+98
-1
lines changed

uuid.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@ import (
1313
"strings"
1414
)
1515

16+
// Array is a pass-by-value UUID that can be used as an effecient key in a map.
17+
type Array [16]byte
18+
19+
// UUID converts uuid into a slice.
20+
func (uuid Array) UUID() UUID {
21+
return uuid[:]
22+
}
23+
24+
// String returns the string representation of uuid,
25+
// xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.
26+
func (uuid Array) String() string {
27+
return uuid.UUID().String()
28+
}
29+
1630
// A UUID is a 128 bit (16 byte) Universal Unique IDentifier as defined in RFC
1731
// 4122.
1832
type UUID []byte
@@ -76,6 +90,17 @@ func Equal(uuid1, uuid2 UUID) bool {
7690
return bytes.Equal(uuid1, uuid2)
7791
}
7892

93+
// Array returns an array representation of uuid that can be used as a map key.
94+
// Array panics if uuid is not valid.
95+
func (uuid UUID) Array() Array {
96+
if len(uuid) != 16 {
97+
panic("invalid uuid")
98+
}
99+
var a Array
100+
copy(a[:], uuid)
101+
return a
102+
}
103+
79104
// String returns the string form of uuid, xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
80105
// , or "" if uuid is invalid.
81106
func (uuid UUID) String() string {

uuid_test.go

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ func TestNode(t *testing.T) {
300300
t.Error("nodeid is all zeros")
301301
}
302302

303-
id := []byte{1,2,3,4,5,6,7,8}
303+
id := []byte{1, 2, 3, 4, 5, 6, 7, 8}
304304
SetNodeID(id)
305305
ni = NodeID()
306306
if !bytes.Equal(ni, id[:6]) {
@@ -431,6 +431,40 @@ func TestBadRand(t *testing.T) {
431431
}
432432
}
433433

434+
func TestUUID_Array(t *testing.T) {
435+
expect := Array{
436+
0xf4, 0x7a, 0xc1, 0x0b,
437+
0x58, 0xcc,
438+
0x03, 0x72,
439+
0x85, 0x67,
440+
0x0e, 0x02, 0xb2, 0xc3, 0xd4, 0x79,
441+
}
442+
uuid := Parse("f47ac10b-58cc-0372-8567-0e02b2c3d479")
443+
if uuid == nil {
444+
t.Fatal("invalid uuid")
445+
}
446+
if uuid.Array() != expect {
447+
t.Fatal("invalid array")
448+
}
449+
}
450+
451+
func TestArray_UUID(t *testing.T) {
452+
array := Array{
453+
0xf4, 0x7a, 0xc1, 0x0b,
454+
0x58, 0xcc,
455+
0x03, 0x72,
456+
0x85, 0x67,
457+
0x0e, 0x02, 0xb2, 0xc3, 0xd4, 0x79,
458+
}
459+
expect := Parse("f47ac10b-58cc-0372-8567-0e02b2c3d479")
460+
if expect == nil {
461+
t.Fatal("invalid uuid")
462+
}
463+
if !bytes.Equal(array.UUID(), expect) {
464+
t.Fatal("invalid uuid")
465+
}
466+
}
467+
434468
func BenchmarkParse(b *testing.B) {
435469
for i := 0; i < b.N; i++ {
436470
uuid := Parse("f47ac10b-58cc-0372-8567-0e02b2c3d479")
@@ -469,3 +503,41 @@ func BenchmarkUUID_URN(b *testing.B) {
469503
}
470504
}
471505
}
506+
507+
func BenchmarkUUID_Array(b *testing.B) {
508+
expect := Array{
509+
0xf4, 0x7a, 0xc1, 0x0b,
510+
0x58, 0xcc,
511+
0x03, 0x72,
512+
0x85, 0x67,
513+
0x0e, 0x02, 0xb2, 0xc3, 0xd4, 0x79,
514+
}
515+
uuid := Parse("f47ac10b-58cc-0372-8567-0e02b2c3d479")
516+
if uuid == nil {
517+
b.Fatal("invalid uuid")
518+
}
519+
for i := 0; i < b.N; i++ {
520+
if uuid.Array() != expect {
521+
b.Fatal("invalid array")
522+
}
523+
}
524+
}
525+
526+
func BenchmarkArray_UUID(b *testing.B) {
527+
array := Array{
528+
0xf4, 0x7a, 0xc1, 0x0b,
529+
0x58, 0xcc,
530+
0x03, 0x72,
531+
0x85, 0x67,
532+
0x0e, 0x02, 0xb2, 0xc3, 0xd4, 0x79,
533+
}
534+
expect := Parse("f47ac10b-58cc-0372-8567-0e02b2c3d479")
535+
if expect == nil {
536+
b.Fatal("invalid uuid")
537+
}
538+
for i := 0; i < b.N; i++ {
539+
if !bytes.Equal(array.UUID(), expect) {
540+
b.Fatal("invalid uuid")
541+
}
542+
}
543+
}

0 commit comments

Comments
 (0)