doy57007 2018-12-26 12:33
浏览 43

协议缓冲区:通过GRPC发送任意结构

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?

  • 写回答

1条回答 默认 最新

  • doujiu6976 2018-12-26 19:27
    关注

    grpc server should unpack the bytes into a go struct as defined here. Can you provide more info about why you are seeing map[string]interface{}?

    评论

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答