@@ -11,48 +11,48 @@ import (
1111
1212const Name = "proto"
1313
14- // vtprotoCodecV2 implements encoding.CodecV2 and uses vtproto and default buffer pool
14+ // CodecV2 implements encoding.CodecV2 and uses vtproto and default buffer pool
1515// to encode/decode proto messages. The implementation is heavily inspired by
1616// https://github.com/planetscale/vtprotobuf/pull/138
1717// and https://github.com/vitessio/vitess/pull/16790.
18- type vtprotoCodecV2 struct {
18+ type CodecV2 struct {
1919 fallback encoding.CodecV2
2020}
2121
22- func (vtprotoCodecV2 ) Name () string {
22+ func (CodecV2 ) Name () string {
2323 return Name
2424}
2525
26- func (c * vtprotoCodecV2 ) Marshal (v any ) (mem.BufferSlice , error ) {
27- m , ok := v .(proto.VtprotoMessage )
26+ func (c * CodecV2 ) Marshal (v any ) (mem.BufferSlice , error ) {
27+ m , ok := v .(proto.VTProtoMessage )
2828 if ! ok {
2929 return c .fallback .Marshal (v )
3030 }
31-
3231 size := m .SizeVT ()
3332 if mem .IsBelowBufferPoolingThreshold (size ) {
34- buf := make ([]byte , 0 , size )
35- if _ , err := m .MarshalToSizedBufferVT (buf [:size ]); err != nil {
33+ buf := make ([]byte , size )
34+ n , err := m .MarshalToSizedBufferVT (buf )
35+ if err != nil {
3636 return nil , err
3737 }
38- return mem.BufferSlice {mem .SliceBuffer (buf )}, nil
38+ return mem.BufferSlice {mem .SliceBuffer (buf [: n ] )}, nil
3939 }
40-
4140 pool := mem .DefaultBufferPool ()
4241 buf := pool .Get (size )
43- if _ , err := m .MarshalToSizedBufferVT ((* buf )[:size ]); err != nil {
42+ n , err := m .MarshalToSizedBufferVT (* buf )
43+ if err != nil {
4444 pool .Put (buf )
4545 return nil , err
4646 }
47+ * buf = (* buf )[:n ]
4748 return mem.BufferSlice {mem .NewBuffer (buf , pool )}, nil
4849}
4950
50- func (c * vtprotoCodecV2 ) Unmarshal (data mem.BufferSlice , v any ) error {
51- m , ok := v .(proto.VtprotoMessage )
51+ func (c * CodecV2 ) Unmarshal (data mem.BufferSlice , v any ) error {
52+ m , ok := v .(proto.VTProtoMessage )
5253 if ! ok {
5354 return c .fallback .Unmarshal (data , v )
5455 }
55-
5656 buf := data .MaterializeToBuffer (mem .DefaultBufferPool ())
5757 defer buf .Free ()
5858 return m .UnmarshalVT (buf .ReadOnlyData ())
@@ -61,7 +61,8 @@ func (c *vtprotoCodecV2) Unmarshal(data mem.BufferSlice, v any) error {
6161// RegisterCodec registers the vtprotoCodec to encode/decode proto messages with
6262// all gRPC clients and servers.
6363func Register () {
64- encoding .RegisterCodecV2 (& vtprotoCodecV2 {
64+ encoding .RegisterCodecV2 (& CodecV2 {
65+ // the default codecv2 implemented in @org_golang_google_grpc//encoding/proto.
6566 fallback : encoding .GetCodecV2 ("proto" ),
6667 })
6768}
0 commit comments