Last active
September 10, 2019 19:34
-
-
Save gtalarico/b276b185e2ce47d42c09bc199a2338cc to your computer and use it in GitHub Desktop.
Get Protobuf option value and others
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Proto | |
enum EntityType { | |
GEOMETRY = 0; | |
PHYSICAL = 1; | |
} | |
extend google.protobuf.MessageOptions { | |
EntityType entity_type = 100; | |
} | |
message Point { | |
float x = 1; | |
float y = 2; | |
float z = 3; | |
option (common.entity_type) = GEOMETRY; | |
} | |
""" | |
import proto_pkg | |
def get_proto_entity_type(message: Message) -> str: | |
""" Get the entity_type option declared in the message, if present """ | |
entity_type_enum_int = message.DESCRIPTOR.GetOptions().Extensions[ | |
proto_pkg.entity_type | |
] | |
return proto_pkg.EntityType.Name(entity_type_enum_int) | |
def get_proto_package(message: Message) -> str: | |
""" Get the package name as defined in the proto `package x` directive """ | |
return message.DESCRIPTOR.file.package | |
def get_proto_message_field_names(message: Message) -> List[str]: | |
""" Get all field names of a message """ | |
return [f.name for f in message.DESCRIPTOR.fields] | |
def get_proto_message_name(message: Message) -> str: | |
return message.DESCRIPTOR.name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment