From d32d3226580baf52f60fb7dee3921d8cc9aab67c Mon Sep 17 00:00:00 2001 From: Petteri Aimonen Date: Mon, 14 Jan 2013 17:30:49 +0200 Subject: Add field type FT_IGNORE to generator. This allows ignoring fields that are unnecessary or too large for an embedded system using nanopb, while allowing them to remain in the .proto for other platforms. Update issue 51 Status: FixedInGit --- generator/nanopb.proto | 1 + generator/nanopb_generator.py | 8 +++++++- generator/nanopb_pb2.py | 9 +++++++-- tests/options.proto | 9 +++++++++ 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/generator/nanopb.proto b/generator/nanopb.proto index b9100ab..bc40f29 100644 --- a/generator/nanopb.proto +++ b/generator/nanopb.proto @@ -11,6 +11,7 @@ enum FieldType { FT_DEFAULT = 0; // Automatically decide field type, generate static field if possible. FT_CALLBACK = 1; // Always generate a callback field. FT_STATIC = 2; // Generate a static field or raise an exception if not possible. + FT_IGNORE = 3; // Ignore the field completely. } message NanoPBOptions { diff --git a/generator/nanopb_generator.py b/generator/nanopb_generator.py index a583b3f..9e85a11 100644 --- a/generator/nanopb_generator.py +++ b/generator/nanopb_generator.py @@ -306,7 +306,13 @@ class Field: 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.fields = [] + + for f in desc.field: + field_options = get_nanopb_suboptions(f, message_options) + if field_options.type != nanopb_pb2.FT_IGNORE: + self.fields.append(Field(self.name, f, field_options)) + self.packed = message_options.packed_struct self.ordered_fields = self.fields[:] self.ordered_fields.sort() diff --git a/generator/nanopb_pb2.py b/generator/nanopb_pb2.py index d08b82a..4ba18b2 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\"\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') + 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*J\n\tFieldType\x12\x0e\n\nFT_DEFAULT\x10\x00\x12\x0f\n\x0b\x46T_CALLBACK\x10\x01\x12\r\n\tFT_STATIC\x10\x02\x12\r\n\tFT_IGNORE\x10\x03: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', @@ -32,17 +32,22 @@ _FIELDTYPE = descriptor.EnumDescriptor( name='FT_STATIC', index=2, number=2, options=None, type=None), + descriptor.EnumValueDescriptor( + name='FT_IGNORE', index=3, number=3, + options=None, + type=None), ], containing_type=None, options=None, serialized_start=199, - serialized_end=258, + serialized_end=273, ) FT_DEFAULT = 0 FT_CALLBACK = 1 FT_STATIC = 2 +FT_IGNORE = 3 NANOPB_FILEOPT_FIELD_NUMBER = 1010 nanopb_fileopt = descriptor.FieldDescriptor( diff --git a/tests/options.proto b/tests/options.proto index 3e9383c..b5badcf 100644 --- a/tests/options.proto +++ b/tests/options.proto @@ -62,3 +62,12 @@ message my_packed_struct optional int32 myfield = 1; } +// Message with ignored field +// Note: doesn't really test if the field is missing in the output, +// but atleast tests that the output compiles. +message Message6 +{ + required int32 field1 = 1; + optional int32 field2 = 2 [(nanopb).type = FT_IGNORE]; +} + -- cgit 1.2.3-korg