Add default_has option (#423)
diff --git a/generator/nanopb_generator.py b/generator/nanopb_generator.py
index 1fa98ee..57c670e 100755
--- a/generator/nanopb_generator.py
+++ b/generator/nanopb_generator.py
@@ -343,6 +343,8 @@
if field_options.HasField("max_size"):
self.max_size = field_options.max_size
+ self.default_has = field_options.default_has
+
if desc.type == FieldD.TYPE_STRING and field_options.HasField("max_length"):
# max_length overrides max_size for strings
self.max_size = field_options.max_length + 1
@@ -575,7 +577,10 @@
elif self.rules == 'FIXARRAY':
outer_init = '{' + ', '.join([inner_init] * self.max_count) + '}'
elif self.rules == 'OPTIONAL':
- outer_init = 'false, ' + inner_init
+ if null_init or not self.default_has:
+ outer_init = 'false, ' + inner_init
+ else:
+ outer_init = 'true, ' + inner_init
else:
outer_init = inner_init
elif self.allocation == 'POINTER':
diff --git a/generator/proto/nanopb.proto b/generator/proto/nanopb.proto
index 83ad9c4..5368c52 100644
--- a/generator/proto/nanopb.proto
+++ b/generator/proto/nanopb.proto
@@ -125,6 +125,9 @@
// ok, but if it results in compilation errors you can increase the field
// size here.
optional DescriptorSize descriptorsize = 20 [default = DS_AUTO];
+
+ // Set default value for has_ fields.
+ optional bool default_has = 23 [default = false];
}
// Extensions to protoc 'Descriptor' type in order to define options
diff --git a/tests/options/SConscript b/tests/options/SConscript
index 3e9e6a4..4847233 100644
--- a/tests/options/SConscript
+++ b/tests/options/SConscript
@@ -11,3 +11,5 @@
env.Object('proto3_options.pb.c')
env.Match(['proto3_options.pb.h', 'proto3_options.expected'])
+p = env.Program(["options.c", "options.pb.c", "$COMMON/pb_decode.o", "$COMMON/pb_encode.o", "$COMMON/pb_common.o"])
+env.RunTest(p)
diff --git a/tests/options/options.c b/tests/options/options.c
new file mode 100644
index 0000000..0d93fdc
--- /dev/null
+++ b/tests/options/options.c
@@ -0,0 +1,28 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include "options.pb.h"
+#include "unittests.h"
+
+int main()
+{
+ int status = 0;
+
+ {
+ HasFieldMessage msg1 = HasFieldMessage_init_default;
+ HasFieldMessage msg2 = HasFieldMessage_init_zero;
+
+ COMMENT("Test default_has option");
+
+ /* Default initializer should obey has_default setting */
+ TEST(msg1.has_present == true);
+ TEST(msg1.has_missing == false);
+ TEST(msg1.has_normal == false);
+
+ /* Zero initializer should always have false */
+ TEST(msg2.has_present == false);
+ TEST(msg2.has_missing == false);
+ TEST(msg2.has_normal == false);
+ }
+
+ return status;
+}
diff --git a/tests/options/options.proto b/tests/options/options.proto
index db143ff..e8c3f46 100644
--- a/tests/options/options.proto
+++ b/tests/options/options.proto
@@ -102,3 +102,11 @@
option (nanopb_msgopt).descriptorsize = DS_4;
required int32 foo = 1;
}
+
+// Default value for has_ field
+message HasFieldMessage
+{
+ optional int32 present = 1 [(nanopb).default_has = true];
+ optional int32 missing = 2 [(nanopb).default_has = false];
+ optional int32 normal = 3;
+}