github.com/cockroachdb/cockroachdb-parser@v0.23.3-0.20240213214944-911057d40c9a/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 "math/rand"
    18  
    19  	"github.com/cockroachdb/cockroachdb-parser/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  // mathRandReader is an io.Reader that calls through to "math/rand".Read
   113  // which is safe for concurrent use.
   114  type mathRandReader struct{}
   115  
   116  func (r mathRandReader) Read(p []byte) (n int, err error) {
   117  	// https://github.com/cockroachdb/cockroachdb-parser/issues/110597 tracks this
   118  	// deprecated usage.
   119  	//lint:ignore SA1019 deprecated
   120  	return math_rand.Read(p)
   121  }
   122  
   123  // fastGen is a non-cryptographically secure Generator.
   124  var fastGen = NewGenWithReader(mathRandReader{})
   125  
   126  // NewPopulatedUUID returns a populated UUID.
   127  func NewPopulatedUUID(r interface {
   128  	Uint32() uint32
   129  }) *UUID {
   130  	var u UUID
   131  	binary.LittleEndian.PutUint32(u[:4], r.Uint32())
   132  	binary.LittleEndian.PutUint32(u[4:8], r.Uint32())
   133  	binary.LittleEndian.PutUint32(u[8:12], r.Uint32())
   134  	binary.LittleEndian.PutUint32(u[12:], r.Uint32())
   135  	return &u
   136  }
   137  
   138  // FromUint128 delegates to FromBytes and wraps the result in a UUID.
   139  func FromUint128(input uint128.Uint128) UUID {
   140  	u, err := FromBytes(input.GetBytes())
   141  	if err != nil {
   142  		panic(errors.Wrap(err, "should never happen with 16 byte slice"))
   143  	}
   144  	return u
   145  }