summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPetteri Aimonen <jpa@git.mail.kapsi.fi>2012-10-29 18:55:49 +0200
committerPetteri Aimonen <jpa@git.mail.kapsi.fi>2012-10-29 18:55:49 +0200
commitdb1eefc24bd9d1181dee35683c5fe12329f7d969 (patch)
treecc38ed9ba2fe8ca9b63b1ca9c9077bd9658a0245
parent0ee4bb96b1e53d16a29869864eff87c8934894ae (diff)
Add option to use short names for enum values.
Update issue 38 Status: FixedInGit
-rw-r--r--generator/nanopb.proto13
-rw-r--r--generator/nanopb_generator.py12
-rw-r--r--generator/nanopb_pb2.py27
-rw-r--r--tests/Makefile6
-rw-r--r--tests/options.expected8
-rw-r--r--tests/options.proto6
6 files changed, 58 insertions, 14 deletions
diff --git a/generator/nanopb.proto b/generator/nanopb.proto
index e7bea139..e548a137 100644
--- a/generator/nanopb.proto
+++ b/generator/nanopb.proto
@@ -14,9 +14,17 @@ enum FieldType {
}
message NanoPBOptions {
+ // Allocated size for 'bytes' and 'string' fields.
optional int32 max_size = 1;
+
+ // Allocated number of entries in arrays ('repeated' fields)
optional int32 max_count = 2;
+
+ // Force type of field (callback or static allocation)
optional FieldType type = 3 [default = FT_DEFAULT];
+
+ // Use long names for enums, i.e. EnumName_EnumValue.
+ optional bool long_names = 4 [default = true];
}
// Protocol Buffers extension number registry
@@ -35,7 +43,12 @@ extend google.protobuf.MessageOptions {
optional NanoPBOptions nanopb_msgopt = 1010;
}
+extend google.protobuf.EnumOptions {
+ optional NanoPBOptions nanopb_enumopt = 1010;
+}
+
extend google.protobuf.FieldOptions {
optional NanoPBOptions nanopb = 1010;
}
+
diff --git a/generator/nanopb_generator.py b/generator/nanopb_generator.py
index 860f3742..2267238c 100644
--- a/generator/nanopb_generator.py
+++ b/generator/nanopb_generator.py
@@ -81,7 +81,12 @@ def names_from_type_name(type_name):
class Enum:
def __init__(self, names, desc, enum_options):
'''desc is EnumDescriptorProto'''
- self.names = names + desc.name
+
+ if enum_options.long_names:
+ self.names = names + desc.name
+ else:
+ self.names = names
+
self.values = [(self.names + x.name, x.number) for x in desc.value]
def __str__(self):
@@ -376,7 +381,8 @@ def parse_file(fdesc, file_options):
base_name = Names()
for enum in fdesc.enum_type:
- enums.append(Enum(base_name, enum, file_options))
+ enum_options = get_nanopb_suboptions(enum, file_options)
+ enums.append(Enum(base_name, enum, enum_options))
for names, message in iterate_messages(fdesc, base_name):
message_options = get_nanopb_suboptions(message, file_options)
@@ -548,6 +554,8 @@ def get_nanopb_suboptions(subdesc, options):
ext_type = nanopb_pb2.nanopb_fileopt
elif isinstance(subdesc.options, descriptor.MessageOptions):
ext_type = nanopb_pb2.nanopb_msgopt
+ elif isinstance(subdesc.options, descriptor.EnumOptions):
+ ext_type = nanopb_pb2.nanopb_enumopt
else:
raise Exception("Unknown options type")
diff --git a/generator/nanopb_pb2.py b/generator/nanopb_pb2.py
index 502726a5..c48a412e 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\"Z\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*;\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:>\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\"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')
_FIELDTYPE = descriptor.EnumDescriptor(
name='FieldType',
@@ -35,8 +35,8 @@ _FIELDTYPE = descriptor.EnumDescriptor(
],
containing_type=None,
options=None,
- serialized_start=142,
- serialized_end=201,
+ serialized_start=168,
+ serialized_end=227,
)
@@ -60,9 +60,17 @@ nanopb_msgopt = descriptor.FieldDescriptor(
message_type=None, enum_type=None, containing_type=None,
is_extension=True, extension_scope=None,
options=None)
+NANOPB_ENUMOPT_FIELD_NUMBER = 1010
+nanopb_enumopt = descriptor.FieldDescriptor(
+ name='nanopb_enumopt', full_name='nanopb_enumopt', index=2,
+ number=1010, type=11, cpp_type=10, label=1,
+ has_default_value=False, default_value=None,
+ message_type=None, enum_type=None, containing_type=None,
+ is_extension=True, extension_scope=None,
+ options=None)
NANOPB_FIELD_NUMBER = 1010
nanopb = descriptor.FieldDescriptor(
- name='nanopb', full_name='nanopb', index=2,
+ name='nanopb', full_name='nanopb', index=3,
number=1010, type=11, cpp_type=10, label=1,
has_default_value=False, default_value=None,
message_type=None, enum_type=None, containing_type=None,
@@ -98,6 +106,13 @@ _NANOPBOPTIONS = descriptor.Descriptor(
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
options=None),
+ descriptor.FieldDescriptor(
+ name='long_names', full_name='NanoPBOptions.long_names', index=3,
+ number=4, type=8, cpp_type=7, label=1,
+ has_default_value=True, default_value=True,
+ message_type=None, enum_type=None, containing_type=None,
+ is_extension=False, extension_scope=None,
+ options=None),
],
extensions=[
],
@@ -108,7 +123,7 @@ _NANOPBOPTIONS = descriptor.Descriptor(
is_extendable=False,
extension_ranges=[],
serialized_start=50,
- serialized_end=140,
+ serialized_end=166,
)
_NANOPBOPTIONS.fields_by_name['type'].enum_type = _FIELDTYPE
@@ -124,6 +139,8 @@ nanopb_fileopt.message_type = _NANOPBOPTIONS
google.protobuf.descriptor_pb2.FileOptions.RegisterExtension(nanopb_fileopt)
nanopb_msgopt.message_type = _NANOPBOPTIONS
google.protobuf.descriptor_pb2.MessageOptions.RegisterExtension(nanopb_msgopt)
+nanopb_enumopt.message_type = _NANOPBOPTIONS
+google.protobuf.descriptor_pb2.EnumOptions.RegisterExtension(nanopb_enumopt)
nanopb.message_type = _NANOPBOPTIONS
google.protobuf.descriptor_pb2.FieldOptions.RegisterExtension(nanopb)
# @@protoc_insertion_point(module_scope)
diff --git a/tests/Makefile b/tests/Makefile
index 434819c3..99e27ffd 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -83,9 +83,9 @@ run_unittests: decode_unittests encode_unittests test_cxxcompile test_encode1 te
./test_missing_fields
test_options: options.pb.h options.expected
- for p in $$(grep . options.expected); do \
- if ! grep -qF "$$p" $<; then \
- echo Expected: $$p; \
+ cat options.expected | while read -r p; do \
+ if ! grep -q "$$p" $<; then \
+ echo Expected: "$$p"; \
exit 1; \
fi \
done
diff --git a/tests/options.expected b/tests/options.expected
index ef74a00d..960daeee 100644
--- a/tests/options.expected
+++ b/tests/options.expected
@@ -1,5 +1,5 @@
-char filesize[20];
-char msgsize[30];
-char fieldsize[40];
+char filesize\[20\];
+char msgsize\[30\];
+char fieldsize\[40\];
pb_callback_t int32_callback;
-
+[^_]EnumValue1 = 1
diff --git a/tests/options.proto b/tests/options.proto
index 6ba7c070..e296bcd1 100644
--- a/tests/options.proto
+++ b/tests/options.proto
@@ -31,3 +31,9 @@ message Message4
required int32 int32_callback = 1 [(nanopb).type = FT_CALLBACK];
}
+// Short enum names
+enum Enum1
+{
+ option (nanopb_enumopt).long_names = false;
+ EnumValue1 = 1;
+}