From 9345eb21126c50c16d9ee6646c02712a70c209dc Mon Sep 17 00:00:00 2001 From: Rowan Seymour Date: Thu, 1 Aug 2024 19:37:29 -0500 Subject: [PATCH] Replace uuids.IsV4 and uuids.IsV7 with uuids.Is and uuids.Version --- uuids/uuid_test.go | 14 +++++++------- uuids/validation.go | 27 ++++++++++++++++----------- uuids/validation_test.go | 29 +++++++++++++++-------------- 3 files changed, 38 insertions(+), 32 deletions(-) diff --git a/uuids/uuid_test.go b/uuids/uuid_test.go index 4df0c1f..0142664 100644 --- a/uuids/uuid_test.go +++ b/uuids/uuid_test.go @@ -13,8 +13,8 @@ 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) } @@ -22,8 +22,8 @@ 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) } @@ -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) diff --git a/uuids/validation.go b/uuids/validation.go index 903f3e1..90aea46 100644 --- a/uuids/validation.go +++ b/uuids/validation.go @@ -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 } diff --git a/uuids/validation_test.go b/uuids/validation_test.go index 30c64ab..48c83e0 100644 --- a/uuids/validation_test.go +++ b/uuids/validation_test.go @@ -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")) }