api_versions_request.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package sarama
  2. const defaultClientSoftwareName = "sarama"
  3. type ApiVersionsRequest struct {
  4. // Version defines the protocol version to use for encode and decode
  5. Version int16
  6. // ClientSoftwareName contains the name of the client.
  7. ClientSoftwareName string
  8. // ClientSoftwareVersion contains the version of the client.
  9. ClientSoftwareVersion string
  10. }
  11. func (r *ApiVersionsRequest) encode(pe packetEncoder) (err error) {
  12. if r.Version >= 3 {
  13. if err := pe.putCompactString(r.ClientSoftwareName); err != nil {
  14. return err
  15. }
  16. if err := pe.putCompactString(r.ClientSoftwareVersion); err != nil {
  17. return err
  18. }
  19. pe.putEmptyTaggedFieldArray()
  20. }
  21. return nil
  22. }
  23. func (r *ApiVersionsRequest) decode(pd packetDecoder, version int16) (err error) {
  24. r.Version = version
  25. if r.Version >= 3 {
  26. if r.ClientSoftwareName, err = pd.getCompactString(); err != nil {
  27. return err
  28. }
  29. if r.ClientSoftwareVersion, err = pd.getCompactString(); err != nil {
  30. return err
  31. }
  32. if _, err := pd.getEmptyTaggedFieldArray(); err != nil {
  33. return err
  34. }
  35. }
  36. return nil
  37. }
  38. func (r *ApiVersionsRequest) key() int16 {
  39. return 18
  40. }
  41. func (r *ApiVersionsRequest) version() int16 {
  42. return r.Version
  43. }
  44. func (r *ApiVersionsRequest) headerVersion() int16 {
  45. if r.Version >= 3 {
  46. return 2
  47. }
  48. return 1
  49. }
  50. func (r *ApiVersionsRequest) isValidVersion() bool {
  51. return r.Version >= 0 && r.Version <= 3
  52. }
  53. func (r *ApiVersionsRequest) requiredVersion() KafkaVersion {
  54. switch r.Version {
  55. case 3:
  56. return V2_4_0_0
  57. case 2:
  58. return V2_0_0_0
  59. case 1:
  60. return V0_11_0_0
  61. case 0:
  62. return V0_10_0_0
  63. default:
  64. return V2_4_0_0
  65. }
  66. }