Skip to content

Commit

Permalink
address dfawley's comments
Browse files Browse the repository at this point in the history
  • Loading branch information
arvindbr8 committed Feb 5, 2024
1 parent 24902d8 commit d82c467
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions encoding/proto/proto.go
Expand Up @@ -39,31 +39,32 @@ func init() {
type codec struct{}

func (codec) Marshal(v any) ([]byte, error) {
var vv proto.Message
switch v := v.(type) {
case protoadapt.MessageV1:
vv = protoadapt.MessageV2Of(v)
case protoadapt.MessageV2:
vv = v
default:
vv := messageV2Of(v)
if vv == nil {
return nil, fmt.Errorf("failed to marshal, message is %T, want proto.Message", v)
}

return proto.Marshal(vv)
}

func (codec) Unmarshal(data []byte, v any) error {
var vv proto.Message
vv := messageV2Of(v)
if vv == nil {
return fmt.Errorf("failed to marshal, message is %T, want proto.Message", v)
}

return proto.Unmarshal(data, vv)
}

func messageV2Of(v any) proto.Message {
switch v := v.(type) {
case protoadapt.MessageV1:
vv = protoadapt.MessageV2Of(v)
return protoadapt.MessageV2Of(v)
case protoadapt.MessageV2:
vv = v
default:
return fmt.Errorf("failed to unmarshal, message is %T, want proto.Message", v)
return v
}

return proto.Unmarshal(data, vv)
return nil
}

func (codec) Name() string {
Expand Down

0 comments on commit d82c467

Please sign in to comment.