utils.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package kafka
  2. // make []int32 sortable so we can sort partition numbers
  3. type int32Slice []int32
  4. func (slice int32Slice) Len() int {
  5. return len(slice)
  6. }
  7. func (slice int32Slice) Less(i, j int) bool {
  8. return slice[i] < slice[j]
  9. }
  10. func (slice int32Slice) Swap(i, j int) {
  11. slice[i], slice[j] = slice[j], slice[i]
  12. }
  13. // make strings encodable for convenience so they can be used as keys
  14. // and/or values in kafka messages
  15. // StringEncoder implements the Encoder interface for Go strings so that you can do things like
  16. // producer.SendMessage(nil, kafka.StringEncoder("hello world"))
  17. type StringEncoder string
  18. func (s StringEncoder) Encode() ([]byte, error) {
  19. return []byte(s), nil
  20. }
  21. // A simple interface for any type that can be encoded as an array of bytes
  22. // in order to be sent as the key or value of a Kafka message.
  23. type Encoder interface {
  24. Encode() ([]byte, error)
  25. }
  26. // create a message struct to return from high-level fetch requests
  27. // we could in theory use sarama/protocol/message.go but that has to match the
  28. // wire protocol, which doesn't quite line up with what we actually need to return
  29. // Message is what is returned from fetch requests.
  30. type Message struct {
  31. Offset int64
  32. Key []byte
  33. Value []byte
  34. }