From ff438f215c8112cff09d8cf844679a9e2d244890 Mon Sep 17 00:00:00 2001 From: Saber Haj Rabiee Date: Wed, 29 May 2019 13:15:28 -0700 Subject: [PATCH 1/3] Adding Bytes() to get slice representation of ULID --- ulid.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ulid.go b/ulid.go index c2f16ae..adc8713 100644 --- a/ulid.go +++ b/ulid.go @@ -234,6 +234,11 @@ func MustParseStrict(ulid string) ULID { return id } +// Bytes returns bytes slice representation of ULID. +func (u ULID) Bytes() []byte { + return u[:] +} + // String returns a lexicographically sortable string encoded ULID // (26 characters, non-standard base 32) e.g. 01AN4Z07BY79KA1307SR9X4MV3 // Format: tttttttttteeeeeeeeeeeeeeee where t is time and e is entropy From 31ba2fc2549fa2bbb5195eb6e99bd46e7cfc9868 Mon Sep 17 00:00:00 2001 From: Saber Haj Rabiee Date: Fri, 31 May 2019 07:29:24 -0700 Subject: [PATCH 2/3] Added Test For Bytes() --- ulid_test.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/ulid_test.go b/ulid_test.go index 6905d32..67c65bf 100644 --- a/ulid_test.go +++ b/ulid_test.go @@ -619,6 +619,17 @@ func TestMonotonicSafe(t *testing.T) { } } +func TestULID_Bytes(t *testing.T) { + tt := time.Unix(1000000, 0) + entropy := ulid.Monotonic(rand.New(rand.NewSource(tt.UnixNano())), 0) + id := ulid.MustNew(ulid.Timestamp(tt), entropy) + bid := id.Bytes() + bid[len(bid)-1]++ + if bytes.Compare(id.Bytes(), bid) == 0 { + t.Error("Bytes() returned a reference to ulid underlying array!") + } +} + type safeMonotonicReader struct { mtx sync.Mutex ulid.MonotonicReader From 8d45742584706082bd766aa36400b28aa9b1b882 Mon Sep 17 00:00:00 2001 From: Saber Haj Rabiee Date: Fri, 31 May 2019 07:32:57 -0700 Subject: [PATCH 3/3] Added The Test For Bytes() --- ulid_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ulid_test.go b/ulid_test.go index 67c65bf..e6c044a 100644 --- a/ulid_test.go +++ b/ulid_test.go @@ -625,7 +625,7 @@ func TestULID_Bytes(t *testing.T) { id := ulid.MustNew(ulid.Timestamp(tt), entropy) bid := id.Bytes() bid[len(bid)-1]++ - if bytes.Compare(id.Bytes(), bid) == 0 { + if bytes.Equal(id.Bytes(), bid) { t.Error("Bytes() returned a reference to ulid underlying array!") } }