Skip to content

Commit

Permalink
refactor(ident): This update contains some major changes
Browse files Browse the repository at this point in the history
Separate subprojects
Single registration at import
Only one ID generator can be registered at a time
  • Loading branch information
godcong committed Jul 11, 2024
1 parent 1b58298 commit 1c837a5
Show file tree
Hide file tree
Showing 11 changed files with 344 additions and 7 deletions.
7 changes: 7 additions & 0 deletions ident/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
10 changes: 10 additions & 0 deletions ident/go.sum
Original file line number Diff line number Diff line change
@@ -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=
6 changes: 3 additions & 3 deletions ident/ident.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand All @@ -49,6 +49,6 @@ func Validate(id string) bool {

var (
_ = GenID
_ = GenSize
_ = Size
_ = Validate
)
2 changes: 1 addition & 1 deletion ident/snowflake/snowflake.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
80 changes: 80 additions & 0 deletions ident/snowflake/snowflake_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
2 changes: 1 addition & 1 deletion ident/ulid/ulid.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
80 changes: 80 additions & 0 deletions ident/ulid/ulid_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
2 changes: 1 addition & 1 deletion ident/uuid/uuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
80 changes: 80 additions & 0 deletions ident/uuid/uuid_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
2 changes: 1 addition & 1 deletion ident/xid/xid.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading

0 comments on commit 1c837a5

Please sign in to comment.