diff options
-rw-r--r-- | generator/nanopb.proto | 3 | ||||
-rw-r--r-- | generator/nanopb_generator.py | 8 | ||||
-rw-r--r-- | generator/nanopb_pb2.py | 17 | ||||
-rw-r--r-- | tests/options.expected | 1 | ||||
-rw-r--r-- | tests/options.proto | 8 |
5 files changed, 31 insertions, 6 deletions
diff --git a/generator/nanopb.proto b/generator/nanopb.proto index 94ce7d9c..b9100abd 100644 --- a/generator/nanopb.proto +++ b/generator/nanopb.proto @@ -25,6 +25,9 @@ message NanoPBOptions { // Use long names for enums, i.e. EnumName_EnumValue. optional bool long_names = 4 [default = true]; + + // Add 'packed' attribute to generated structs. + optional bool packed_struct = 5 [default = false]; } // Protocol Buffers extension number registry diff --git a/generator/nanopb_generator.py b/generator/nanopb_generator.py index bffdb353..a583b3f6 100644 --- a/generator/nanopb_generator.py +++ b/generator/nanopb_generator.py @@ -307,6 +307,7 @@ class Message: def __init__(self, names, desc, message_options): self.name = names self.fields = [Field(self.name, f, get_nanopb_suboptions(f, message_options)) for f in desc.field] + self.packed = message_options.packed_struct self.ordered_fields = self.fields[:] self.ordered_fields.sort() @@ -317,7 +318,12 @@ class Message: def __str__(self): result = 'typedef struct _%s {\n' % self.name result += '\n'.join([str(f) for f in self.ordered_fields]) - result += '\n} %s;' % self.name + result += '\n}' + + if self.packed: + result += ' pb_packed' + + result += ' %s;' % self.name return result def types(self): diff --git a/generator/nanopb_pb2.py b/generator/nanopb_pb2.py index c48a412e..d08b82ab 100644 --- a/generator/nanopb_pb2.py +++ b/generator/nanopb_pb2.py @@ -12,7 +12,7 @@ import google.protobuf.descriptor_pb2 DESCRIPTOR = descriptor.FileDescriptor( name='nanopb.proto', package='', - serialized_pb='\n\x0cnanopb.proto\x1a google/protobuf/descriptor.proto\"t\n\rNanoPBOptions\x12\x10\n\x08max_size\x18\x01 \x01(\x05\x12\x11\n\tmax_count\x18\x02 \x01(\x05\x12$\n\x04type\x18\x03 \x01(\x0e\x32\n.FieldType:\nFT_DEFAULT\x12\x18\n\nlong_names\x18\x04 \x01(\x08:\x04true*;\n\tFieldType\x12\x0e\n\nFT_DEFAULT\x10\x00\x12\x0f\n\x0b\x46T_CALLBACK\x10\x01\x12\r\n\tFT_STATIC\x10\x02:E\n\x0enanopb_fileopt\x12\x1c.google.protobuf.FileOptions\x18\xf2\x07 \x01(\x0b\x32\x0e.NanoPBOptions:G\n\rnanopb_msgopt\x12\x1f.google.protobuf.MessageOptions\x18\xf2\x07 \x01(\x0b\x32\x0e.NanoPBOptions:E\n\x0enanopb_enumopt\x12\x1c.google.protobuf.EnumOptions\x18\xf2\x07 \x01(\x0b\x32\x0e.NanoPBOptions:>\n\x06nanopb\x12\x1d.google.protobuf.FieldOptions\x18\xf2\x07 \x01(\x0b\x32\x0e.NanoPBOptions') + serialized_pb='\n\x0cnanopb.proto\x1a google/protobuf/descriptor.proto\"\x92\x01\n\rNanoPBOptions\x12\x10\n\x08max_size\x18\x01 \x01(\x05\x12\x11\n\tmax_count\x18\x02 \x01(\x05\x12$\n\x04type\x18\x03 \x01(\x0e\x32\n.FieldType:\nFT_DEFAULT\x12\x18\n\nlong_names\x18\x04 \x01(\x08:\x04true\x12\x1c\n\rpacked_struct\x18\x05 \x01(\x08:\x05\x66\x61lse*;\n\tFieldType\x12\x0e\n\nFT_DEFAULT\x10\x00\x12\x0f\n\x0b\x46T_CALLBACK\x10\x01\x12\r\n\tFT_STATIC\x10\x02:E\n\x0enanopb_fileopt\x12\x1c.google.protobuf.FileOptions\x18\xf2\x07 \x01(\x0b\x32\x0e.NanoPBOptions:G\n\rnanopb_msgopt\x12\x1f.google.protobuf.MessageOptions\x18\xf2\x07 \x01(\x0b\x32\x0e.NanoPBOptions:E\n\x0enanopb_enumopt\x12\x1c.google.protobuf.EnumOptions\x18\xf2\x07 \x01(\x0b\x32\x0e.NanoPBOptions:>\n\x06nanopb\x12\x1d.google.protobuf.FieldOptions\x18\xf2\x07 \x01(\x0b\x32\x0e.NanoPBOptions') _FIELDTYPE = descriptor.EnumDescriptor( name='FieldType', @@ -35,8 +35,8 @@ _FIELDTYPE = descriptor.EnumDescriptor( ], containing_type=None, options=None, - serialized_start=168, - serialized_end=227, + serialized_start=199, + serialized_end=258, ) @@ -113,6 +113,13 @@ _NANOPBOPTIONS = descriptor.Descriptor( message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, options=None), + descriptor.FieldDescriptor( + name='packed_struct', full_name='NanoPBOptions.packed_struct', index=4, + number=5, type=8, cpp_type=7, label=1, + has_default_value=True, default_value=False, + message_type=None, enum_type=None, containing_type=None, + is_extension=False, extension_scope=None, + options=None), ], extensions=[ ], @@ -122,8 +129,8 @@ _NANOPBOPTIONS = descriptor.Descriptor( options=None, is_extendable=False, extension_ranges=[], - serialized_start=50, - serialized_end=166, + serialized_start=51, + serialized_end=197, ) _NANOPBOPTIONS.fields_by_name['type'].enum_type = _FIELDTYPE diff --git a/tests/options.expected b/tests/options.expected index 93501430..e6179a27 100644 --- a/tests/options.expected +++ b/tests/options.expected @@ -4,3 +4,4 @@ char fieldsize\[40\]; pb_callback_t int32_callback; \sEnumValue1 = 1 Message5_EnumValue1 +} pb_packed my_packed_struct; diff --git a/tests/options.proto b/tests/options.proto index 413d21b5..3e9383cd 100644 --- a/tests/options.proto +++ b/tests/options.proto @@ -54,3 +54,11 @@ message Message5 } required Enum2 field = 1 [default = EnumValue1]; } + +// Packed structure +message my_packed_struct +{ + option (nanopb_msgopt).packed_struct = true; + optional int32 myfield = 1; +} + |