Allow overriding proto3 mode (#228)
authorPetteri Aimonen <jpa@git.mail.kapsi.fi>
Thu, 22 Dec 2016 15:37:14 +0000 (17:37 +0200)
committerPetteri Aimonen <jpa@git.mail.kapsi.fi>
Thu, 22 Dec 2016 15:37:14 +0000 (17:37 +0200)
generator/nanopb_generator.py
tests/options/SConscript
tests/options/proto3_options.expected [new file with mode: 0644]
tests/options/proto3_options.proto [new file with mode: 0644]

index 066ef93..a2ee22d 100755 (executable)
@@ -285,7 +285,7 @@ class Field:
                 can_be_static = False
             else:
                 self.array_decl = '[%d]' % self.max_count
-        elif field_options.HasField("proto3"):
+        elif field_options.proto3:
             self.rules = 'SINGULAR'
         elif desc.label == FieldD.LABEL_REQUIRED:
             self.rules = 'REQUIRED'
@@ -1373,6 +1373,9 @@ def get_nanopb_suboptions(subdesc, options, name):
     new_options = nanopb_pb2.NanoPBOptions()
     new_options.CopyFrom(options)
 
+    if hasattr(subdesc, 'syntax') and subdesc.syntax == "proto3":
+        new_options.proto3 = True
+
     # Handle options defined in a separate file
     dotname = '.'.join(name.parts)
     for namemask, options in Globals.separate_options:
@@ -1380,9 +1383,6 @@ def get_nanopb_suboptions(subdesc, options, name):
             Globals.matched_namemasks.add(namemask)
             new_options.MergeFrom(options)
 
-    if hasattr(subdesc, 'syntax') and subdesc.syntax == "proto3":
-        new_options.proto3 = True
-
     # Handle options defined in .proto
     if isinstance(subdesc.options, descriptor.FieldOptions):
         ext_type = nanopb_pb2.nanopb
index 89a00fa..215e3bd 100644 (file)
@@ -4,6 +4,9 @@ Import("env")
 
 env.NanopbProto("options")
 env.Object('options.pb.c')
-
 env.Match(['options.pb.h', 'options.expected'])
 
+env.NanopbProto("proto3_options")
+env.Object('proto3_options.pb.c')
+env.Match(['proto3_options.pb.h', 'proto3_options.expected'])
+
diff --git a/tests/options/proto3_options.expected b/tests/options/proto3_options.expected
new file mode 100644 (file)
index 0000000..cc2f29c
--- /dev/null
@@ -0,0 +1,4 @@
+! bool has_proto3_default
+bool has_proto3_off
+! bool has_proto3_on
+
diff --git a/tests/options/proto3_options.proto b/tests/options/proto3_options.proto
new file mode 100644 (file)
index 0000000..1017f04
--- /dev/null
@@ -0,0 +1,11 @@
+syntax = "proto3";
+
+import "nanopb.proto";
+
+message Message1
+{
+    int32 proto3_default = 1;
+    int32 proto3_off = 2 [(nanopb).proto3 = false];
+    int32 proto3_on = 3 [(nanopb).proto3 = true];
+}
+