create_topics_response.go 2.2 KB

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