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 diff --git a/ulid_test.go b/ulid_test.go index 6905d32..e6c044a 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.Equal(id.Bytes(), bid) { + t.Error("Bytes() returned a reference to ulid underlying array!") + } +} + type safeMonotonicReader struct { mtx sync.Mutex ulid.MonotonicReader