Skip to content

Commit 0d4fda9

Browse files
sabifypeterbourgon
authored andcommitted
Adding Bytes() to get slice representation of ULID (#49)
* Adding Bytes() to get slice representation of ULID * Added Test For Bytes() * Added The Test For Bytes()
1 parent be3bccf commit 0d4fda9

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

Diff for: ulid.go

+5
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,11 @@ func MustParseStrict(ulid string) ULID {
234234
return id
235235
}
236236

237+
// Bytes returns bytes slice representation of ULID.
238+
func (u ULID) Bytes() []byte {
239+
return u[:]
240+
}
241+
237242
// String returns a lexicographically sortable string encoded ULID
238243
// (26 characters, non-standard base 32) e.g. 01AN4Z07BY79KA1307SR9X4MV3
239244
// Format: tttttttttteeeeeeeeeeeeeeee where t is time and e is entropy

Diff for: ulid_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,17 @@ func TestMonotonicSafe(t *testing.T) {
619619
}
620620
}
621621

622+
func TestULID_Bytes(t *testing.T) {
623+
tt := time.Unix(1000000, 0)
624+
entropy := ulid.Monotonic(rand.New(rand.NewSource(tt.UnixNano())), 0)
625+
id := ulid.MustNew(ulid.Timestamp(tt), entropy)
626+
bid := id.Bytes()
627+
bid[len(bid)-1]++
628+
if bytes.Equal(id.Bytes(), bid) {
629+
t.Error("Bytes() returned a reference to ulid underlying array!")
630+
}
631+
}
632+
622633
type safeMonotonicReader struct {
623634
mtx sync.Mutex
624635
ulid.MonotonicReader

0 commit comments

Comments
 (0)