From 2979f5c94afce1462b1a762f55e4a301f1090a75 Mon Sep 17 00:00:00 2001 From: Zhangyi Chen Date: Tue, 31 Oct 2017 11:12:27 +0800 Subject: [PATCH] r53423: Fix the bug that empty required arrays in sub messages are not serialized as empty --- src/mcpack2pb/generator.cpp | 59 +++++++++++++++++++++++++++++-------- 1 file changed, 47 insertions(+), 12 deletions(-) diff --git a/src/mcpack2pb/generator.cpp b/src/mcpack2pb/generator.cpp index cb5cfcc9cd..2e6799d401 100644 --- a/src/mcpack2pb/generator.cpp +++ b/src/mcpack2pb/generator.cpp @@ -765,12 +765,21 @@ static bool generate_parsing(const google::protobuf::Descriptor* d, (printer).Print( \ " serializer.add_multiple_$type$($msg$.$lcfield$().data(), $msg$.$lcfield$_size());\n" \ " serializer.end_array();\n" \ - "} else {\n" \ - " serializer.add_null();\n" \ - "}\n" \ + "}" \ , "msg", msg \ , "type", to_mcpack_typestr(cit, (field)) \ , "lcfield", (field)->lowercase_name()); \ + if ((field)->options().GetExtension(idl_on)) { \ + (printer).Print( \ + " else {\n" \ + " serializer.add_empty_array();\n" \ + "}\n"); \ + } else { \ + (printer).Print( \ + " else {\n" \ + " serializer.add_null();\n" \ + "}\n"); \ + } \ } else if (looser_cond) { \ (printer).Print( \ "if ($msg$.$lcfield$_size()) {\n" \ @@ -783,12 +792,21 @@ static bool generate_parsing(const google::protobuf::Descriptor* d, " serializer.add_$type$($msg$.$lcfield$(j));\n" \ " }\n" \ " serializer.end_array();\n" \ - "} else {\n" \ - " serializer.add_null();\n" \ - "}\n" \ + "}" \ , "msg", msg \ , "type", to_mcpack_typestr(cit, (field)) \ , "lcfield", (field)->lowercase_name()); \ + if ((field)->options().GetExtension(idl_on)) { \ + (printer).Print( \ + " else {\n" \ + " serializer.add_empty_array();\n" \ + "}\n"); \ + } else { \ + (printer).Print( \ + " else {\n" \ + " serializer.add_null();\n" \ + "}\n"); \ + } \ } else { \ if ((field)->type() == google::protobuf::FieldDescriptor::TYPE_ENUM) { \ LOG(ERROR) << "Disallow converting " << (field)->full_name() \ @@ -993,12 +1011,20 @@ static bool generate_serializing(const google::protobuf::Descriptor* d, " serializer.end_object();\n" " }\n" " serializer.end_array();\n" - " }\n" - "} else if (msg.$lcfield$_size()) {\n" + " }" , "field", get_idl_name(f) , "lcfield", f->lowercase_name() , "vmsg2", var_name2); - + if (f->options().GetExtension(idl_on)) { + impl.Print( + " else {\n" + " serializer.add_empty_array(\"$field$\");\n" + " }\n", "field", get_idl_name(f)); + } else { + impl.Print("\n"); + } + impl.Print("} else if (msg.$lcfield$_size()) {\n" + , "lcfield", f->lowercase_name()); impl.Indent(); impl.Print("serializer.begin_object(\"$field$\");\n" , "field", get_idl_name(f)); @@ -1032,12 +1058,21 @@ static bool generate_serializing(const google::protobuf::Descriptor* d, " serializer.end_object();\n" " }\n" " serializer.end_array();\n" - "} else {\n" - " serializer.add_null();\n" - "}\n" + "}" , "vmsg3", var_name3 , "lcfield", f->lowercase_name() , "lcfield2", f2->lowercase_name()); + if (f2->options().GetExtension(idl_on)) { + impl.Print( + " else {\n" + " serializer.add_empty_array();\n" + "}\n"); + } else { + impl.Print( + " else {\n" + " serializer.add_null();\n" + "}\n"); + } } else { impl.Print( "if (msg.$lcfield$(i).has_$lcfield2$()) {\n"