In grpc-mate's protobuf definition, we defined some enum types, when we compile them into python code, it may confuse people on how to use it , especially for the enum defined inside protobuf message
standalone enum types
enum ProductStatus {
InStock = 0;
OutStock = 1;
}
when compile such enum into python code , it will be like below
_PRODUCTSTATUS = _descriptor.EnumDescriptor(
name='ProductStatus',
full_name='ProductStatus',
filename=None,
file=DESCRIPTOR,
values=[
_descriptor.EnumValueDescriptor(
name='InStock', index=0, number=0,
serialized_options=None,
type=None),
_descriptor.EnumValueDescriptor(
name='OutStock', index=1, number=1,
serialized_options=None,
type=None),
],
containing_type=None,
serialized_options=None,
serialized_start=196,
serialized_end=238,
)
_sym_db.RegisterEnumDescriptor(_PRODUCTSTATUS)
ProductStatus = enum_type_wrapper.EnumTypeWrapper(_PRODUCTSTATUS)
InStock = 0
OutStock = 1
the enum type will be just a number but with it's Own name, we could import the enum like from grpc_mate.product_common_pb2 import InStock
nested enum type inside protobuf message
message UploadProductResponse {
enum ResultStatus {
SUCCESS = 0;
FAILED = 1;
}
ResultStatus result_status = 1;
}
it will be compiled as python code like below
_UPLOADPRODUCTRESPONSE_RESULTSTATUS = _descriptor.EnumDescriptor(
name='ResultStatus',
full_name='UploadProductResponse.ResultStatus',
filename=None,
file=DESCRIPTOR,
values=[
_descriptor.EnumValueDescriptor(
name='SUCCESS', index=0, number=0,
serialized_options=None,
type=None),
_descriptor.EnumValueDescriptor(
name='FAILED', index=1, number=1,
serialized_options=None,
type=None),
],
containing_type=None,
serialized_options=None,
serialized_start=188,
serialized_end=227,
)
_sym_db.RegisterEnumDescriptor(_UPLOADPRODUCTRESPONSE_RESULTSTATUS)
lots of people will get confused on how to use ResultStatus enum inside UploadProductResponse, due to python's launage features, we could access such enum as UploadProductResponse.SUCCESS
or UploadProductResponse.FAILED
. as I said in last section, the enum in python is just a number, we could also access the number like UploadProductResponse.ResultStatus.Value('SUCCESS') -> 0
, or access it's name via UploadProductResponse.ResultStatus.Name(0) -> 'SUCCESS'
. all the methods are defined in enum_type_wrapper.py.