github.com/datastax/go-cassandra-native-protocol@v0.0.0-20220706104457-5e8aad05cf90/primitive/util.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 ) 20 21 // SupportedProtocolVersions returns a slice containing all the protocol versions supported by this library. 22 func SupportedProtocolVersions() []ProtocolVersion { 23 return []ProtocolVersion{ 24 ProtocolVersion2, 25 ProtocolVersion3, 26 ProtocolVersion4, 27 ProtocolVersion5, 28 ProtocolVersionDse1, 29 ProtocolVersionDse2, 30 } 31 } 32 33 func SupportedOssProtocolVersions() []ProtocolVersion { 34 return matchingProtocolVersions(func(v ProtocolVersion) bool { return v.IsOss() }) 35 } 36 37 func SupportedDseProtocolVersions() []ProtocolVersion { 38 return matchingProtocolVersions(func(v ProtocolVersion) bool { return v.IsDse() }) 39 } 40 41 func SupportedBetaProtocolVersions() []ProtocolVersion { 42 return matchingProtocolVersions(func(v ProtocolVersion) bool { return v.IsBeta() }) 43 } 44 45 func SupportedNonBetaProtocolVersions() []ProtocolVersion { 46 return matchingProtocolVersions(func(v ProtocolVersion) bool { return !v.IsBeta() }) 47 } 48 49 func SupportedProtocolVersionsGreaterThanOrEqualTo(version ProtocolVersion) []ProtocolVersion { 50 return matchingProtocolVersions(func(v ProtocolVersion) bool { return v >= version }) 51 } 52 53 func SupportedProtocolVersionsGreaterThan(version ProtocolVersion) []ProtocolVersion { 54 return matchingProtocolVersions(func(v ProtocolVersion) bool { return v > version }) 55 } 56 57 func SupportedProtocolVersionsLesserThanOrEqualTo(version ProtocolVersion) []ProtocolVersion { 58 return matchingProtocolVersions(func(v ProtocolVersion) bool { return v <= version }) 59 } 60 61 func SupportedProtocolVersionsLesserThan(version ProtocolVersion) []ProtocolVersion { 62 return matchingProtocolVersions(func(v ProtocolVersion) bool { return v < version }) 63 } 64 65 func matchingProtocolVersions(filters ...func(version ProtocolVersion) bool) []ProtocolVersion { 66 var result []ProtocolVersion 67 for _, v := range SupportedProtocolVersions() { 68 include := true 69 for _, filter := range filters { 70 if !filter(v) { 71 include = false 72 break 73 } 74 } 75 if include { 76 result = append(result, v) 77 } 78 } 79 return result 80 } 81 82 func CheckSupportedProtocolVersion(version ProtocolVersion) error { 83 if !version.IsSupported() { 84 return fmt.Errorf("invalid protocol version: %v", version) 85 } 86 return nil 87 } 88 89 func CheckDseProtocolVersion(version ProtocolVersion) error { 90 if !version.IsDse() { 91 return fmt.Errorf("invalid DSE protocol version: %v", version) 92 } 93 return nil 94 } 95 96 func CheckValidOpCode(code OpCode) error { 97 if !code.IsValid() { 98 return fmt.Errorf("invalid opcode: %v", code) 99 } 100 return nil 101 } 102 103 func CheckRequestOpCode(code OpCode) error { 104 if !code.IsRequest() { 105 return fmt.Errorf("expected request opcode, but got: %v", code) 106 } 107 return nil 108 } 109 110 func CheckResponseOpCode(code OpCode) error { 111 if !code.IsResponse() { 112 return fmt.Errorf("expected response opcode, but got: %v", code) 113 } 114 return nil 115 } 116 117 func CheckValidConsistencyLevel(consistency ConsistencyLevel) error { 118 if !consistency.IsValid() { 119 return fmt.Errorf("invalid consistency level: %v", consistency) 120 } 121 return nil 122 } 123 124 func CheckSerialConsistencyLevel(consistency ConsistencyLevel) error { 125 if !consistency.IsSerial() { 126 return fmt.Errorf("invalid serial consistency level: %v", consistency) 127 } 128 return nil 129 } 130 131 func CheckValidEventType(eventType EventType) error { 132 if !eventType.IsValid() { 133 return fmt.Errorf("invalid event type: %v", eventType) 134 } 135 return nil 136 } 137 138 func CheckValidWriteType(writeType WriteType) error { 139 if !writeType.IsValid() { 140 return fmt.Errorf("invalid write type: %v", writeType) 141 } 142 return nil 143 } 144 145 func CheckValidBatchType(batchType BatchType) error { 146 if !batchType.IsValid() { 147 return fmt.Errorf("invalid BATCH type: %v", batchType) 148 } 149 return nil 150 } 151 152 func CheckValidDataTypeCode(code DataTypeCode, version ProtocolVersion) error { 153 if !code.IsValid() || !version.SupportsDataType(code) { 154 return fmt.Errorf("invalid data type code for %v: %v", version, code) 155 } 156 return nil 157 } 158 159 func CheckValidSchemaChangeType(t SchemaChangeType) error { 160 if !t.IsValid() { 161 return fmt.Errorf("invalid schema change type: %v", t) 162 } 163 return nil 164 } 165 166 func CheckValidSchemaChangeTarget(target SchemaChangeTarget, version ProtocolVersion) error { 167 if !target.IsValid() || !version.SupportsSchemaChangeTarget(target) { 168 return fmt.Errorf("invalid schema change target for %v: %v", version, target) 169 } 170 return nil 171 } 172 173 func CheckValidStatusChangeType(t StatusChangeType) error { 174 if !t.IsValid() { 175 return fmt.Errorf("invalid status change type: %v", t) 176 } 177 return nil 178 } 179 180 func CheckValidTopologyChangeType(t TopologyChangeType, version ProtocolVersion) error { 181 if !t.IsValid() || !version.SupportsTopologyChangeType(t) { 182 return fmt.Errorf("invalid topology change type for %v: %v", version, t) 183 } 184 return nil 185 } 186 187 func CheckValidResultType(t ResultType) error { 188 if !t.IsValid() { 189 return fmt.Errorf("invalid result type: %v", t) 190 } 191 return nil 192 } 193 194 func CheckValidDseRevisionType(t DseRevisionType, version ProtocolVersion) error { 195 if !t.IsValid() || !version.SupportsDseRevisionType(t) { 196 return fmt.Errorf("invalid DSE revision type for %v: %v", version, t) 197 } 198 return nil 199 } 200 201 func CheckValidFailureCode(c FailureCode) error { 202 if !c.IsValid() { 203 return fmt.Errorf("invalid failure code: %v", c) 204 } 205 return nil 206 }