uuid.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Copyright 2011 Google Inc. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package uuid
  5. import (
  6. "bytes"
  7. "crypto/rand"
  8. "encoding/hex"
  9. "io"
  10. guuid "github.com/google/uuid"
  11. )
  12. // Array is a pass-by-value UUID that can be used as an effecient key in a map.
  13. type Array [16]byte
  14. // UUID converts uuid into a slice.
  15. func (uuid Array) UUID() UUID {
  16. return uuid[:]
  17. }
  18. // String returns the string representation of uuid,
  19. // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.
  20. func (uuid Array) String() string {
  21. return guuid.UUID(uuid).String()
  22. }
  23. // A UUID is a 128 bit (16 byte) Universal Unique IDentifier as defined in RFC
  24. // 4122.
  25. type UUID []byte
  26. // A Version represents a UUIDs version.
  27. type Version = guuid.Version
  28. // A Variant represents a UUIDs variant.
  29. type Variant = guuid.Variant
  30. // Constants returned by Variant.
  31. const (
  32. Invalid = guuid.Invalid // Invalid UUID
  33. RFC4122 = guuid.RFC4122 // The variant specified in RFC4122
  34. Reserved = guuid.Reserved // Reserved, NCS backward compatibility.
  35. Microsoft = guuid.Microsoft // Reserved, Microsoft Corporation backward compatibility.
  36. Future = guuid.Future // Reserved for future definition.
  37. )
  38. var rander = rand.Reader // random function
  39. // New returns a new random (version 4) UUID as a string. It is a convenience
  40. // function for NewRandom().String().
  41. func New() string {
  42. return NewRandom().String()
  43. }
  44. // Parse decodes s into a UUID or returns nil. Both the UUID form of
  45. // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx and
  46. // urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx are decoded.
  47. func Parse(s string) UUID {
  48. gu, err := guuid.Parse(s)
  49. if err == nil {
  50. return gu[:]
  51. }
  52. return nil
  53. }
  54. // ParseBytes is like Parse, except it parses a byte slice instead of a string.
  55. func ParseBytes(b []byte) (UUID, error) {
  56. gu, err := guuid.ParseBytes(b)
  57. if err == nil {
  58. return gu[:], nil
  59. }
  60. return nil, err
  61. }
  62. // Equal returns true if uuid1 and uuid2 are equal.
  63. func Equal(uuid1, uuid2 UUID) bool {
  64. return bytes.Equal(uuid1, uuid2)
  65. }
  66. // Array returns an array representation of uuid that can be used as a map key.
  67. // Array panics if uuid is not valid.
  68. func (uuid UUID) Array() Array {
  69. if len(uuid) != 16 {
  70. panic("invalid uuid")
  71. }
  72. var a Array
  73. copy(a[:], uuid)
  74. return a
  75. }
  76. // String returns the string form of uuid, xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
  77. // , or "" if uuid is invalid.
  78. func (uuid UUID) String() string {
  79. if len(uuid) != 16 {
  80. return ""
  81. }
  82. var buf [36]byte
  83. encodeHex(buf[:], uuid)
  84. return string(buf[:])
  85. }
  86. // URN returns the RFC 2141 URN form of uuid,
  87. // urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, or "" if uuid is invalid.
  88. func (uuid UUID) URN() string {
  89. if len(uuid) != 16 {
  90. return ""
  91. }
  92. var buf [36 + 9]byte
  93. copy(buf[:], "urn:uuid:")
  94. encodeHex(buf[9:], uuid)
  95. return string(buf[:])
  96. }
  97. func encodeHex(dst []byte, uuid UUID) {
  98. hex.Encode(dst[:], uuid[:4])
  99. dst[8] = '-'
  100. hex.Encode(dst[9:13], uuid[4:6])
  101. dst[13] = '-'
  102. hex.Encode(dst[14:18], uuid[6:8])
  103. dst[18] = '-'
  104. hex.Encode(dst[19:23], uuid[8:10])
  105. dst[23] = '-'
  106. hex.Encode(dst[24:], uuid[10:])
  107. }
  108. // Variant returns the variant encoded in uuid. It returns Invalid if
  109. // uuid is invalid.
  110. func (uuid UUID) Variant() Variant {
  111. if len(uuid) != 16 {
  112. return Invalid
  113. }
  114. switch {
  115. case (uuid[8] & 0xc0) == 0x80:
  116. return RFC4122
  117. case (uuid[8] & 0xe0) == 0xc0:
  118. return Microsoft
  119. case (uuid[8] & 0xe0) == 0xe0:
  120. return Future
  121. default:
  122. return Reserved
  123. }
  124. }
  125. // Version returns the version of uuid. It returns false if uuid is not
  126. // valid.
  127. func (uuid UUID) Version() (Version, bool) {
  128. if len(uuid) != 16 {
  129. return 0, false
  130. }
  131. return Version(uuid[6] >> 4), true
  132. }
  133. // SetRand sets the random number generator to r, which implements io.Reader.
  134. // If r.Read returns an error when the package requests random data then
  135. // a panic will be issued.
  136. //
  137. // Calling SetRand with nil sets the random number generator to the default
  138. // generator.
  139. func SetRand(r io.Reader) {
  140. guuid.SetRand(r)
  141. }