github.com/datastax/go-cassandra-native-protocol@v0.0.0-20220706104457-5e8aad05cf90/datatype/map.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 datatype
    16  
    17  import (
    18  	"fmt"
    19  	"io"
    20  
    21  	"github.com/datastax/go-cassandra-native-protocol/primitive"
    22  )
    23  
    24  // Map is a data type that represents a CQL map type.
    25  // +k8s:deepcopy-gen=true
    26  // +k8s:deepcopy-gen:interfaces=github.com/datastax/go-cassandra-native-protocol/datatype.DataType
    27  type Map struct {
    28  	KeyType   DataType
    29  	ValueType DataType
    30  }
    31  
    32  func (t *Map) Code() primitive.DataTypeCode {
    33  	return primitive.DataTypeCodeMap
    34  }
    35  
    36  func (t *Map) String() string {
    37  	return t.AsCql()
    38  }
    39  
    40  func (t *Map) AsCql() string {
    41  	return fmt.Sprintf("map<%v,%v>", t.KeyType.AsCql(), t.ValueType.AsCql())
    42  }
    43  
    44  func NewMap(keyType DataType, valueType DataType) *Map {
    45  	return &Map{KeyType: keyType, ValueType: valueType}
    46  }
    47  
    48  func writeMapType(t DataType, dest io.Writer, version primitive.ProtocolVersion) (err error) {
    49  	mapType, ok := t.(*Map)
    50  	if !ok {
    51  		return fmt.Errorf("expected *Map, got %T", t)
    52  	} else if err = WriteDataType(mapType.KeyType, dest, version); err != nil {
    53  		return fmt.Errorf("cannot write map key type: %w", err)
    54  	} else if err = WriteDataType(mapType.ValueType, dest, version); err != nil {
    55  		return fmt.Errorf("cannot write map value type: %w", err)
    56  	}
    57  	return nil
    58  }
    59  
    60  func lengthOfMapType(t DataType, version primitive.ProtocolVersion) (length int, err error) {
    61  	mapType, ok := t.(*Map)
    62  	if !ok {
    63  		return -1, fmt.Errorf("expected *Map, got %T", t)
    64  	}
    65  	if keyLength, err := LengthOfDataType(mapType.KeyType, version); err != nil {
    66  		return -1, fmt.Errorf("cannot compute length of map key type: %w", err)
    67  	} else {
    68  		length += keyLength
    69  	}
    70  	if valueLength, err := LengthOfDataType(mapType.ValueType, version); err != nil {
    71  		return -1, fmt.Errorf("cannot compute length of map value type: %w", err)
    72  	} else {
    73  		length += valueLength
    74  	}
    75  	return length, nil
    76  }
    77  
    78  func readMapType(source io.Reader, version primitive.ProtocolVersion) (decoded DataType, err error) {
    79  	mapType := &Map{}
    80  	if mapType.KeyType, err = ReadDataType(source, version); err != nil {
    81  		return nil, fmt.Errorf("cannot read map key type: %w", err)
    82  	} else if mapType.ValueType, err = ReadDataType(source, version); err != nil {
    83  		return nil, fmt.Errorf("cannot read map value type: %w", err)
    84  	}
    85  	return mapType, nil
    86  }