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. See github.com/google/uuid for
  45. // the formats parsed.
  46. func Parse(s string) UUID {
  47. gu, err := guuid.Parse(s)
  48. if err == nil {
  49. return gu[:]
  50. }
  51. return nil
  52. }
  53. // ParseBytes is like Parse, except it parses a byte slice instead of a string.
  54. func ParseBytes(b []byte) (UUID, error) {
  55. gu, err := guuid.ParseBytes(b)
  56. if err == nil {
  57. return gu[:], nil
  58. }
  59. return nil, err
  60. }
  61. // Equal returns true if uuid1 and uuid2 are equal.
  62. func Equal(uuid1, uuid2 UUID) bool {
  63. return bytes.Equal(uuid1, uuid2)
  64. }
  65. // Array returns an array representation of uuid that can be used as a map key.
  66. // Array panics if uuid is not valid.
  67. func (uuid UUID) Array() Array {
  68. if len(uuid) != 16 {
  69. panic("invalid uuid")
  70. }
  71. var a Array
  72. copy(a[:], uuid)
  73. return a
  74. }
  75. // String returns the string form of uuid, xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
  76. // , or "" if uuid is invalid.
  77. func (uuid UUID) String() string {
  78. if len(uuid) != 16 {
  79. return ""
  80. }
  81. var buf [36]byte
  82. encodeHex(buf[:], uuid)
  83. return string(buf[:])
  84. }
  85. // URN returns the RFC 2141 URN form of uuid,
  86. // urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, or "" if uuid is invalid.
  87. func (uuid UUID) URN() string {
  88. if len(uuid) != 16 {
  89. return ""
  90. }
  91. var buf [36 + 9]byte
  92. copy(buf[:], "urn:uuid:")
  93. encodeHex(buf[9:], uuid)
  94. return string(buf[:])
  95. }
  96. func encodeHex(dst []byte, uuid UUID) {
  97. hex.Encode(dst[:], uuid[:4])
  98. dst[8] = '-'
  99. hex.Encode(dst[9:13], uuid[4:6])
  100. dst[13] = '-'
  101. hex.Encode(dst[14:18], uuid[6:8])
  102. dst[18] = '-'
  103. hex.Encode(dst[19:23], uuid[8:10])
  104. dst[23] = '-'
  105. hex.Encode(dst[24:], uuid[10:])
  106. }
  107. // Variant returns the variant encoded in uuid. It returns Invalid if
  108. // uuid is invalid.
  109. func (uuid UUID) Variant() Variant {
  110. if len(uuid) != 16 {
  111. return Invalid
  112. }
  113. switch {
  114. case (uuid[8] & 0xc0) == 0x80:
  115. return RFC4122
  116. case (uuid[8] & 0xe0) == 0xc0:
  117. return Microsoft
  118. case (uuid[8] & 0xe0) == 0xe0:
  119. return Future
  120. default:
  121. return Reserved
  122. }
  123. }
  124. // Version returns the version of uuid. It returns false if uuid is not
  125. // valid.
  126. func (uuid UUID) Version() (Version, bool) {
  127. if len(uuid) != 16 {
  128. return 0, false
  129. }
  130. return Version(uuid[6] >> 4), true
  131. }
  132. // SetRand sets the random number generator to r, which implements io.Reader.
  133. // If r.Read returns an error when the package requests random data then
  134. // a panic will be issued.
  135. //
  136. // Calling SetRand with nil sets the random number generator to the default
  137. // generator.
  138. func SetRand(r io.Reader) {
  139. guuid.SetRand(r)
  140. }