Skip to content

Instantly share code, notes, and snippets.

@AlmogBaku
Created October 9, 2021 09:50
Show Gist options
  • Save AlmogBaku/4a9ad3c66247f404e26378e31e049992 to your computer and use it in GitHub Desktop.
Save AlmogBaku/4a9ad3c66247f404e26378e31e049992 to your computer and use it in GitHub Desktop.
Sarama protobuf encoder
package protoencoder
import (
"github.com/Shopify/sarama"
"google.golang.org/protobuf/proto"
)
type encoder struct {
msg proto.Message
}
func (e *encoder) Encode() ([]byte, error) {
return proto.Marshal(e.msg)
}
func (e *encoder) Length() int {
return proto.Size(e.msg)
}
// ProtoEncoder encodes ProtocolBuffer messages natively on Sarama
// Utilizing this encoder will only encode the message during the producing time
func ProtoEncoder(msg proto.Message) sarama.Encoder {
return &encoder{msg: msg}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment