| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 1 | package sarama |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "time" |
| 6 | ) |
| 7 | |
| 8 | type CreateTopicsResponse struct { |
| 9 | // Version defines the protocol version to use for encode and decode |
| 10 | Version int16 |
| 11 | // ThrottleTime contains the duration for which the request was throttled due |
| 12 | // to a quota violation, or zero if the request did not violate any quota. |
| 13 | ThrottleTime time.Duration |
| 14 | // TopicErrors contains a map of any errors for the topics we tried to create. |
| 15 | TopicErrors map[string]*TopicError |
| 16 | // TopicResults contains a map of the results for the topics we tried to create. |
| 17 | TopicResults map[string]*CreatableTopicResult |
| 18 | } |
| 19 | |
| 20 | func (c *CreateTopicsResponse) setVersion(v int16) { |
| 21 | c.Version = v |
| 22 | } |
| 23 | |
| 24 | func (c *CreateTopicsResponse) encode(pe packetEncoder) error { |
| 25 | if c.Version >= 2 { |
| 26 | pe.putDurationMs(c.ThrottleTime) |
| 27 | } |
| 28 | |
| 29 | if err := pe.putArrayLength(len(c.TopicErrors)); err != nil { |
| 30 | return err |
| 31 | } |
| 32 | for topic, topicError := range c.TopicErrors { |
| 33 | if err := pe.putString(topic); err != nil { |
| 34 | return err |
| 35 | } |
| 36 | if err := topicError.encode(pe, c.Version); err != nil { |
| 37 | return err |
| 38 | } |
| 39 | if c.Version >= 5 { |
| 40 | result, ok := c.TopicResults[topic] |
| 41 | if !ok { |
| 42 | return fmt.Errorf("expected TopicResult for topic, %s, for V5 protocol", topic) |
| 43 | } |
| 44 | if err := result.encode(pe, c.Version); err != nil { |
| 45 | return err |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | pe.putEmptyTaggedFieldArray() |
| 51 | return nil |
| 52 | } |
| 53 | |
| 54 | func (c *CreateTopicsResponse) decode(pd packetDecoder, version int16) (err error) { |
| 55 | c.Version = version |
| 56 | |
| 57 | if version >= 2 { |
| 58 | if c.ThrottleTime, err = pd.getDurationMs(); err != nil { |
| 59 | return err |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | n, err := pd.getArrayLength() |
| 64 | if err != nil { |
| 65 | return err |
| 66 | } |
| 67 | |
| 68 | c.TopicErrors = make(map[string]*TopicError, n) |
| 69 | if version >= 5 { |
| 70 | c.TopicResults = make(map[string]*CreatableTopicResult, n) |
| 71 | } |
| 72 | for i := 0; i < n; i++ { |
| 73 | topic, err := pd.getString() |
| 74 | if err != nil { |
| 75 | return err |
| 76 | } |
| 77 | c.TopicErrors[topic] = new(TopicError) |
| 78 | if err := c.TopicErrors[topic].decode(pd, version); err != nil { |
| 79 | return err |
| 80 | } |
| 81 | if version >= 5 { |
| 82 | c.TopicResults[topic] = &CreatableTopicResult{} |
| 83 | if err := c.TopicResults[topic].decode(pd, version); err != nil { |
| 84 | return err |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | if _, err := pd.getEmptyTaggedFieldArray(); err != nil { |
| 90 | return err |
| 91 | } |
| 92 | return nil |
| 93 | } |
| 94 | |
| 95 | func (c *CreateTopicsResponse) key() int16 { |
| 96 | return apiKeyCreateTopics |
| 97 | } |
| 98 | |
| 99 | func (c *CreateTopicsResponse) version() int16 { |
| 100 | return c.Version |
| 101 | } |
| 102 | |
| 103 | func (c *CreateTopicsResponse) headerVersion() int16 { |
| 104 | if c.Version >= 5 { |
| 105 | return 1 |
| 106 | } |
| 107 | return 0 |
| 108 | } |
| 109 | |
| 110 | func (c *CreateTopicsResponse) isFlexible() bool { |
| 111 | return c.isFlexibleVersion(c.Version) |
| 112 | } |
| 113 | |
| 114 | func (c *CreateTopicsResponse) isFlexibleVersion(version int16) bool { |
| 115 | return version >= 5 |
| 116 | } |
| 117 | |
| 118 | func (c *CreateTopicsResponse) isValidVersion() bool { |
| 119 | return c.Version >= 0 && c.Version <= 5 |
| 120 | } |
| 121 | |
| 122 | func (c *CreateTopicsResponse) requiredVersion() KafkaVersion { |
| 123 | switch c.Version { |
| 124 | case 5: |
| 125 | return V2_4_0_0 |
| 126 | case 4: |
| 127 | return V2_4_0_0 |
| 128 | case 3: |
| 129 | return V2_0_0_0 |
| 130 | case 2: |
| 131 | return V0_11_0_0 |
| 132 | case 1: |
| 133 | return V0_10_2_0 |
| 134 | case 0: |
| 135 | return V0_10_1_0 |
| 136 | default: |
| 137 | return V2_8_0_0 |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | func (r *CreateTopicsResponse) throttleTime() time.Duration { |
| 142 | return r.ThrottleTime |
| 143 | } |
| 144 | |
| 145 | type TopicError struct { |
| 146 | Err KError |
| 147 | ErrMsg *string |
| 148 | } |
| 149 | |
| 150 | func (t *TopicError) Error() string { |
| 151 | text := t.Err.Error() |
| 152 | if t.ErrMsg != nil { |
| 153 | text = fmt.Sprintf("%s - %s", text, *t.ErrMsg) |
| 154 | } |
| 155 | return text |
| 156 | } |
| 157 | |
| 158 | func (t *TopicError) Unwrap() error { |
| 159 | return t.Err |
| 160 | } |
| 161 | |
| 162 | func (t *TopicError) encode(pe packetEncoder, version int16) error { |
| 163 | pe.putKError(t.Err) |
| 164 | |
| 165 | if version >= 1 { |
| 166 | if err := pe.putNullableString(t.ErrMsg); err != nil { |
| 167 | return err |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | return nil |
| 172 | } |
| 173 | |
| 174 | func (t *TopicError) decode(pd packetDecoder, version int16) (err error) { |
| 175 | t.Err, err = pd.getKError() |
| 176 | if err != nil { |
| 177 | return err |
| 178 | } |
| 179 | |
| 180 | if version >= 1 { |
| 181 | if t.ErrMsg, err = pd.getNullableString(); err != nil { |
| 182 | return err |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | return nil |
| 187 | } |
| 188 | |
| 189 | // CreatableTopicResult struct { |
| 190 | type CreatableTopicResult struct { |
| 191 | // TopicConfigErrorCode contains a Optional topic config error returned if configs are not returned in the response. |
| 192 | TopicConfigErrorCode KError |
| 193 | // NumPartitions contains a Number of partitions of the topic. |
| 194 | NumPartitions int32 |
| 195 | // ReplicationFactor contains a Replication factor of the topic. |
| 196 | ReplicationFactor int16 |
| 197 | // Configs contains a Configuration of the topic. |
| 198 | Configs map[string]*CreatableTopicConfigs |
| 199 | } |
| 200 | |
| 201 | func (r *CreatableTopicResult) encode(pe packetEncoder, version int16) error { |
| 202 | pe.putInt32(r.NumPartitions) |
| 203 | pe.putInt16(r.ReplicationFactor) |
| 204 | |
| 205 | if err := pe.putArrayLength(len(r.Configs)); err != nil { |
| 206 | return err |
| 207 | } |
| 208 | for name, config := range r.Configs { |
| 209 | if err := pe.putString(name); err != nil { |
| 210 | return err |
| 211 | } |
| 212 | if err := config.encode(pe, version); err != nil { |
| 213 | return err |
| 214 | } |
| 215 | } |
| 216 | if r.TopicConfigErrorCode == ErrNoError { |
| 217 | pe.putEmptyTaggedFieldArray() |
| 218 | return nil |
| 219 | } |
| 220 | |
| 221 | // TODO: refactor to helper for tagged fields |
| 222 | pe.putUVarint(1) // number of tagged fields |
| 223 | |
| 224 | pe.putUVarint(0) // tag |
| 225 | |
| 226 | pe.putUVarint(2) // value length |
| 227 | |
| 228 | pe.putKError(r.TopicConfigErrorCode) // tag value |
| 229 | |
| 230 | return nil |
| 231 | } |
| 232 | |
| 233 | func (r *CreatableTopicResult) decode(pd packetDecoder, version int16) (err error) { |
| 234 | r.NumPartitions, err = pd.getInt32() |
| 235 | if err != nil { |
| 236 | return err |
| 237 | } |
| 238 | |
| 239 | r.ReplicationFactor, err = pd.getInt16() |
| 240 | if err != nil { |
| 241 | return err |
| 242 | } |
| 243 | |
| 244 | n, err := pd.getArrayLength() |
| 245 | if err != nil { |
| 246 | return err |
| 247 | } |
| 248 | r.Configs = make(map[string]*CreatableTopicConfigs, n) |
| 249 | for i := 0; i < n; i++ { |
| 250 | name, err := pd.getString() |
| 251 | if err != nil { |
| 252 | return err |
| 253 | } |
| 254 | r.Configs[name] = &CreatableTopicConfigs{} |
| 255 | if err := r.Configs[name].decode(pd, version); err != nil { |
| 256 | return err |
| 257 | } |
| 258 | } |
| 259 | err = pd.getTaggedFieldArray(taggedFieldDecoders{ |
| 260 | 0: func(pd packetDecoder) error { |
| 261 | r.TopicConfigErrorCode, err = pd.getKError() |
| 262 | if err != nil { |
| 263 | return err |
| 264 | } |
| 265 | return nil |
| 266 | }, |
| 267 | }) |
| 268 | if err != nil { |
| 269 | return err |
| 270 | } |
| 271 | return nil |
| 272 | } |
| 273 | |
| 274 | // CreatableTopicConfigs contains a Configuration of the topic. |
| 275 | type CreatableTopicConfigs struct { |
| 276 | // Value contains the configuration value. |
| 277 | Value *string |
| 278 | // ReadOnly contains a True if the configuration is read-only. |
| 279 | ReadOnly bool |
| 280 | // ConfigSource contains the configuration source. |
| 281 | ConfigSource ConfigSource |
| 282 | // IsSensitive contains a True if this configuration is sensitive. |
| 283 | IsSensitive bool |
| 284 | } |
| 285 | |
| 286 | func (c *CreatableTopicConfigs) encode(pe packetEncoder, version int16) (err error) { |
| 287 | if err = pe.putNullableString(c.Value); err != nil { |
| 288 | return err |
| 289 | } |
| 290 | pe.putBool(c.ReadOnly) |
| 291 | pe.putInt8(int8(c.ConfigSource)) |
| 292 | pe.putBool(c.IsSensitive) |
| 293 | pe.putEmptyTaggedFieldArray() |
| 294 | return nil |
| 295 | } |
| 296 | |
| 297 | func (c *CreatableTopicConfigs) decode(pd packetDecoder, version int16) (err error) { |
| 298 | c.Value, err = pd.getNullableString() |
| 299 | if err != nil { |
| 300 | return err |
| 301 | } |
| 302 | c.ReadOnly, err = pd.getBool() |
| 303 | if err != nil { |
| 304 | return err |
| 305 | } |
| 306 | source, err := pd.getInt8() |
| 307 | if err != nil { |
| 308 | return err |
| 309 | } |
| 310 | c.ConfigSource = ConfigSource(source) |
| 311 | c.IsSensitive, err = pd.getBool() |
| 312 | if err != nil { |
| 313 | return err |
| 314 | } |
| 315 | |
| 316 | if _, err := pd.getEmptyTaggedFieldArray(); err != nil { |
| 317 | return err |
| 318 | } |
| 319 | return nil |
| 320 | } |