aboutsummaryrefslogtreecommitdiffstats
path: root/generator/nanopb_generator.py
diff options
context:
space:
mode:
authorPetteri Aimonen <jpa@git.mail.kapsi.fi>2012-11-14 00:20:44 +0200
committerPetteri Aimonen <jpa@git.mail.kapsi.fi>2012-11-14 00:36:16 +0200
commit02ecee2de89f3225a6cd4bf87b0e4285bb1d06eb (patch)
tree47bef1d3142a2035500cb2ea74aab5218a1885f9 /generator/nanopb_generator.py
parent332a9ee95c38db4537d0a33e58fef617d3e1d589 (diff)
Fix naming of enums when long_names=false.
Modify test case to check that options.pb.o compiles. Update issue 42 Status: FixedInGit Update issue 43 Status: FixedInGit
Diffstat (limited to 'generator/nanopb_generator.py')
-rw-r--r--generator/nanopb_generator.py21
1 files changed, 18 insertions, 3 deletions
diff --git a/generator/nanopb_generator.py b/generator/nanopb_generator.py
index 9e6f2e2b..68cc800c 100644
--- a/generator/nanopb_generator.py
+++ b/generator/nanopb_generator.py
@@ -73,6 +73,9 @@ class Names:
else:
raise ValueError("Name parts should be of type str")
+ def __eq__(self, other):
+ return isinstance(other, Names) and self.parts == other.parts
+
def names_from_type_name(type_name):
'''Parse Names() from FieldDescriptorProto type_name'''
if type_name[0] != '.':
@@ -83,12 +86,15 @@ class Enum:
def __init__(self, names, desc, enum_options):
'''desc is EnumDescriptorProto'''
+ self.options = enum_options
+ self.names = names + desc.name
+
if enum_options.long_names:
- self.names = names + desc.name
+ self.values = [(self.names + x.name, x.number) for x in desc.value]
else:
- self.names = names
+ self.values = [(x.name, x.number) for x in desc.value]
- self.values = [(self.names + x.name, x.number) for x in desc.value]
+ self.value_longnames = [names + desc.name + x.name for x in desc.value]
def __str__(self):
result = 'typedef enum _%s {\n' % self.names
@@ -391,6 +397,15 @@ def parse_file(fdesc, file_options):
for enum in message.enum_type:
enums.append(Enum(names, enum, message_options))
+ # Fix field default values where enum short names are used.
+ for enum in enums:
+ if not enum.options.long_names:
+ for message in messages:
+ for field in message.fields:
+ if field.default in enum.value_longnames:
+ idx = enum.value_longnames.index(field.default)
+ field.default = enum.values[idx][0]
+
return enums, messages
def toposort2(data):