Skip to content

Commit

Permalink
style(grpc_codec): use switch-case (#12956)
Browse files Browse the repository at this point in the history
* style(grpc_codec): use switch-case

Signed-off-by: Maxime Brunet <[email protected]>

* Update go/vt/servenv/grpc_codec.go

Co-authored-by: Maxime Brunet <[email protected]>
Signed-off-by: Vicent Martí <[email protected]>

* Update go/vt/servenv/grpc_codec.go

Co-authored-by: Maxime Brunet <[email protected]>
Signed-off-by: Vicent Martí <[email protected]>

* Follow switch-case order in error message

Signed-off-by: Maxime Brunet <[email protected]>

---------

Signed-off-by: Maxime Brunet <[email protected]>
Signed-off-by: Vicent Martí <[email protected]>
Co-authored-by: Vicent Martí <[email protected]>
  • Loading branch information
maxbrunet and vmg authored May 30, 2023
1 parent bc5fc91 commit 090ca95
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions go/vt/servenv/grpc_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,25 @@ type vtprotoMessage interface {
}

func (vtprotoCodec) Marshal(v any) ([]byte, error) {
vt, ok := v.(vtprotoMessage)
if ok {
return vt.MarshalVT()
switch v := v.(type) {
case vtprotoMessage:
return v.MarshalVT()
case proto.Message:
return proto.Marshal(v)
default:
return nil, fmt.Errorf("failed to marshal, message is %T, must satisfy the vtprotoMessage interface or want proto.Message", v)
}

vv, ok := v.(proto.Message)
if !ok {
return nil, fmt.Errorf("failed to marshal, message is %T, want proto.Message", v)
}
return proto.Marshal(vv)
}

func (vtprotoCodec) Unmarshal(data []byte, v any) error {
vt, ok := v.(vtprotoMessage)
if ok {
return vt.UnmarshalVT(data)
}

vv, ok := v.(proto.Message)
if !ok {
return fmt.Errorf("failed to unmarshal, message is %T, want proto.Message", v)
switch v := v.(type) {
case vtprotoMessage:
return v.UnmarshalVT(data)
case proto.Message:
return proto.Unmarshal(data, v)
default:
return fmt.Errorf("failed to unmarshal, message is %T, must satisfy the vtprotoMessage interface or want proto.Message", v)
}
return proto.Unmarshal(data, vv)
}

func (vtprotoCodec) Name() string {
Expand Down

0 comments on commit 090ca95

Please sign in to comment.