从协议缓冲区中获取所有字段名称? [英] Getting all field names from a protocol buffer?

查看:45
本文介绍了从协议缓冲区中获取所有字段名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将原型的所有字段名称放入列表中.有没有办法做到这一点?我查看了文档,似乎没有任何内容.

I want to get all the field names of a proto into a list. Is there a way to do this? I looked in the documentation and there doesn't seem to be anything for this.

推荐答案

每个 proto 类都有一个 DESCRIPTOR 类变量,可用于检查相应 protobuf 消息的字段.

Every proto class possess a DESCRIPTOR class variable that can be used to inspect the fields of corresponding protobuf messages.

查看 描述符FieldDescriptor 类以获取更多详细信息.

Have a look at the documentation of the Descriptor and FieldDescriptor classes for more details.

下面是一个简单的例子,将message中所有字段的FieldDescriptor放入一个列表中:

Here is a simple example to get the FieldDescriptors of all the fields in message into a list:

res = message.DESCRIPTOR.fields

要获取与 .proto 文件中显示的完全相同"的字段名称:

To get the names of the fields "exactly as they appear in the .proto file":

res = [field.name for field in message.DESCRIPTOR.fields]

或(来自评论):

res = message.DESCRIPTOR.fields_by_name.keys()

要获取包括包含范围"的字段的全名:

To get the full names of the fields "including containing scope":

res = [field.full_name for field in message.DESCRIPTOR.fields]

这篇关于从协议缓冲区中获取所有字段名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆