当前位置:   article > 正文

Protobuf 复杂消息数据的解析和构建

Protobuf 复杂消息数据的解析和构建

Protobuf 是 Google 开发的语言中立、平台中立的结构化数据序列化和反序列化协议。用于应用程序间结构化数据的传输,相对于JSON、XML等基于文本的协议,它以二进制方式传输数据,效率更高。

有关 protobuf 详细介绍请参见官方文档 Protocol Buffers Documentation

在实际使用中发现官方文档对消息体中各种不同类型数据的构建和解析没有特别详细的说明,特别是复杂数据。本文分享在实际使用中的经验,抛砖引玉,希望对大家会有所帮助。

以下的示例均以Python语言为例来说明。

基本数据类型列表

下面的示例基于官方文档中包含的 helloword例子修改。用户信息 UserInfo 包括用户名、用户ID和组ID列表,其中组ID列表就是基本数据类型 int32 的列表。

  1. service Greeter {
  2. rpc SayHello (HelloRequest) returns (HelloReply) {}
  3. }
  4. message UserInfo {
  5. string name = 1;
  6. int32 uid = 2;
  7. repeated int32 gid = 3;
  8. }
  9. message HelloRequest {
  10. UserInfo user = 1;
  11. }
  12. // The response message containing the greetings
  13. message HelloReply {
  14. string message = 1;
  15. }

构建方式如下所示:

  1. from __future__ import print_function
  2. import os
  3. import logging
  4. import getpass
  5. import grpc
  6. import helloworld_pb2
  7. import helloworld_pb2_grpc
  8. def run():
  9. print("Will try to greet world ...")
  10. with grpc.insecure_channel("localhost:50051") as channel:
  11. stub = helloworld_pb2_grpc.GreeterStub(channel)
  12. user = getpass.getuser()
  13. uid = os.getuid()
  14. gids = os.getgroups()
  15. ui = helloworld_pb2.UserInfo()
  16. ui.name = user
  17. ui.uid = uid
  18. for g in gids:
  19. ui.gid.append(g)
  20. response = stub.SayHello(helloworld_pb2.HelloRequest(user=ui))
  21. print("Greeter client received: " + response.message)
  22. if __name__ == "__main__":
  23. logging.basicConfig()
  24. run()

对于基本数据类型的列表,在创建对应消息的实例时已经创建了空的列表,因此通过Python操作列表的接口操作即可。例如示例中直接通过 append() 将用户的所有组ID加入到列表中。

解析时,通过常规列表API即可

gids = ','.join([str(x) for x in request.user.gid])

字典

在消息体中 map 数据类型和 Python 中的字典类型,比如以下示例中的 course 表示每门课程的名称的得分。 

  1. ...
  2. message UserInfo {
  3. string name = 1;
  4. int32 uid = 2;
  5. repeated int32 gid = 3;
  6. map<string, int32> course = 4;
  7. ...

构建方式与 Python 中字典的构建方式相同。

  1. ...
  2. ui = helloworld_pb2.UserInfo()
  3. ui.name = user
  4. ...
  5. ui.course['foo'] = 5
  6. ui.course['bar'] = 10
  7. ...

  解析方法按Python中字典的方式即可。

course = ' '.join([f"{k}:{v}" for k,v in request.user.course.items()])

字典列表

即列表中的每一项为一个字典。

在 proto 定义中增加 Friend 并修改 UserInfo 如下所求,friend_list 即为列表,列表中的每个元素为字典 friend

  1. message Friend {
  2. map<string, int32> friend = 1;
  3. }
  4. message UserInfo {
  5. string name = 1;
  6. int32 uid = 2;
  7. repeated int32 gid = 3;
  8. map<string, int32> course = 4;
  9. repeated Friend friend_list = 5;
  10. }

构建方式如下:

  1. ...
  2. ui = helloworld_pb2.UserInfo()
  3. ...
  4. ui.friend_list.append(helloworld_pb2.Friend(friend={'Bob':12}))
  5. ui.friend_list.append(helloworld_pb2.Friend(friend={'Peter':13}))
  6. ui.friend_list.append(helloworld_pb2.Friend(friend={'Alice':11}))
  7. ...

friend_list 为列表,操作方式同上。但对于列表中的字典元素,需要单独创建对应的类实例。创建方法可参考对应消息类的构造函数,消息类的定义在 .pyi 文件中。

  1. ...
  2. class Friend(_message.Message):
  3. __slots__ = ("friend",)
  4. class FriendEntry(_message.Message):
  5. __slots__ = ("key", "value")
  6. KEY_FIELD_NUMBER: _ClassVar[int]
  7. VALUE_FIELD_NUMBER: _ClassVar[int]
  8. key: str
  9. value: int
  10. def __init__(self, key: _Optional[str] = ..., value: _Optional[int] = ...) -> None: ...
  11. FRIEND_FIELD_NUMBER: _ClassVar[int]
  12. friend: _containers.ScalarMap[str, int]
  13. def __init__(self, friend: _Optional[_Mapping[str, int]] = ...) -> None: ...

解析方式如下所示:

friends = ' '.join([f"{name}:{age}" for x in request.user.friend_list for name, age in x.friend.items()])

其它复杂数据结构不外乎由基本数据类型、字典、列表组合而成。总之,对于各类包含复杂数据结构消息的构建和解析,都可以参考 .pyi 中消息类的定义,从而找到消息构建和解析方法。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/80030
推荐阅读
相关标签
  

闽ICP备14008679号