Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Mihail Stoykov <[email protected]>
  • Loading branch information
olegbespalov and mstoykov committed Sep 11, 2023
1 parent 1df3dbc commit 0250ce4
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 21 deletions.
2 changes: 1 addition & 1 deletion grpc/testdata/wrappers_testing/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (s *service) TestBoolean(ctx context.Context, in *wrappers.BoolValue) (*wra
}

func (s *service) TestDouble(ctx context.Context, in *wrappers.DoubleValue) (*wrappers.DoubleValue, error) {
if s.TestBooleanImplementation != nil {
if s.TestDoubleImplementation != nil {
return s.TestDoubleImplementation(ctx, in)
}

Expand Down
22 changes: 5 additions & 17 deletions lib/netext/grpcext/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,23 +154,11 @@ func (c *Conn) Invoke(
}

if resp != nil {
// (rogchap) there is a lot of marshaling/unmarshaling here, but if we just pass the dynamic message
// the default Marshaller would be used, which would strip any zero/default values from the JSON.
// eg. given this message:
// message Point {
// double x = 1;
// double y = 2;
// double z = 3;
// }
// and a value like this:
// msg := Point{X: 6, Y: 4, Z: 0}
// would result in JSON output:
// {"x":6,"y":4}
// rather than the desired:
// {"x":6,"y":4,"z":0}
raw, _ := marshaler.Marshal(resp)
var msg interface{}
_ = json.Unmarshal(raw, &msg)
msg, err := convert(marshaler, resp)
if err != nil {
return nil, fmt.Errorf("unable to convert response object to JSON: %w", err)
}

response.Message = msg
}
return &response, nil
Expand Down
6 changes: 3 additions & 3 deletions lib/netext/grpcext/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (s *Stream) ReceiveConverted() (interface{}, error) {
return nil, err
}

msg, errConv := s.convert(raw)
msg, errConv := convert(s.marshaler, raw)
if errConv != nil {
return nil, errConv
}
Expand Down Expand Up @@ -78,10 +78,10 @@ func (s *Stream) receive() (*dynamicpb.Message, error) {
// {"x":6,"y":4}
// rather than the desired:
// {"x":6,"y":4,"z":0}
func (s *Stream) convert(msg *dynamicpb.Message) (interface{}, error) {
func convert(marshaler protojson.MarshalOptions, msg *dynamicpb.Message) (interface{}, error) {
// TODO(olegbespalov): add the test that checks that message is not nil

raw, err := s.marshaler.Marshal(msg)
raw, err := marshaler.Marshal(msg)
if err != nil {
return nil, fmt.Errorf("failed to marshal the message: %w", err)
}
Expand Down

0 comments on commit 0250ce4

Please sign in to comment.