uuid.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package uuid
  2. import (
  3. "bytes"
  4. "fmt"
  5. "github.com/golang-plus/uuid/internal"
  6. "github.com/golang-plus/uuid/internal/dcesecurity"
  7. "github.com/golang-plus/uuid/internal/namebased/md5"
  8. "github.com/golang-plus/uuid/internal/namebased/sha1"
  9. "github.com/golang-plus/uuid/internal/random"
  10. "github.com/golang-plus/uuid/internal/timebased"
  11. )
  12. // UUID respresents an UUID type compliant with specification in RFC 4122.
  13. type UUID [16]byte
  14. // Layout returns layout of UUID.
  15. func (u UUID) Layout() Layout {
  16. return Layout(internal.GetLayout(u[:]))
  17. }
  18. // Version returns version of UUID.
  19. func (u UUID) Version() Version {
  20. return Version(internal.GetVersion(u[:]))
  21. }
  22. // Equal returns true if current uuid equal to passed uuid.
  23. func (u UUID) Equal(another UUID) bool {
  24. return bytes.EqualFold(u[:], another[:])
  25. }
  26. // Format returns the formatted string of UUID.
  27. func (u UUID) Format(style Style) string {
  28. switch style {
  29. case StyleWithoutDash:
  30. return fmt.Sprintf("%x", u[:])
  31. //case StyleStandard:
  32. default:
  33. return fmt.Sprintf("%08x-%04x-%04x-%04x-%012x", u[:4], u[4:6], u[6:8], u[8:10], u[10:])
  34. }
  35. }
  36. // String returns the string of UUID with standard style(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx | 8-4-4-4-12).
  37. func (u UUID) String() string {
  38. return u.Format(StyleStandard)
  39. }
  40. var (
  41. // Nil represents the Nil UUID (00000000-0000-0000-0000-000000000000).
  42. Nil = UUID{}
  43. )
  44. // NewTimeBased returns a new time based UUID (version 1).
  45. func NewTimeBased() (UUID, error) {
  46. u, err := timebased.NewUUID()
  47. if err != nil {
  48. return Nil, err
  49. }
  50. uuid := UUID{}
  51. copy(uuid[:], u)
  52. return uuid, nil
  53. }
  54. // NewV1 is short of NewTimeBased.
  55. func NewV1() (UUID, error) {
  56. return NewTimeBased()
  57. }
  58. // NewDCESecurity returns a new DCE security UUID (version 2).
  59. func NewDCESecurity(domain Domain) (UUID, error) {
  60. u, err := dcesecurity.NewUUID(dcesecurity.Domain(domain))
  61. if err != nil {
  62. return Nil, err
  63. }
  64. uuid := UUID{}
  65. copy(uuid[:], u)
  66. return uuid, nil
  67. }
  68. // NewV2 is short of NewDCESecurity.
  69. func NewV2(domain Domain) (UUID, error) {
  70. return NewDCESecurity(domain)
  71. }
  72. // NewNameBasedMD5 returns a new name based UUID with MD5 hash (version 3).
  73. func NewNameBasedMD5(namespace, name string) (UUID, error) {
  74. u, err := md5.NewUUID(namespace, name)
  75. if err != nil {
  76. return Nil, err
  77. }
  78. uuid := UUID{}
  79. copy(uuid[:], u)
  80. return uuid, nil
  81. }
  82. // NewV3 is short of NewNameBasedMD5.
  83. func NewV3(namespace, name string) (UUID, error) {
  84. return NewNameBasedMD5(namespace, name)
  85. }
  86. // NewRandom returns a new random UUID (version 4).
  87. func NewRandom() (UUID, error) {
  88. u, err := random.NewUUID()
  89. if err != nil {
  90. return Nil, err
  91. }
  92. uuid := UUID{}
  93. copy(uuid[:], u)
  94. return uuid, nil
  95. }
  96. // NewV4 is short of NewRandom.
  97. func NewV4() (UUID, error) {
  98. return NewRandom()
  99. }
  100. // New is short of NewRandom.
  101. func New() (UUID, error) {
  102. return NewRandom()
  103. }
  104. // NewNameBasedSHA1 returns a new name based UUID with SHA1 hash (version 5).
  105. func NewNameBasedSHA1(namespace, name string) (UUID, error) {
  106. u, err := sha1.NewUUID(namespace, name)
  107. if err != nil {
  108. return Nil, err
  109. }
  110. uuid := UUID{}
  111. copy(uuid[:], u)
  112. return uuid, nil
  113. }
  114. // NewV5 is short of NewNameBasedSHA1.
  115. func NewV5(namespace, name string) (UUID, error) {
  116. return NewNameBasedSHA1(namespace, name)
  117. }