Skip to content
This repository has been archived by the owner on Sep 13, 2023. It is now read-only.

Commit

Permalink
Merge pull request #10 from neotoolkit/fix/uuid
Browse files Browse the repository at this point in the history
fix: fix lint issue
  • Loading branch information
sashamelentyev committed Jun 26, 2022
2 parents 45c7ae3 + f8c2038 commit 9084b5e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
7 changes: 6 additions & 1 deletion uuid.go
Expand Up @@ -14,8 +14,13 @@ type UUID struct {
// V4 returns UUID V4 as string
func (u UUID) V4() string {
var uuid [16]byte
io.ReadFull(rand.Reader, uuid[:])

if _, err := io.ReadFull(rand.Reader, uuid[:]); err != nil {
panic(err)
}

uuid[6] = (uuid[6] & 0x0f) | 0x40 // Version 4
uuid[8] = (uuid[8] & 0x3f) | 0x80 // Variant is 10

return fmt.Sprintf("%x-%x-%x-%x-%x", uuid[0:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:])
}
2 changes: 2 additions & 0 deletions uuid_test.go
Expand Up @@ -10,10 +10,12 @@ import (
func TestUUID_v4(t *testing.T) {
f := faker.NewFaker()
value := f.UUID().V4()

match, err := regexp.MatchString("^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$", value)
if err != nil {
t.Fatal(err)
}

if !match {
t.Fatal("want true, got false")
}
Expand Down

0 comments on commit 9084b5e

Please sign in to comment.