grpc with dynamic messages

1,867 views
Skip to first unread message

david...@gmail.com

unread,
May 25, 2016, 1:14:24 PM5/25/16
to grpc.io

Hi 

Protobuf supports building of dynamic messages. I was wondering if it is possible to define a service that returns a DynamicMessage type? 
In this specific case, I am thinking of having a service that returns a row from the database but the schema will only be known at run time. Thus the need for dynamic messages. 

Is it possible? if not, is there plan to support this in the future? 

Thanks. 

Christian Rivasseau

unread,
May 25, 2016, 1:19:14 PM5/25/16
to david...@gmail.com, grpc.io
Would it help to return a response that contains a google.proto.Any ?

--
You received this message because you are subscribed to the Google Groups "grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+u...@googlegroups.com.
To post to this group, send email to grp...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/grpc-io/f495283a-f62c-4458-b803-8ceef2f9e438%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Christian Rivasseau
Co-founder and CTO @ Lefty
+33 6 67 35 26 74

David Zhu

unread,
May 25, 2016, 2:51:11 PM5/25/16
to Christian Rivasseau, grpc.io
I tried to use google.proto.Any to wrap the dynamic message that I generated. 

    RowReply.newBuilder().setRow(Any.pack(m)).build();


However, during unpack time, I need to specify the class to unpack to and specifying DynamicMessage.class did not work. 

Something like 

    Any any = response.getRow();

    Message m = null;

    if (any.is(DynamicMessage.class)){

      m = any.unpack(DynamicMessage.class); 

    }

gave an exception 

    Failed to get default instance for class com.google.protobuf.DynamicMessage

Does the Any type require there being a class definition for the Message it wraps? 

I also tried to use Message.class in place of DynamicMessage.class, but no success either. 


Thanks. 



Yu ( David ) Zhu

Eric Anderson

unread,
May 26, 2016, 3:00:57 PM5/26/16
to David Zhu, Christian Rivasseau, grpc.io
On Wed, May 25, 2016 at 11:51 AM, David Zhu <david...@gmail.com> wrote:

However, during unpack time, I need to specify the class to unpack to and specifying DynamicMessage.class did not work. 

Something like 

    Any any = response.getRow();

    Message m = null;

    if (any.is(DynamicMessage.class)){

      m = any.unpack(DynamicMessage.class); 

    }

gave an exception 

    Failed to get default instance for class com.google.protobuf.DynamicMessage


Instead of using the convenience method unpack, you will need to manually do its work. Something like this (untested):

Message m = DynamicMessage.parseFrom(descriptor, any.getValue());


Any is a tuple of (string type_url, bytes value), and here we are manually accessing the value. The descriptor you would need to determine based on any.getTypeUrl(). Any.is() is just another convenience that compares the type URL in the any to the type of the class to see if they match.


Does the Any type require there being a class definition for the Message it wraps?


You need some information about the message parse it (as opposed to just treating it as bytes). The easiest way is to have the class definition for the Message, but then you have no need for DynamicMessage. The point of DynamicMessage is you can load the descriptor at runtime and still interact with the message.

David Zhu

unread,
May 26, 2016, 3:32:17 PM5/26/16
to Eric Anderson, Christian Rivasseau, grpc.io
Thanks.  

I dug into the source code of gRPC and found the solution to this, which is basically what you suggested. 
Before I send the DynamicMessage, I send the descriptor information so I can parse it at clientside using parseFrom. 




Yu ( David ) Zhu

Reply all
Reply to author
Forward
0 new messages