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
  • Loading branch information
olegbespalov committed Sep 11, 2023
1 parent 32ce751 commit e45a5d7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 22 deletions.
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
10 changes: 5 additions & 5 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 @@ -68,8 +68,8 @@ func (s *Stream) receive() (*dynamicpb.Message, error) {
//
// message Point {
// double x = 1;
// double y = 2;
// double z = 3;
// double y = 2;
// double z = 3;
// }
//
// and a value like this:
Expand All @@ -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 e45a5d7

Please sign in to comment.