Workaround for proto3
@@ -14,6 +14,21 @@ namespace protobuf {
namespace compiler {
namespace perlxs {
+inline bool HasFieldPresence(const FileDescriptor* file) {
+ return file->syntax() != FileDescriptor::SYNTAX_PROTO3;
+}
+
+bool HasHasMethod(const FieldDescriptor* field) {
+ if (HasFieldPresence(field->file())) {
+ // In proto1/proto2, every field has a has_$name$() method.
+ return true;
+ }
+ // For message types without true field presence, only fields with a message
+ // type have a has_$name$() method.
+ return field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE;
+}
+
+
PerlXSGenerator::PerlXSGenerator() {}
PerlXSGenerator::~PerlXSGenerator() {}
@@ -749,7 +764,7 @@ PerlXSGenerator::GenerateMessageXSFieldAccessors(const FieldDescriptor* field,
"\n"
" OUTPUT:\n"
" RETVAL\n");
- } else {
+ } else if ( HasHasMethod(field) ) {
printer.Print(vars,
"I32\n"
"has_$perlname$(svTHIS)\n"
@@ -1712,9 +1727,15 @@ PerlXSGenerator::StartFieldToHashref(const FieldDescriptor * field,
"$i$ < msg$pdepth$->$cppname$_size(); $i$++ ) {\n");
} else {
vars["i"] = "";
+ if ( HasHasMethod(field) ) {
printer.Print(vars,
"if ( msg$pdepth$->has_$cppname$() ) {\n");
}
+ else {
+ printer.Print(vars,
+ "{\n");
+ }
+ }
printer.Indent();
}
