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 a7c7846 commit c926a0d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 53 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* `[uuid]` `GenUUID` renamed to `UUID`
* `[uuid]` `GenUUID4` renamed to `UUID4`
* `[uuid]` `GenUUID5` renamed to `UUID5`
* `[uuid]` Code refactoring

### 12.80.0

Expand Down
8 changes: 2 additions & 6 deletions uuid/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,10 @@ import (

// ////////////////////////////////////////////////////////////////////////////////// //

func ExampleGenUUID() {
fmt.Printf("UUID: %s\n", GenUUID())
}

func ExampleGenUUID4() {
fmt.Printf("UUID v4: %s\n", GenUUID4())
fmt.Printf("UUID v4: %s\n", UUID4().String())
}

func ExampleGenUUID5() {
fmt.Printf("UUID v5: %s\n", GenUUID5(NsURL, "http://www.domain.com"))
fmt.Printf("UUID v5: %s\n", UUID5(NsURL, "http://www.domain.com").String())
}
79 changes: 41 additions & 38 deletions uuid/uuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,49 +26,26 @@ var (

// ////////////////////////////////////////////////////////////////////////////////// //

// GenUUID generates v4 UUID (Universally Unique Identifier)
//
// Deprecated: Use method UUID instead
func GenUUID() string {
return UUID4()
}
// UUID contains UUID data
type UUID []byte

// 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)
func UUID4() UUID {
uuid := make(UUID, 16)

rand.Read(uuid)

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)
return UUID(uuid)
}

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

hash := sha1.New()
hash.Write(ns)
Expand All @@ -79,23 +56,49 @@ func UUID5(ns []byte, name string) string {
uuid[6] = (uuid[6] & 0x0F) | 0x50
uuid[8] = (uuid[8] & 0x3F) | 0x80

return toString(uuid)
return UUID(uuid)
}

// ////////////////////////////////////////////////////////////////////////////////// //

func toString(uuid []byte) string {
// String returns string representation of UUID
func (u UUID) String() string {
buf := make([]byte, 36)

hex.Encode(buf[0:8], uuid[0:4])
hex.Encode(buf[0:8], u[0:4])
buf[8] = '-'
hex.Encode(buf[9:13], uuid[4:6])
hex.Encode(buf[9:13], u[4:6])
buf[13] = '-'
hex.Encode(buf[14:18], uuid[6:8])
hex.Encode(buf[14:18], u[6:8])
buf[18] = '-'
hex.Encode(buf[19:23], uuid[8:10])
hex.Encode(buf[19:23], u[8:10])
buf[23] = '-'
hex.Encode(buf[24:], uuid[10:])
hex.Encode(buf[24:], u[10:])

return string(buf)
}

// ////////////////////////////////////////////////////////////////////////////////// //

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

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

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

// ////////////////////////////////////////////////////////////////////////////////// //
13 changes: 4 additions & 9 deletions uuid/uuid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,14 @@ var _ = Suite(&UUIDSuite{})

// ////////////////////////////////////////////////////////////////////////////////// //

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

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

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

func (s *UUIDSuite) TestDeprecated(c *C) {
Expand Down

0 comments on commit c926a0d

Please sign in to comment.