diff options
-rw-r--r-- | generator/nanopb_generator.py | 21 | ||||
-rw-r--r-- | tests/Makefile | 2 | ||||
-rw-r--r-- | tests/options.expected | 2 | ||||
-rw-r--r-- | tests/options.proto | 6 |
4 files changed, 26 insertions, 5 deletions
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): diff --git a/tests/Makefile b/tests/Makefile index 99e27ffd..1f2be716 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -82,7 +82,7 @@ run_unittests: decode_unittests encode_unittests test_cxxcompile test_encode1 te ./test_missing_fields -test_options: options.pb.h options.expected +test_options: options.pb.h options.expected options.pb.o cat options.expected | while read -r p; do \ if ! grep -q "$$p" $<; then \ echo Expected: "$$p"; \ diff --git a/tests/options.expected b/tests/options.expected index 960daeee..61d9805e 100644 --- a/tests/options.expected +++ b/tests/options.expected @@ -2,4 +2,4 @@ char filesize\[20\]; char msgsize\[30\]; char fieldsize\[40\]; pb_callback_t int32_callback; -[^_]EnumValue1 = 1 +\sEnumValue1 = 1 diff --git a/tests/options.proto b/tests/options.proto index e296bcd1..7eb2eb30 100644 --- a/tests/options.proto +++ b/tests/options.proto @@ -36,4 +36,10 @@ enum Enum1 { option (nanopb_enumopt).long_names = false; EnumValue1 = 1; + EnumValue2 = 2; +} + +message EnumTest +{ + required Enum1 field = 1 [default = EnumValue2]; } |