aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPetteri Aimonen <jpa@git.mail.kapsi.fi>2013-09-08 11:05:20 +0300
committerPetteri Aimonen <jpa@git.mail.kapsi.fi>2013-09-08 11:05:20 +0300
commitd7f3a74388b4825d2c980c53d0a740ddfd0e4770 (patch)
tree2041f8c3a43ee6be7df1a9133674b006770f2837
parent4aef194a99705805153471c371e16a3633a4cc4e (diff)
Handle unsupported extension field types more gracefully.
Previously the generator would stop with NotImplementedException as soon as a required or repeated extension field is found. New behaviour is to just ignore the unsupported field and note that in a comment in the generated file. Furthermore, allow skipping of extension fields using the generator option (nanopb).type = FT_IGNORE. Update issue 83 Status: FixedInGit
-rw-r--r--generator/nanopb_generator.py19
-rw-r--r--tests/extensions.proto2
2 files changed, 16 insertions, 5 deletions
diff --git a/generator/nanopb_generator.py b/generator/nanopb_generator.py
index e3cddb61..55e5ab65 100644
--- a/generator/nanopb_generator.py
+++ b/generator/nanopb_generator.py
@@ -310,18 +310,26 @@ class ExtensionField(Field):
Field.__init__(self, self.fullname + 'struct', desc, field_options)
if self.rules != 'OPTIONAL':
- raise NotImplementedError("Only 'optional' is supported for extension fields. "
- + "(%s.rules == %s)" % (self.fullname, self.rules))
-
- self.rules = 'OPTEXT'
+ self.skip = True
+ else:
+ self.skip = False
+ self.rules = 'OPTEXT'
def extension_decl(self):
'''Declaration of the extension type in the .pb.h file'''
+ if self.skip:
+ msg = '/* Extension field %s was skipped because only "optional"\n' % self.fullname
+ msg +=' type of extension fields is currently supported. */\n'
+ return msg
+
return 'extern const pb_extension_type_t %s;\n' % self.fullname
def extension_def(self):
'''Definition of the extension type in the .pb.c file'''
+ if self.skip:
+ return ''
+
result = 'typedef struct {\n'
result += str(self)
result += '\n} %s;\n\n' % self.struct_name
@@ -475,7 +483,8 @@ def parse_file(fdesc, file_options):
for names, extension in iterate_extensions(fdesc, base_name):
field_options = get_nanopb_suboptions(extension, file_options, names)
- extensions.append(ExtensionField(names, extension, field_options))
+ if field_options.type != nanopb_pb2.FT_IGNORE:
+ extensions.append(ExtensionField(names, extension, field_options))
# Fix field default values where enum short names are used.
for enum in enums:
diff --git a/tests/extensions.proto b/tests/extensions.proto
index cbffdce6..d85e8193 100644
--- a/tests/extensions.proto
+++ b/tests/extensions.proto
@@ -7,6 +7,8 @@ extend AllTypes {
message ExtensionMessage {
extend AllTypes {
optional ExtensionMessage AllTypes_extensionfield2 = 254;
+ required ExtensionMessage AllTypes_extensionfield3 = 253;
+ repeated ExtensionMessage AllTypes_extensionfield4 = 252;
}
required string test1 = 1;