Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 1 | package sarama |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "time" |
| 6 | ) |
| 7 | |
| 8 | type CreatePartitionsResponse struct { |
| 9 | ThrottleTime time.Duration |
| 10 | TopicPartitionErrors map[string]*TopicPartitionError |
| 11 | } |
| 12 | |
| 13 | func (c *CreatePartitionsResponse) encode(pe packetEncoder) error { |
| 14 | pe.putInt32(int32(c.ThrottleTime / time.Millisecond)) |
| 15 | if err := pe.putArrayLength(len(c.TopicPartitionErrors)); err != nil { |
| 16 | return err |
| 17 | } |
| 18 | |
| 19 | for topic, partitionError := range c.TopicPartitionErrors { |
| 20 | if err := pe.putString(topic); err != nil { |
| 21 | return err |
| 22 | } |
| 23 | if err := partitionError.encode(pe); err != nil { |
| 24 | return err |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | return nil |
| 29 | } |
| 30 | |
| 31 | func (c *CreatePartitionsResponse) decode(pd packetDecoder, version int16) (err error) { |
| 32 | throttleTime, err := pd.getInt32() |
| 33 | if err != nil { |
| 34 | return err |
| 35 | } |
| 36 | c.ThrottleTime = time.Duration(throttleTime) * time.Millisecond |
| 37 | |
| 38 | n, err := pd.getArrayLength() |
| 39 | if err != nil { |
| 40 | return err |
| 41 | } |
| 42 | |
| 43 | c.TopicPartitionErrors = make(map[string]*TopicPartitionError, n) |
| 44 | for i := 0; i < n; i++ { |
| 45 | topic, err := pd.getString() |
| 46 | if err != nil { |
| 47 | return err |
| 48 | } |
| 49 | c.TopicPartitionErrors[topic] = new(TopicPartitionError) |
| 50 | if err := c.TopicPartitionErrors[topic].decode(pd, version); err != nil { |
| 51 | return err |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | return nil |
| 56 | } |
| 57 | |
| 58 | func (r *CreatePartitionsResponse) key() int16 { |
| 59 | return 37 |
| 60 | } |
| 61 | |
| 62 | func (r *CreatePartitionsResponse) version() int16 { |
| 63 | return 0 |
| 64 | } |
| 65 | |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 66 | func (r *CreatePartitionsResponse) headerVersion() int16 { |
| 67 | return 0 |
| 68 | } |
| 69 | |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 70 | func (r *CreatePartitionsResponse) requiredVersion() KafkaVersion { |
| 71 | return V1_0_0_0 |
| 72 | } |
| 73 | |
| 74 | type TopicPartitionError struct { |
| 75 | Err KError |
| 76 | ErrMsg *string |
| 77 | } |
| 78 | |
| 79 | func (t *TopicPartitionError) Error() string { |
| 80 | text := t.Err.Error() |
| 81 | if t.ErrMsg != nil { |
| 82 | text = fmt.Sprintf("%s - %s", text, *t.ErrMsg) |
| 83 | } |
| 84 | return text |
| 85 | } |
| 86 | |
| 87 | func (t *TopicPartitionError) encode(pe packetEncoder) error { |
| 88 | pe.putInt16(int16(t.Err)) |
| 89 | |
| 90 | if err := pe.putNullableString(t.ErrMsg); err != nil { |
| 91 | return err |
| 92 | } |
| 93 | |
| 94 | return nil |
| 95 | } |
| 96 | |
| 97 | func (t *TopicPartitionError) decode(pd packetDecoder, version int16) (err error) { |
| 98 | kerr, err := pd.getInt16() |
| 99 | if err != nil { |
| 100 | return err |
| 101 | } |
| 102 | t.Err = KError(kerr) |
| 103 | |
| 104 | if t.ErrMsg, err = pd.getNullableString(); err != nil { |
| 105 | return err |
| 106 | } |
| 107 | |
| 108 | return nil |
| 109 | } |