I'm aiming at creating an API that receives JSON objects (nested and of arbitrary nature), packs them into google/protobuf/struct.proto
structures and sends them with GRPC. It's all fine, the good jsonpb
library covers the unmarshalling process.
The problem arises in the GRPC server, the receiver, it unpacks the struct.proto object into a normal golang data structure for convenience of use, so it becomes a map[string]interface{}
, with other nested objects of various kinds.
So when I try to convert the object back to a struct.proto for sending it back to the client, there are few options. I found a converter that uses reflection, which has a bug I can't quite catch (has to do with not being able to distinguish data types, it treats nested stuff as interface{}), this one.
After banging my head on it for quite a few hours, I'm starting to wonder if this is even the best approach. I need to convert the struct.proto objects into something convenient and convert them back, is there a better way for doing the conversion as fast as possible while maintaining the nesting capability?
It's strange that the struct.proto is easy to convert one way and very hard to do the opposite. Must be because the only way to extract object metadata is through reflection. And the lack of generics make it even harder.
Using JSON is quite bad for performance, and gob is not an option as the protocol must be as universal as possible. The last chance I could think of is to find another marshaller and send the data as raw bytes through the protobuf, and then unmarshal it in the server.
Any help is welcome, thanks.
EDIT 1:
I think in the end I will have to implement the possibility to have multiple marshalling protocols and a byte array in the protobuf definition, so the server and client negotiate on the fastest they can support... and that would allow me to implement it with gob and jbson as a fallback. Comments?