github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/util/uuid/uuid_wrapper.go (about)

     1  // Copyright 2016 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package uuid
    12  
    13  import (
    14  	"encoding/binary"
    15  	"encoding/json"
    16  	"fmt"
    17  	"math/rand"
    18  
    19  	"github.com/cockroachdb/cockroach/pkg/util/uint128"
    20  	"github.com/cockroachdb/errors"
    21  )
    22  
    23  // Short returns the first eight characters of the output of String().
    24  func (u UUID) Short() string {
    25  	return u.String()[:8]
    26  }
    27  
    28  // ShortStringer implements fmt.Stringer to output Short() on String().
    29  type ShortStringer UUID
    30  
    31  // String is part of fmt.Stringer.
    32  func (s ShortStringer) String() string {
    33  	return UUID(s).Short()
    34  }
    35  
    36  var _ fmt.Stringer = ShortStringer{}
    37  
    38  // Equal returns true iff the receiver equals the argument.
    39  //
    40  // This method exists only to conform to the API expected by gogoproto's
    41  // generated Equal implementations.
    42  func (u UUID) Equal(t UUID) bool {
    43  	return u == t
    44  }
    45  
    46  // GetBytes returns the UUID as a byte slice. It incurs an allocation if
    47  // the return value escapes.
    48  func (u UUID) GetBytes() []byte {
    49  	return u.bytes()
    50  }
    51  
    52  // GetBytesMut returns the UUID as a mutable byte slice. Unlike GetBytes,
    53  // it does not necessarily incur an allocation if the return value escapes.
    54  // Instead, the return value escaping will cause the method's receiver (and
    55  // any struct that it is a part of) to escape. Use only if GetBytes is causing
    56  // an allocation and the UUID is already on the heap.
    57  func (u *UUID) GetBytesMut() []byte {
    58  	return u.bytesMut()
    59  }
    60  
    61  // ToUint128 returns the UUID as a Uint128.
    62  func (u UUID) ToUint128() uint128.Uint128 {
    63  	return uint128.FromBytes(u.bytes())
    64  }
    65  
    66  // Size returns the marshaled size of u, in bytes.
    67  func (u UUID) Size() int {
    68  	return len(u)
    69  }
    70  
    71  // MarshalTo marshals u to data.
    72  func (u UUID) MarshalTo(data []byte) (int, error) {
    73  	return copy(data, u.GetBytes()), nil
    74  }
    75  
    76  // Unmarshal unmarshals data to u.
    77  func (u *UUID) Unmarshal(data []byte) error {
    78  	return u.UnmarshalBinary(data)
    79  }
    80  
    81  // MarshalJSON returns the JSON encoding of u.
    82  func (u UUID) MarshalJSON() ([]byte, error) {
    83  	return json.Marshal(u.String())
    84  }
    85  
    86  // UnmarshalJSON unmarshals the JSON encoded data into u.
    87  func (u *UUID) UnmarshalJSON(data []byte) error {
    88  	var uuidString string
    89  	if err := json.Unmarshal(data, &uuidString); err != nil {
    90  		return err
    91  	}
    92  	uuid, err := FromString(uuidString)
    93  	*u = uuid
    94  	return err
    95  }
    96  
    97  // MakeV4 calls Must(NewV4)
    98  func MakeV4() UUID {
    99  	return Must(NewV4())
   100  }
   101  
   102  // FastMakeV4 generates a UUID using a fast but not cryptographically secure
   103  // source of randomness.
   104  func FastMakeV4() UUID {
   105  	u, err := fastGen.NewV4()
   106  	if err != nil {
   107  		panic(errors.Wrap(err, "should never happen with math/rand.Rand"))
   108  	}
   109  	return u
   110  }
   111  
   112  // defaultRandReader is an io.Reader that calls through to "math/rand".Read
   113  // which is safe for concurrent use.
   114  type defaultRandReader struct{}
   115  
   116  func (r defaultRandReader) Read(p []byte) (n int, err error) {
   117  	return rand.Read(p)
   118  }
   119  
   120  // fastGen is a non-cryptographically secure Generator.
   121  var fastGen = NewGenWithReader(defaultRandReader{})
   122  
   123  // NewPopulatedUUID returns a populated UUID.
   124  func NewPopulatedUUID(r interface {
   125  	Int63() int64
   126  }) *UUID {
   127  	var u UUID
   128  	binary.LittleEndian.PutUint64(u[:8], uint64(r.Int63()))
   129  	binary.LittleEndian.PutUint64(u[8:], uint64(r.Int63()))
   130  	return &u
   131  }
   132  
   133  // FromUint128 delegates to FromBytes and wraps the result in a UUID.
   134  func FromUint128(input uint128.Uint128) UUID {
   135  	u, err := FromBytes(input.GetBytes())
   136  	if err != nil {
   137  		panic(errors.Wrap(err, "should never happen with 16 byte slice"))
   138  	}
   139  	return u
   140  }