utils.go 881 B

12345678910111213141516171819202122232425262728293031323334
  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. }