Skip to content

Refactor descpb_to_proto.py to handle repeated enum fields properly #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions utils/descpb_to_proto.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from google.protobuf.descriptor_pb2 import DescriptorProto, FieldDescriptorProto
from collections import OrderedDict
from itertools import groupby
from google.protobuf.internal.containers import RepeatedScalarFieldContainer

"""
This script converts back a FileDescriptor structure to a readable .proto file.
Expand Down Expand Up @@ -110,16 +111,20 @@ def parse_msg(desc, scopes, syntax):
out = wrap_block('message' * is_msg, out, desc)
return out


def fmt_value(val, options=None, desc=None, optarr=[]):
if type(val) != str:
if type(val) == bool:
val = str(val).lower()
elif desc and desc.enum_type:
val = desc.enum_type.values_by_number[val].name
elif desc and desc.type == FieldDescriptorProto.TYPE_ENUM:
if isinstance(val, (list, RepeatedScalarFieldContainer)):
val = ', '.join(desc.enum_type.values_by_number[v].name for v in val)
else:
val = desc.enum_type.values_by_number[val].name
val = str(val)
else:
val = '"%s"' % val.encode('unicode_escape').decode('utf8')

if options:
opts = [*optarr]
for (option, value) in options.ListFields():
Expand Down