package encoder import ( "encoding/json" "fmt" "google.golang.org/protobuf/proto" ) type Encoder interface { Encode(v interface{}) ([]byte, error) } type JsonEncoder struct{} type ProtoEncoder struct{} var JsonEncoderInstance = &JsonEncoder{} var ProtoEncoderInstance = &ProtoEncoder{} func (j *JsonEncoder) Encode(v interface{}) ([]byte, error) { return json.Marshal(v) } func (p *ProtoEncoder) Encode(v interface{}) ([]byte, error) { if message, ok := v.(proto.Message); ok { return proto.Marshal(message) } return nil, fmt.Errorf("not a proto message") }