github.com/datastax/go-cassandra-native-protocol@v0.0.0-20220706104457-5e8aad05cf90/primitive/uuid.go (about)

     1  // Copyright 2020 DataStax
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package primitive
    16  
    17  import (
    18  	"errors"
    19  	"fmt"
    20  	"io"
    21  )
    22  
    23  // [uuid]
    24  
    25  const LengthOfUuid = 16
    26  
    27  type UUID [16]byte
    28  
    29  func (u *UUID) DeepCopy() *UUID {
    30  	if u == nil {
    31  		return nil
    32  	}
    33  	newUuid := *u
    34  	return &newUuid
    35  }
    36  
    37  func (u *UUID) String() string {
    38  	if u == nil {
    39  		return ""
    40  	}
    41  
    42  	var offsets = [...]int{0, 2, 4, 6, 9, 11, 14, 16, 19, 21, 24, 26, 28, 30, 32, 34}
    43  	const hexString = "0123456789abcdef"
    44  	r := make([]byte, 36)
    45  	for i, b := range u {
    46  		r[offsets[i]] = hexString[b>>4]
    47  		r[offsets[i]+1] = hexString[b&0xF]
    48  	}
    49  	r[8] = '-'
    50  	r[13] = '-'
    51  	r[18] = '-'
    52  	r[23] = '-'
    53  	return string(r)
    54  }
    55  
    56  // Bytes returns the raw byte slice for this UUID. A UUID is always 128 bits
    57  // (16 bytes) long.
    58  func (u *UUID) Bytes() []byte {
    59  	return u[:]
    60  }
    61  
    62  // ParseUuid parses a 32 digit hexadecimal number (that might contain hyphens)
    63  // representing an UUID.
    64  func ParseUuid(input string) (*UUID, error) {
    65  	var u UUID
    66  	i := 0
    67  	for _, r := range input {
    68  		switch {
    69  		case r == '-' && i&1 == 0:
    70  			continue
    71  		case r >= '0' && r <= '9' && i < 32:
    72  			u[i/2] |= byte(r-'0') << uint(4-i&1*4)
    73  		case r >= 'a' && r <= 'f' && i < 32:
    74  			u[i/2] |= byte(r-'a'+10) << uint(4-i&1*4)
    75  		case r >= 'A' && r <= 'F' && i < 32:
    76  			u[i/2] |= byte(r-'A'+10) << uint(4-i&1*4)
    77  		default:
    78  			return nil, fmt.Errorf("invalid UUID: %q", input)
    79  		}
    80  		i += 1
    81  	}
    82  	if i != 32 {
    83  		return nil, fmt.Errorf("invalid UUID: %q", input)
    84  	}
    85  	return &u, nil
    86  }
    87  
    88  func ReadUuid(source io.Reader) (*UUID, error) {
    89  	decoded := new(UUID)
    90  	if _, err := io.ReadFull(source, decoded[:]); err != nil {
    91  		return nil, fmt.Errorf("cannot read [uuid] content: %w", err)
    92  	}
    93  	return decoded, nil
    94  }
    95  
    96  func WriteUuid(uuid *UUID, dest io.Writer) error {
    97  	if uuid == nil {
    98  		return errors.New("cannot write nil [uuid]")
    99  	} else if n, err := dest.Write(uuid[:]); err != nil {
   100  		return fmt.Errorf("cannot write [uuid] content: %w", err)
   101  	} else if n < LengthOfUuid {
   102  		return errors.New("not enough capacity to write [uuid] content")
   103  	}
   104  	return nil
   105  }