From 42cb3594c2c04da53b02fbbb517d805355815bd2 Mon Sep 17 00:00:00 2001 From: rfyiamcool Date: Tue, 28 Nov 2023 17:04:39 +0800 Subject: [PATCH 1/2] feat: add uuid bytes() Signed-off-by: rfyiamcool --- uuid.go | 8 ++++++++ uuid_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/uuid.go b/uuid.go index dc75f7d..dc2d8f7 100644 --- a/uuid.go +++ b/uuid.go @@ -194,6 +194,14 @@ func (uuid UUID) String() string { return string(buf[:]) } +// Bytes returns the bytes form of uuid, xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx +// , or "" if uuid is invalid. +func (uuid UUID) Bytes() []byte { + var buf [36]byte + encodeHex(buf[:], uuid) + return buf[:] +} + // URN returns the RFC 2141 URN form of uuid, // urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, or "" if uuid is invalid. func (uuid UUID) URN() string { diff --git a/uuid_test.go b/uuid_test.go index fc97f2d..731210d 100644 --- a/uuid_test.go +++ b/uuid_test.go @@ -224,6 +224,33 @@ func TestNew(t *testing.T) { } } +func TestBytes(t *testing.T) { + m := make(map[UUID]bool) + for x := 1; x < 32; x++ { + s := New() + if m[s] { + t.Errorf("New returned duplicated UUID %s", s) + } + m[s] = true + if s.String() != string(s.Bytes()) { + t.Error("uuid.String() don't equal uuid.string(Byte())") + continue + } + uuid, err := ParseBytes(s.Bytes()) + if err != nil { + t.Errorf("New.String() returned %q which does not decode", s) + continue + } + + if v := uuid.Version(); v != 4 { + t.Errorf("Random UUID of version %s", v) + } + if uuid.Variant() != RFC4122 { + t.Errorf("Random UUID is variant %d", uuid.Variant()) + } + } +} + func TestClockSeq(t *testing.T) { // Fake time.Now for this test to return a monotonically advancing time; restore it at end. defer func(orig func() time.Time) { timeNow = orig }(timeNow) From 9e5f57d21298b7662896ab9c2e2b02cb2ec9bcad Mon Sep 17 00:00:00 2001 From: rfyiamcool Date: Tue, 28 Nov 2023 17:06:45 +0800 Subject: [PATCH 2/2] feat: add uuid bytes() Signed-off-by: rfyiamcool --- uuid.go | 3 +-- uuid_test.go | 30 +++--------------------------- 2 files changed, 4 insertions(+), 29 deletions(-) diff --git a/uuid.go b/uuid.go index dc2d8f7..21a6c61 100644 --- a/uuid.go +++ b/uuid.go @@ -194,8 +194,7 @@ func (uuid UUID) String() string { return string(buf[:]) } -// Bytes returns the bytes form of uuid, xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx -// , or "" if uuid is invalid. +// Bytes returns the bytes form of uuid, xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx. func (uuid UUID) Bytes() []byte { var buf [36]byte encodeHex(buf[:], uuid) diff --git a/uuid_test.go b/uuid_test.go index 731210d..be3b753 100644 --- a/uuid_test.go +++ b/uuid_test.go @@ -224,33 +224,6 @@ func TestNew(t *testing.T) { } } -func TestBytes(t *testing.T) { - m := make(map[UUID]bool) - for x := 1; x < 32; x++ { - s := New() - if m[s] { - t.Errorf("New returned duplicated UUID %s", s) - } - m[s] = true - if s.String() != string(s.Bytes()) { - t.Error("uuid.String() don't equal uuid.string(Byte())") - continue - } - uuid, err := ParseBytes(s.Bytes()) - if err != nil { - t.Errorf("New.String() returned %q which does not decode", s) - continue - } - - if v := uuid.Version(); v != 4 { - t.Errorf("Random UUID of version %s", v) - } - if uuid.Variant() != RFC4122 { - t.Errorf("Random UUID is variant %d", uuid.Variant()) - } - } -} - func TestClockSeq(t *testing.T) { // Fake time.Now for this test to return a monotonically advancing time; restore it at end. defer func(orig func() time.Time) { timeNow = orig }(timeNow) @@ -316,6 +289,9 @@ func TestCoding(t *testing.T) { if v := data.String(); v != text { t.Errorf("%x: encoded to %s, expected %s", data, v, text) } + if bs := data.Bytes(); string(bs) != text { + t.Errorf("%x: encoded to %s, expected %s", data, string(bs), text) + } if v := data.URN(); v != urn { t.Errorf("%x: urn is %s, expected %s", data, v, urn) }