Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call Reset before UnmarshallVT to keep the default grpc semantic #131

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion codec/grpc/grpc_codec.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package grpc

import "fmt"
import (
"fmt"
)

// Name is the name registered for the proto compressor.
const Name = "proto"
Expand All @@ -12,6 +14,10 @@ type vtprotoMessage interface {
UnmarshalVT([]byte) error
}

type reseter interface{
Reset()
}

func (Codec) Marshal(v interface{}) ([]byte, error) {
vt, ok := v.(vtprotoMessage)
if !ok {
Expand All @@ -25,6 +31,12 @@ func (Codec) Unmarshal(data []byte, v interface{}) error {
if !ok {
return fmt.Errorf("failed to unmarshal, message is %T (missing vtprotobuf helpers)", v)
}
//All types that implement github.com/golang/protobuf/proto.Message have a Reset method
vv, ok := v.(reseter)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
vv, ok := v.(reseter)
vv, ok := v.(interface { ResetVT() })

Copy link
Author

@aureliar8 aureliar8 May 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't require the message to implement ResetVT() because this method isn't generated by default (it requires the pool option)
Doing so would break the codec for user who just use the marshal and umarshal features

But I can follow this style and do

Suggested change
vv, ok := v.(reseter)
vv, ok := v.(interface{ Reset() })

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right :) IMO the resetvt func should be turned into a plug-in that's activated automatically with pool plugin.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you mean that ResetVT() should be generated automatically when the unmarshal plugin is enabled ?

if !ok {
return fmt.Errorf("failed to unmarshal: can't reset. Message type %T don't implement Reset()", vv)
}
vv.Reset()
return vt.UnmarshalVT(data)
}

Expand Down