metadata_request.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package sarama
  2. type MetadataRequest struct {
  3. Version int16
  4. Topics []string
  5. AllowAutoTopicCreation bool
  6. }
  7. func (r *MetadataRequest) encode(pe packetEncoder) error {
  8. if r.Version < 0 || r.Version > 5 {
  9. return PacketEncodingError{"invalid or unsupported MetadataRequest version field"}
  10. }
  11. if r.Version == 0 || r.Topics != nil || len(r.Topics) > 0 {
  12. err := pe.putArrayLength(len(r.Topics))
  13. if err != nil {
  14. return err
  15. }
  16. for i := range r.Topics {
  17. err = pe.putString(r.Topics[i])
  18. if err != nil {
  19. return err
  20. }
  21. }
  22. } else {
  23. pe.putInt32(-1)
  24. }
  25. if r.Version > 3 {
  26. pe.putBool(r.AllowAutoTopicCreation)
  27. }
  28. return nil
  29. }
  30. func (r *MetadataRequest) decode(pd packetDecoder, version int16) error {
  31. r.Version = version
  32. size, err := pd.getInt32()
  33. if err != nil {
  34. return err
  35. }
  36. if size < 0 {
  37. return nil
  38. } else {
  39. topicCount := size
  40. if topicCount == 0 {
  41. return nil
  42. }
  43. r.Topics = make([]string, topicCount)
  44. for i := range r.Topics {
  45. topic, err := pd.getString()
  46. if err != nil {
  47. return err
  48. }
  49. r.Topics[i] = topic
  50. }
  51. }
  52. if r.Version > 3 {
  53. autoCreation, err := pd.getBool()
  54. if err != nil {
  55. return err
  56. }
  57. r.AllowAutoTopicCreation = autoCreation
  58. }
  59. return nil
  60. }
  61. func (r *MetadataRequest) key() int16 {
  62. return 3
  63. }
  64. func (r *MetadataRequest) version() int16 {
  65. return r.Version
  66. }
  67. func (r *MetadataRequest) requiredVersion() KafkaVersion {
  68. switch r.Version {
  69. case 1:
  70. return V0_10_0_0
  71. case 2:
  72. return V0_10_1_0
  73. case 3, 4:
  74. return V0_11_0_0
  75. case 5:
  76. return V1_0_0_0
  77. default:
  78. return MinVersion
  79. }
  80. }