From b6d3a8e751420557f0aaa9c4da52796cca55d070 Mon Sep 17 00:00:00 2001 From: Tim Mattison Date: Wed, 6 Mar 2024 10:55:48 -0500 Subject: [PATCH 1/2] Moved NewV7FromReader to uuid_test since it shouldn't be used outside of testing --- uuid_test.go | 14 ++++++++++++++ version7.go | 17 ----------------- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/uuid_test.go b/uuid_test.go index c1c6001..f0f6e95 100644 --- a/uuid_test.go +++ b/uuid_test.go @@ -8,6 +8,7 @@ import ( "bytes" "errors" "fmt" + "io" "os" "runtime" "strings" @@ -884,6 +885,19 @@ func TestVersion7FromReader(t *testing.T) { } } +// NewV7FromReader returns a Version 7 UUID based on the current time and uses +// NewRandomFromReader to get bytes 8-15 of the UUID. +// On error, NewV7FromReader returns Nil and an error. +func NewV7FromReader(r io.Reader) (UUID, error) { + uuid, err := NewRandomFromReader(r) + if err != nil { + return uuid, err + } + + makeV7(uuid[:]) + return uuid, nil +} + func TestVersion7Monotonicity(t *testing.T) { length := 10000 u1 := Must(NewV7()).String() diff --git a/version7.go b/version7.go index 0f5d994..f5dee8f 100644 --- a/version7.go +++ b/version7.go @@ -4,10 +4,6 @@ package uuid -import ( - "io" -) - // UUID version 7 features a time-ordered value field derived from the widely // implemented and well known Unix Epoch timestamp source, // the number of milliseconds seconds since midnight 1 Jan 1970 UTC, leap seconds excluded. @@ -29,19 +25,6 @@ func NewV7() (UUID, error) { return uuid, nil } -// NewV7FromReader returns a Version 7 UUID based on the current time(Unix Epoch). -// it use NewRandomFromReader fill random bits. -// On error, NewV7FromReader returns Nil and an error. -func NewV7FromReader(r io.Reader) (UUID, error) { - uuid, err := NewRandomFromReader(r) - if err != nil { - return uuid, err - } - - makeV7(uuid[:]) - return uuid, nil -} - // makeV7 fill 48 bits time (uuid[0] - uuid[5]), set version b0111 (uuid[6]) // uuid[8] already has the right version number (Variant is 10) // see function NewV7 and NewV7FromReader From 36e3bce8769ffa7a392e5990c61ee8506091772a Mon Sep 17 00:00:00 2001 From: Tim Mattison Date: Wed, 6 Mar 2024 10:58:59 -0500 Subject: [PATCH 2/2] Additional comment for clarity on the behavior --- uuid_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/uuid_test.go b/uuid_test.go index f0f6e95..13b2ae6 100644 --- a/uuid_test.go +++ b/uuid_test.go @@ -886,7 +886,8 @@ func TestVersion7FromReader(t *testing.T) { } // NewV7FromReader returns a Version 7 UUID based on the current time and uses -// NewRandomFromReader to get bytes 8-15 of the UUID. +// NewRandomFromReader to get bytes 8-15 of the UUID. Bytes 0-7 from the reader +// are read but are overwritten by the makeV7 function. // On error, NewV7FromReader returns Nil and an error. func NewV7FromReader(r io.Reader) (UUID, error) { uuid, err := NewRandomFromReader(r)