Jelajahi Sumber

Revert the change to wire_format.h.

It seems too much code relies on the broken behaviour. See issue #493.
Instead, we reimplement MakeTag just for C#, temporarily.
Jon Skeet 10 tahun lalu
induk
melakukan
322ec53161

+ 1 - 1
src/google/protobuf/compiler/csharp/csharp_field_base.cc

@@ -57,7 +57,7 @@ void FieldGeneratorBase::SetCommonFieldVariables(
   // repeated fields varies by wire format. The wire format is encoded in the bottom 3 bits, which
   // never effects the tag size.
   int tag_size = internal::WireFormat::TagSize(descriptor_->number(), descriptor_->type());
-  uint tag = internal::WireFormat::MakeTag(descriptor_);
+  uint tag = FixedMakeTag(descriptor_);
   uint8 tag_array[5];
   io::CodedOutputStream::WriteTagToArray(tag, tag_array);
   string tag_bytes = SimpleItoa(tag_array[0]);

+ 11 - 0
src/google/protobuf/compiler/csharp/csharp_helpers.cc

@@ -338,6 +338,17 @@ std::string FileDescriptorToBase64(const FileDescriptor* descriptor) {
   return StringToBase64(fdp_bytes);
 }
 
+// TODO(jonskeet): Remove this when internal::WireFormat::MakeTag works
+// properly...
+// Workaround for issue #493
+uint FixedMakeTag(const FieldDescriptor* field) {
+  internal::WireFormatLite::WireType field_type = field->is_packed()
+      ? internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED
+      : internal::WireFormat::WireTypeForFieldType(field->type());
+
+  return internal::WireFormatLite::MakeTag(field->number(), field_type);
+}
+
 FieldGeneratorBase* CreateFieldGenerator(const FieldDescriptor* descriptor,
                                          int fieldOrdinal) {
   switch (descriptor->type()) {

+ 2 - 0
src/google/protobuf/compiler/csharp/csharp_helpers.h

@@ -97,6 +97,8 @@ std::string StringToBase64(const std::string& input);
 
 std::string FileDescriptorToBase64(const FileDescriptor* descriptor);
 
+uint FixedMakeTag(const FieldDescriptor* descriptor);
+
 FieldGeneratorBase* CreateFieldGenerator(const FieldDescriptor* descriptor, int fieldOrdinal);
 
 bool HasRequiredFields(const Descriptor* descriptor);

+ 1 - 2
src/google/protobuf/compiler/csharp/csharp_message.cc

@@ -194,8 +194,7 @@ void MessageGenerator::Generate(io::Printer* printer) {
       "slash", field_names().size() > 0 ? "\"" : "");
   std::vector<std::string> tags;
   for (int i = 0; i < field_names().size(); i++) {
-    uint32 tag = internal::WireFormat::MakeTag(
-        descriptor_->FindFieldByName(field_names()[i]));
+    uint32 tag = FixedMakeTag(descriptor_->FindFieldByName(field_names()[i]));
     tags.push_back(SimpleItoa(tag));
   }
   printer->Print(

+ 1 - 1
src/google/protobuf/wire_format.h

@@ -290,7 +290,7 @@ class LIBPROTOBUF_EXPORT UnknownFieldSetFieldSkipper : public FieldSkipper {
 
 inline WireFormatLite::WireType WireFormat::WireTypeForField(
     const FieldDescriptor* field) {
-  if (field->is_packed()) {
+  if (field->options().packed()) {
     return WireFormatLite::WIRETYPE_LENGTH_DELIMITED;
   } else {
     return WireTypeForFieldType(field->type());