From 8a020438a5f6a96018f76dec954ff96301edddc3 Mon Sep 17 00:00:00 2001 From: Oleg Bespalov Date: Mon, 11 Sep 2023 10:25:58 +0200 Subject: [PATCH] Apply suggestions from code review --- lib/netext/grpcext/conn.go | 22 +++++----------------- lib/netext/grpcext/stream.go | 10 +++++----- 2 files changed, 10 insertions(+), 22 deletions(-) diff --git a/lib/netext/grpcext/conn.go b/lib/netext/grpcext/conn.go index d2d382d..5e9f9e0 100644 --- a/lib/netext/grpcext/conn.go +++ b/lib/netext/grpcext/conn.go @@ -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 diff --git a/lib/netext/grpcext/stream.go b/lib/netext/grpcext/stream.go index ac9e567..5816166 100644 --- a/lib/netext/grpcext/stream.go +++ b/lib/netext/grpcext/stream.go @@ -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 } @@ -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: @@ -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) }