From 1c837a579a6909823bc2bf633a3eafacede54a2d Mon Sep 17 00:00:00 2001 From: godcong Date: Thu, 11 Jul 2024 21:09:01 +0800 Subject: [PATCH] refactor(ident): This update contains some major changes Separate subprojects Single registration at import Only one ID generator can be registered at a time --- ident/go.mod | 7 +++ ident/go.sum | 10 ++++ ident/ident.go | 6 +-- ident/snowflake/snowflake.go | 2 +- ident/snowflake/snowflake_test.go | 80 +++++++++++++++++++++++++++++++ ident/ulid/ulid.go | 2 +- ident/ulid/ulid_test.go | 80 +++++++++++++++++++++++++++++++ ident/uuid/uuid.go | 2 +- ident/uuid/uuid_test.go | 80 +++++++++++++++++++++++++++++++ ident/xid/xid.go | 2 +- ident/xid/xid_test.go | 80 +++++++++++++++++++++++++++++++ 11 files changed, 344 insertions(+), 7 deletions(-) create mode 100644 ident/snowflake/snowflake_test.go create mode 100644 ident/ulid/ulid_test.go create mode 100644 ident/uuid/uuid_test.go create mode 100644 ident/xid/xid_test.go diff --git a/ident/go.mod b/ident/go.mod index 169b016..f1b4eb1 100644 --- a/ident/go.mod +++ b/ident/go.mod @@ -7,4 +7,11 @@ require ( github.com/google/uuid v1.6.0 github.com/oklog/ulid/v2 v2.1.0 github.com/rs/xid v1.5.0 + github.com/stretchr/testify v1.9.0 +) + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/ident/go.sum b/ident/go.sum index 2f7be70..8e28a61 100644 --- a/ident/go.sum +++ b/ident/go.sum @@ -1,9 +1,19 @@ github.com/bwmarrin/snowflake v0.3.0 h1:xm67bEhkKh6ij1790JB83OujPR5CzNe8QuQqAgISZN0= github.com/bwmarrin/snowflake v0.3.0/go.mod h1:NdZxfVWX+oR6y2K0o6qAYv6gIOP9rjG0/E9WsDpxqwE= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/oklog/ulid/v2 v2.1.0 h1:+9lhoxAP56we25tyYETBBY1YLA2SaoLvUFgrP2miPJU= github.com/oklog/ulid/v2 v2.1.0/go.mod h1:rcEKHmBBKfef9DhnvX7y1HZBYxjXb0cP5ExxNsTT1QQ= github.com/pborman/getopt v0.0.0-20170112200414-7148bc3a4c30/go.mod h1:85jBQOZwpVEaDAr341tbn15RS4fCAsIst0qp7i8ex1o= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rs/xid v1.5.0 h1:mKX4bl4iPYJtEIxp6CYiUuLQ/8DYMoz0PUdtGgMFRVc= github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/ident/ident.go b/ident/ident.go index 9206600..723d555 100644 --- a/ident/ident.go +++ b/ident/ident.go @@ -37,8 +37,8 @@ func GenID() string { return defaultIdentifier.Gen() } -// GenSize The function "GenSize" returns the size of the generated identifier -func GenSize() int { +// Size The function "Size" returns the size of the generated identifier +func Size() int { return defaultIdentifier.Size() } @@ -49,6 +49,6 @@ func Validate(id string) bool { var ( _ = GenID - _ = GenSize + _ = Size _ = Validate ) diff --git a/ident/snowflake/snowflake.go b/ident/snowflake/snowflake.go index 7a0c571..b37681c 100644 --- a/ident/snowflake/snowflake.go +++ b/ident/snowflake/snowflake.go @@ -42,7 +42,7 @@ func (s Snowflake) Gen() string { // Validate checks if the provided ID is a valid Snowflake ID. func (s Snowflake) Validate(id string) bool { _, err := snowflake.ParseString(id) - return err != nil + return err == nil } // Size returns the bit size of the generated Snowflake ID. diff --git a/ident/snowflake/snowflake_test.go b/ident/snowflake/snowflake_test.go new file mode 100644 index 0000000..3b04b27 --- /dev/null +++ b/ident/snowflake/snowflake_test.go @@ -0,0 +1,80 @@ +package snowflake + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/origadmin/toolkits/ident" +) + +func TestSnowflakeGen(t *testing.T) { + s := New() + id := s.Gen() + assert.NotEmpty(t, id) +} + +func TestSnowflakeValidateValidID(t *testing.T) { + s := New() + id := s.Gen() + valid := s.Validate(id) + assert.True(t, valid) +} + +func TestSnowflakeValidateInvalidID(t *testing.T) { + s := New() + invalidID := "invalidID" + valid := s.Validate(invalidID) + assert.False(t, valid) +} + +func TestSnowflakeSize(t *testing.T) { + s := New() + size := s.Size() + assert.Equal(t, bitSize, size) +} + +func TestRegister(t *testing.T) { + if ident.Default() == nil { + t.Errorf("Expected default identifier to be set, but it was not") + } + // Check that the default identifier has been updated + if ident.Default().Name() != "snowflake" { + t.Errorf("Expected default identifier to be updated, but it was not") + } +} + +func TestGenID(t *testing.T) { + // Generate an ID + generatedID := ident.GenID() + + // Check that the generated ID is valid + if !ident.Validate(generatedID) { + t.Errorf("Generated ID is not valid") + } +} + +func TestGenSize(t *testing.T) { + // Check that the size of the generated ID is correct + if ident.Size() != bitSize { + t.Errorf("Expected size of generated ID to be %d, but it was %d", bitSize, ident.Size()) + } +} + +func TestValidate(t *testing.T) { + // Create a new identifier + generator := New() + + // Generate an ID + generatedID := generator.Gen() + + // Check that the generated ID is valid + if !ident.Validate(generatedID) { + t.Errorf("Generated ID is not valid") + } + + // Check that an invalid ID is not valid + if ident.Validate("invalid") { + t.Errorf("Invalid ID is valid") + } +} diff --git a/ident/ulid/ulid.go b/ident/ulid/ulid.go index 5595c7c..6d4b65b 100644 --- a/ident/ulid/ulid.go +++ b/ident/ulid/ulid.go @@ -36,7 +36,7 @@ func (U ULID) Gen() string { // It implements the ident.Identifier interface. func (U ULID) Validate(id string) bool { _, err := ulid.ParseStrict(id) - return err != nil + return err == nil } // Size returns the size of a ULID string in bits. diff --git a/ident/ulid/ulid_test.go b/ident/ulid/ulid_test.go new file mode 100644 index 0000000..15db184 --- /dev/null +++ b/ident/ulid/ulid_test.go @@ -0,0 +1,80 @@ +package ulid + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/origadmin/toolkits/ident" +) + +func TestULIDGen(t *testing.T) { + s := New() + id := s.Gen() + assert.NotEmpty(t, id) +} + +func TestULIDValidateValidID(t *testing.T) { + s := New() + id := s.Gen() + valid := s.Validate(id) + assert.True(t, valid) +} + +func TestULIDValidateInvalidID(t *testing.T) { + s := New() + invalidID := "invalidID" + valid := s.Validate(invalidID) + assert.False(t, valid) +} + +func TestULIDSize(t *testing.T) { + s := New() + size := s.Size() + assert.Equal(t, bitSize, size) +} + +func TestRegister(t *testing.T) { + if ident.Default() == nil { + t.Errorf("Expected default identifier to be set, but it was not") + } + // Check that the default identifier has been updated + if ident.Default().Name() != "ulid" { + t.Errorf("Expected default identifier to be updated, but it was not") + } +} + +func TestGenID(t *testing.T) { + // Generate an ID + generatedID := ident.GenID() + + // Check that the generated ID is valid + if !ident.Validate(generatedID) { + t.Errorf("Generated ID is not valid") + } +} + +func TestGenSize(t *testing.T) { + // Check that the size of the generated ID is correct + if ident.Size() != bitSize { + t.Errorf("Expected size of generated ID to be %d, but it was %d", bitSize, ident.Size()) + } +} + +func TestValidate(t *testing.T) { + // Create a new identifier + generator := New() + + // Generate an ID + generatedID := generator.Gen() + + // Check that the generated ID is valid + if !ident.Validate(generatedID) { + t.Errorf("Generated ID is not valid") + } + + // Check that an invalid ID is not valid + if ident.Validate("invalid") { + t.Errorf("Invalid ID is valid") + } +} diff --git a/ident/uuid/uuid.go b/ident/uuid/uuid.go index 25b776c..0eb0be1 100644 --- a/ident/uuid/uuid.go +++ b/ident/uuid/uuid.go @@ -34,7 +34,7 @@ func (U UUID) Gen() string { // Validate checks if the provided ID is a valid UUID func (U UUID) Validate(id string) bool { _, err := uuid.Parse(id) - return err != nil + return err == nil } // Size returns the size of the UUID in bits diff --git a/ident/uuid/uuid_test.go b/ident/uuid/uuid_test.go new file mode 100644 index 0000000..c5b8341 --- /dev/null +++ b/ident/uuid/uuid_test.go @@ -0,0 +1,80 @@ +package uuid + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/origadmin/toolkits/ident" +) + +func TestUUIDGen(t *testing.T) { + s := New() + id := s.Gen() + assert.NotEmpty(t, id) +} + +func TestUUIDValidateValidID(t *testing.T) { + s := New() + id := s.Gen() + valid := s.Validate(id) + assert.True(t, valid) +} + +func TestUUIDValidateInvalidID(t *testing.T) { + s := New() + invalidID := "invalidID" + valid := s.Validate(invalidID) + assert.False(t, valid) +} + +func TestUUIDSize(t *testing.T) { + s := New() + size := s.Size() + assert.Equal(t, bitSize, size) +} + +func TestRegister(t *testing.T) { + if ident.Default() == nil { + t.Errorf("Expected default identifier to be set, but it was not") + } + // Check that the default identifier has been updated + if ident.Default().Name() != "uuid" { + t.Errorf("Expected default identifier to be updated, but it was not") + } +} + +func TestGenID(t *testing.T) { + // Generate an ID + generatedID := ident.GenID() + + // Check that the generated ID is valid + if !ident.Validate(generatedID) { + t.Errorf("Generated ID is not valid") + } +} + +func TestGenSize(t *testing.T) { + // Check that the size of the generated ID is correct + if ident.Size() != bitSize { + t.Errorf("Expected size of generated ID to be %d, but it was %d", bitSize, ident.Size()) + } +} + +func TestValidate(t *testing.T) { + // Create a new identifier + generator := New() + + // Generate an ID + generatedID := generator.Gen() + + // Check that the generated ID is valid + if !ident.Validate(generatedID) { + t.Errorf("Generated ID is not valid") + } + + // Check that an invalid ID is not valid + if ident.Validate("invalid") { + t.Errorf("Invalid ID is valid") + } +} diff --git a/ident/xid/xid.go b/ident/xid/xid.go index a297c2f..5b1cbfa 100644 --- a/ident/xid/xid.go +++ b/ident/xid/xid.go @@ -35,7 +35,7 @@ func (x XID) Gen() string { // Validate checks if the provided id is a valid xid. func (x XID) Validate(id string) bool { _, err := xid.FromString(id) - return err != nil + return err == nil } // Size returns the size of the xid in bits. diff --git a/ident/xid/xid_test.go b/ident/xid/xid_test.go new file mode 100644 index 0000000..ffd2d6d --- /dev/null +++ b/ident/xid/xid_test.go @@ -0,0 +1,80 @@ +package xid + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/origadmin/toolkits/ident" +) + +func TestXIDGen(t *testing.T) { + s := New() + id := s.Gen() + assert.NotEmpty(t, id) +} + +func TestXIDValidateValidID(t *testing.T) { + s := New() + id := s.Gen() + valid := s.Validate(id) + assert.True(t, valid) +} + +func TestXIDValidateInvalidID(t *testing.T) { + s := New() + invalidID := "invalidID" + valid := s.Validate(invalidID) + assert.False(t, valid) +} + +func TestXIDSize(t *testing.T) { + s := New() + size := s.Size() + assert.Equal(t, bitSize, size) +} + +func TestRegister(t *testing.T) { + if ident.Default() == nil { + t.Errorf("Expected default identifier to be set, but it was not") + } + // Check that the default identifier has been updated + if ident.Default().Name() != "xid" { + t.Errorf("Expected default identifier to be updated, but it was not") + } +} + +func TestGenID(t *testing.T) { + // Generate an ID + generatedID := ident.GenID() + + // Check that the generated ID is valid + if !ident.Validate(generatedID) { + t.Errorf("Generated ID is not valid") + } +} + +func TestGenSize(t *testing.T) { + // Check that the size of the generated ID is correct + if ident.Size() != bitSize { + t.Errorf("Expected size of generated ID to be %d, but it was %d", bitSize, ident.Size()) + } +} + +func TestValidate(t *testing.T) { + // Create a new identifier + generator := New() + + // Generate an ID + generatedID := generator.Gen() + + // Check that the generated ID is valid + if !ident.Validate(generatedID) { + t.Errorf("Generated ID is not valid") + } + + // Check that an invalid ID is not valid + if ident.Validate("invalid") { + t.Errorf("Invalid ID is valid") + } +}