github.com/datastax/go-cassandra-native-protocol@v0.0.0-20220706104457-5e8aad05cf90/primitive/reasonmap.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 "fmt" 19 "io" 20 "net" 21 ) 22 23 // <reasonmap> is a map of endpoint to failure reason codes, available from Protocol Version 5 onwards. 24 // The map is encoded starting with an [int] n followed by n pairs of <endpoint><failurecode> where 25 // <endpoint> is an [inetaddr] and <failurecode> is a [short]. 26 // Note: a reasonmap is inherently a map, but it is not modeled as a map in Go because [inetaddr] 27 // is not a valid map key type. 28 29 // FailureReason is a map entry for a <reasonmap>; it contains the endpoint that failed and the corresponding failure 30 // code. 31 // +k8s:deepcopy-gen=true 32 type FailureReason struct { 33 Endpoint net.IP 34 Code FailureCode 35 } 36 37 func ReadReasonMap(source io.Reader) ([]*FailureReason, error) { 38 if length, err := ReadInt(source); err != nil { 39 return nil, fmt.Errorf("cannot read reason map length: %w", err) 40 } else { 41 reasonMap := make([]*FailureReason, length) 42 for i := 0; i < int(length); i++ { 43 if addr, err := ReadInetAddr(source); err != nil { 44 return nil, fmt.Errorf("cannot read reason map key for element %d: %w", i, err) 45 } else if code, err := ReadShort(source); err != nil { 46 return nil, fmt.Errorf("cannot read reason map value for element %d: %w", i, err) 47 } else if err := CheckValidFailureCode(FailureCode(code)); err != nil { 48 return nil, err 49 } else { 50 reasonMap[i] = &FailureReason{addr, FailureCode(code)} 51 } 52 } 53 return reasonMap, err 54 } 55 } 56 57 func WriteReasonMap(reasonMap []*FailureReason, dest io.Writer) error { 58 if err := WriteInt(int32(len(reasonMap)), dest); err != nil { 59 return fmt.Errorf("cannot write reason map length: %w", err) 60 } 61 for i, reason := range reasonMap { 62 if err := WriteInetAddr(reason.Endpoint, dest); err != nil { 63 return fmt.Errorf("cannot write reason map key for element %d: %w", i, err) 64 } else if err := CheckValidFailureCode(reason.Code); err != nil { 65 return err 66 } else if err = WriteShort(uint16(reason.Code), dest); err != nil { 67 return fmt.Errorf("cannot write reason map value for element %d: %w", i, err) 68 } 69 } 70 return nil 71 } 72 73 func LengthOfReasonMap(reasonMap []*FailureReason) (int, error) { 74 length := LengthOfInt 75 for i, reason := range reasonMap { 76 if inetAddrLength, err := LengthOfInetAddr(reason.Endpoint); err != nil { 77 return -1, fmt.Errorf("cannot compute length of reason map key for element %d: %w", i, err) 78 } else { 79 length += inetAddrLength + LengthOfShort 80 } 81 } 82 return length, nil 83 }