create_topics_response.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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) requiredVersion() KafkaVersion {
  61. switch c.Version {
  62. case 2:
  63. return V1_0_0_0
  64. case 1:
  65. return V0_11_0_0
  66. default:
  67. return V0_10_1_0
  68. }
  69. }
  70. type TopicError struct {
  71. Err KError
  72. ErrMsg *string
  73. }
  74. func (t *TopicError) Error() string {
  75. text := t.Err.Error()
  76. if t.ErrMsg != nil {
  77. text = fmt.Sprintf("%s - %s", text, *t.ErrMsg)
  78. }
  79. return text
  80. }
  81. func (t *TopicError) encode(pe packetEncoder, version int16) error {
  82. pe.putInt16(int16(t.Err))
  83. if version >= 1 {
  84. if err := pe.putNullableString(t.ErrMsg); err != nil {
  85. return err
  86. }
  87. }
  88. return nil
  89. }
  90. func (t *TopicError) decode(pd packetDecoder, version int16) (err error) {
  91. kErr, err := pd.getInt16()
  92. if err != nil {
  93. return err
  94. }
  95. t.Err = KError(kErr)
  96. if version >= 1 {
  97. if t.ErrMsg, err = pd.getNullableString(); err != nil {
  98. return err
  99. }
  100. }
  101. return nil
  102. }