Skip to content

Commit

Permalink
[uuid] Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Oct 16, 2023
1 parent fb373c0 commit a7c7846
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
### 12.81.0

* `[strutil]` Added method `ReplaceIgnoreCase`
* `[uuid]` `GenUUID` renamed to `UUID`
* `[uuid]` `GenUUID4` renamed to `UUID4`
* `[uuid]` `GenUUID5` renamed to `UUID5`

### 12.80.0

Expand Down
33 changes: 28 additions & 5 deletions uuid/uuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,47 @@ var (
// ////////////////////////////////////////////////////////////////////////////////// //

// GenUUID generates v4 UUID (Universally Unique Identifier)
//
// Deprecated: Use method UUID instead
func GenUUID() string {
return GenUUID4()
return UUID4()
}

// UUID4 generates v4 UUID (Universally Unique Identifier)
func UUID() string {
return UUID4()
}

// GenUUID4 generates random generated UUID
//
// Deprecated: Use method UUID4 instead
func GenUUID4() string {
return UUID4()
}

// UUID4 generates random generated UUID
//
// Deprecated: Use method UUID4 instead
func UUID4() string {
uuid := make([]byte, 16)

rand.Read(uuid)

uuid[6] = (uuid[6] & 0X0F) | 0X40
uuid[8] = (uuid[8] & 0X3F) | 0X80
uuid[6] = (uuid[6] & 0x0F) | 0x40
uuid[8] = (uuid[8] & 0x3F) | 0x80

return toString(uuid)
}

// GenUUID5 generates UUID based on SHA-1 hash of namespace UUID and name
//
// Deprecated: Use method UUID5 instead
func GenUUID5(ns []byte, name string) string {
return UUID5(ns, name)
}

// UUID5 generates UUID based on SHA-1 hash of namespace UUID and name
func UUID5(ns []byte, name string) string {
uuid := make([]byte, 16)

hash := sha1.New()
Expand All @@ -53,8 +76,8 @@ func GenUUID5(ns []byte, name string) string {

copy(uuid, hash.Sum(nil))

uuid[6] = (uuid[6] & 0X0F) | 0x50
uuid[8] = (uuid[8] & 0X3F) | 0x80
uuid[6] = (uuid[6] & 0x0F) | 0x50
uuid[8] = (uuid[8] & 0x3F) | 0x80

return toString(uuid)
}
Expand Down
16 changes: 11 additions & 5 deletions uuid/uuid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,23 @@ var _ = Suite(&UUIDSuite{})
// ////////////////////////////////////////////////////////////////////////////////// //

func (s *UUIDSuite) TestGenUUID(c *C) {
c.Assert(GenUUID(), HasLen, 36)
c.Assert(GenUUID(), Not(Equals), "00000000-0000-0000-0000-000000000000")
c.Assert(UUID(), HasLen, 36)
c.Assert(UUID(), Not(Equals), "00000000-0000-0000-0000-000000000000")
}

func (s *UUIDSuite) TestGenUUID4(c *C) {
c.Assert(GenUUID4(), HasLen, 36)
c.Assert(GenUUID4(), Not(Equals), "00000000-0000-0000-0000-000000000000")
c.Assert(UUID4(), HasLen, 36)
c.Assert(UUID4(), Not(Equals), "00000000-0000-0000-0000-000000000000")
}

func (s *UUIDSuite) TestGenUUID5(c *C) {
c.Assert(GenUUID5(NsURL, "TEST"), HasLen, 36)
c.Assert(UUID5(NsURL, "TEST"), HasLen, 36)
c.Assert(UUID5(NsURL, "TEST"), Not(Equals), "00000000-0000-0000-0000-000000000000")
}

func (s *UUIDSuite) TestDeprecated(c *C) {
c.Assert(GenUUID(), Not(Equals), "00000000-0000-0000-0000-000000000000")
c.Assert(GenUUID4(), Not(Equals), "00000000-0000-0000-0000-000000000000")
c.Assert(GenUUID5(NsURL, "TEST"), Not(Equals), "00000000-0000-0000-0000-000000000000")
}

Expand Down

0 comments on commit a7c7846

Please sign in to comment.