Skip to content

Commit

Permalink
Replace uuids.IsV4 and uuids.IsV7 with uuids.Is and uuids.Version
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Aug 2, 2024
1 parent 3059f6f commit 9345eb2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 32 deletions.
14 changes: 7 additions & 7 deletions uuids/uuid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ func TestNewV4(t *testing.T) {
uuid1 := uuids.NewV4()
uuid2 := uuids.NewV4()

assert.True(t, uuids.IsV4(string(uuid1)))
assert.True(t, uuids.IsV4(string(uuid2)))
assert.Equal(t, 4, uuids.Version(string(uuid1)))
assert.Equal(t, 4, uuids.Version(string(uuid2)))
assert.NotEqual(t, uuid1, uuid2)
}

func TestNewV7(t *testing.T) {
uuid1 := uuids.NewV7()
uuid2 := uuids.NewV7()

assert.True(t, uuids.IsV7(string(uuid1)))
assert.True(t, uuids.IsV7(string(uuid2)))
assert.Equal(t, 7, uuids.Version(string(uuid1)))
assert.Equal(t, 7, uuids.Version(string(uuid2)))
assert.NotEqual(t, uuid1, uuid2)
}

Expand All @@ -36,9 +36,9 @@ func TestSeededGenerator(t *testing.T) {
uuid2 := uuids.NewV7()
uuid3 := uuids.NewV4()

assert.True(t, uuids.IsV4(string(uuid1)))
assert.True(t, uuids.IsV7(string(uuid2)))
assert.True(t, uuids.IsV4(string(uuid3)))
assert.Equal(t, 4, uuids.Version(string(uuid1)))
assert.Equal(t, 7, uuids.Version(string(uuid2)))
assert.Equal(t, 4, uuids.Version(string(uuid3)))

assert.Equal(t, uuids.UUID(`d2f852ec-7b4e-457f-ae7f-f8b243c49ff5`), uuid1)
assert.Equal(t, uuids.UUID(`01910efd-5890-71e2-bd38-d266ec8d3716`), uuid2)
Expand Down
27 changes: 16 additions & 11 deletions uuids/validation.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
package uuids

import "regexp"
import (
"regexp"
"strconv"
)

var (
V4Regex = regexp.MustCompile(`[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}`)
V7Regex = regexp.MustCompile(`[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}`)
Regex = regexp.MustCompile(`[0-9a-f]{8}-[0-9a-f]{4}-([1-7])[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}`)

V4OnlyRegex = regexp.MustCompile(`^` + V4Regex.String() + `$`)
V7OnlyRegex = regexp.MustCompile(`^` + V7Regex.String() + `$`)
onlyRegex = regexp.MustCompile(`^` + Regex.String() + `$`)
)

// IsV4 returns whether the given string contains only a valid v4 UUID
func IsV4(s string) bool {
return V4OnlyRegex.MatchString(s)
// Is returns whether the given string contains only a valid v4 UUID
func Is(s string) bool {
return onlyRegex.MatchString(s)
}

// IsV7 returns whether the given string contains only a valid v7 UUID
func IsV7(s string) bool {
return V7OnlyRegex.MatchString(s)
func Version(s string) int {
m := onlyRegex.FindStringSubmatch(s)
if m == nil {
return 0
}
v, _ := strconv.Atoi(m[1])
return v
}
29 changes: 15 additions & 14 deletions uuids/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@ import (
"github.com/stretchr/testify/assert"
)

func TestIsV4(t *testing.T) {
assert.False(t, uuids.IsV4(""))
assert.True(t, uuids.IsV4("182faeb1-eb29-41e5-b288-c1af671ee671"))
assert.False(t, uuids.IsV4("182faeb1-eb29-71e5-b288-c1af671ee671"))
assert.False(t, uuids.IsV4("182faeb1-eb29-41e5-b288-c1af671ee67x"))
assert.False(t, uuids.IsV4("182faeb1-eb29-41e5-b288-c1af671ee67"))
assert.False(t, uuids.IsV4("182faeb1-eb29-41e5-b288-c1af671ee6712"))
func TestIs(t *testing.T) {
assert.True(t, uuids.Is("182faeb1-eb29-41e5-b288-c1af671ee671")) // v4
assert.True(t, uuids.Is("182faeb1-eb29-71e5-b288-c1af671ee671")) // v7

assert.False(t, uuids.Is(""))
assert.False(t, uuids.Is("182faeb1"))
assert.False(t, uuids.Is("182faeb1-eb29-41e5-b288-c1af671ee67x"))
assert.False(t, uuids.Is("182faeb1-eb29-41e5-b288-c1af671ee67"))
assert.False(t, uuids.Is("182faeb1-eb29-41e5-b288-c1af671ee6712"))
}

func TestIsV7(t *testing.T) {
assert.False(t, uuids.IsV7(""))
assert.True(t, uuids.IsV7("182faeb1-eb29-71e5-b288-c1af671ee671"))
assert.False(t, uuids.IsV7("182faeb1-eb29-41e5-b288-c1af671ee671"))
assert.False(t, uuids.IsV7("182faeb1-eb29-71e5-b288-c1af671ee67x"))
assert.False(t, uuids.IsV7("182faeb1-eb29-71e5-b288-c1af671ee67"))
assert.False(t, uuids.IsV7("182faeb1-eb29-71e5-b288-c1af671ee6712"))
func TestVersion(t *testing.T) {
assert.Equal(t, 4, uuids.Version("182faeb1-eb29-41e5-b288-c1af671ee671")) // v4
assert.Equal(t, 7, uuids.Version("182faeb1-eb29-71e5-b288-c1af671ee671")) // v7

assert.Equal(t, 0, uuids.Version(""))
assert.Equal(t, 0, uuids.Version("182faeb1"))
}

0 comments on commit 9345eb2

Please sign in to comment.