github.com/datastax/go-cassandra-native-protocol@v0.0.0-20220706104457-5e8aad05cf90/datatype/tuple.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 "bytes" 19 "fmt" 20 "io" 21 22 "github.com/datastax/go-cassandra-native-protocol/primitive" 23 ) 24 25 // Tuple is a data type that represents a CQL tuple type. 26 // +k8s:deepcopy-gen=true 27 // +k8s:deepcopy-gen:interfaces=github.com/datastax/go-cassandra-native-protocol/datatype.DataType 28 type Tuple struct { 29 FieldTypes []DataType 30 } 31 32 func NewTuple(fieldTypes ...DataType) *Tuple { 33 return &Tuple{FieldTypes: fieldTypes} 34 } 35 36 func (t *Tuple) Code() primitive.DataTypeCode { 37 return primitive.DataTypeCodeTuple 38 } 39 40 func (t *Tuple) String() string { 41 return t.AsCql() 42 } 43 44 func (t *Tuple) AsCql() string { 45 buf := &bytes.Buffer{} 46 buf.WriteString("tuple<") 47 for i, elementType := range t.FieldTypes { 48 if i > 0 { 49 buf.WriteString(",") 50 } 51 buf.WriteString(elementType.AsCql()) 52 } 53 buf.WriteString(">") 54 return buf.String() 55 } 56 57 func writeTupleType(t DataType, dest io.Writer, version primitive.ProtocolVersion) (err error) { 58 tupleType, ok := t.(*Tuple) 59 if !ok { 60 return fmt.Errorf("expected *Tuple, got %T", t) 61 } else if err = primitive.WriteShort(uint16(len(tupleType.FieldTypes)), dest); err != nil { 62 return fmt.Errorf("cannot write tuple type field count: %w", err) 63 } 64 for i, fieldType := range tupleType.FieldTypes { 65 if err = WriteDataType(fieldType, dest, version); err != nil { 66 return fmt.Errorf("cannot write tuple field %d: %w", i, err) 67 } 68 } 69 return nil 70 } 71 72 func lengthOfTupleType(t DataType, version primitive.ProtocolVersion) (int, error) { 73 if tupleType, ok := t.(*Tuple); !ok { 74 return -1, fmt.Errorf("expected *Tuple, got %T", t) 75 } else { 76 length := primitive.LengthOfShort // field count 77 for i, fieldType := range tupleType.FieldTypes { 78 if fieldLength, err := LengthOfDataType(fieldType, version); err != nil { 79 return -1, fmt.Errorf("cannot compute length of tuple field %d: %w", i, err) 80 } else { 81 length += fieldLength 82 } 83 } 84 return length, nil 85 } 86 } 87 88 func readTupleType(source io.Reader, version primitive.ProtocolVersion) (DataType, error) { 89 if fieldCount, err := primitive.ReadShort(source); err != nil { 90 return nil, fmt.Errorf("cannot read tuple field count: %w", err) 91 } else { 92 tupleType := &Tuple{} 93 tupleType.FieldTypes = make([]DataType, fieldCount) 94 for i := 0; i < int(fieldCount); i++ { 95 if tupleType.FieldTypes[i], err = ReadDataType(source, version); err != nil { 96 return nil, fmt.Errorf("cannot read tuple field %d: %w", i, err) 97 } 98 } 99 return tupleType, nil 100 } 101 }