From 02ecee2de89f3225a6cd4bf87b0e4285bb1d06eb Mon Sep 17 00:00:00 2001 From: Petteri Aimonen Date: Wed, 14 Nov 2012 00:20:44 +0200 Subject: 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 --- generator/nanopb_generator.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) (limited to 'generator/nanopb_generator.py') 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): -- cgit 1.2.3-korg