Răsfoiți Sursa

Merge extensions correctly

Jon Skeet 16 ani în urmă
părinte
comite
49fcd4f794

+ 4 - 0
src/ProtoGen/MessageGenerator.cs

@@ -351,6 +351,10 @@ namespace Google.ProtocolBuffers.ProtoGen {
         foreach (FieldDescriptor field in Descriptor.Fields) {
           SourceGenerators.CreateFieldGenerator(field).GenerateMergingCode(writer);
         }
+        // if message type has extensions
+        if (Descriptor.Proto.ExtensionRangeCount > 0) {
+          writer.WriteLine("  this.MergeExtensionFields(other);");
+        }
         writer.WriteLine("this.MergeUnknownFields(other.UnknownFields);");
         writer.WriteLine("return this;");
         writer.Outdent();

+ 1 - 3
src/ProtocolBuffers.Test/GeneratedMessageTest.cs

@@ -313,7 +313,6 @@ namespace Google.ProtocolBuffers {
           .GetExtensionCount(UnitTestProtoFile.RepeatedInt32Extension));
     }
 
-    /* Reinstate this test in the commit where it's fixed...
     [Test]
     public void ExtensionMergeFrom() {
       TestAllExtensions original = TestAllExtensions.CreateBuilder()
@@ -322,8 +321,7 @@ namespace Google.ProtocolBuffers {
           TestAllExtensions.CreateBuilder().MergeFrom(original).Build();
       Assert.IsTrue((merged.HasExtension(UnitTestProtoFile.OptionalInt32Extension)));
       Assert.AreEqual(1, (int)merged.GetExtension(UnitTestProtoFile.OptionalInt32Extension));
-    }
-    */
+    }    
 
     /* Removed multiple files option for the moment
     [Test]

+ 1 - 0
src/ProtocolBuffers.Test/TestProtos/UnitTestMessageSetProtoFile.cs

@@ -194,6 +194,7 @@ namespace Google.ProtocolBuffers.TestProtos {
       
       public override Builder MergeFrom(TestMessageSet other) {
         if (other == TestMessageSet.DefaultInstance) return this;
+          this.MergeExtensionFields(other);
         this.MergeUnknownFields(other.UnknownFields);
         return this;
       }

+ 3 - 0
src/ProtocolBuffers.Test/TestProtos/UnitTestProtoFile.cs

@@ -4884,6 +4884,7 @@ namespace Google.ProtocolBuffers.TestProtos {
       
       public override Builder MergeFrom(TestAllExtensions other) {
         if (other == TestAllExtensions.DefaultInstance) return this;
+          this.MergeExtensionFields(other);
         this.MergeUnknownFields(other.UnknownFields);
         return this;
       }
@@ -7597,6 +7598,7 @@ namespace Google.ProtocolBuffers.TestProtos {
       
       public override Builder MergeFrom(TestEmptyMessageWithExtensions other) {
         if (other == TestEmptyMessageWithExtensions.DefaultInstance) return this;
+          this.MergeExtensionFields(other);
         this.MergeUnknownFields(other.UnknownFields);
         return this;
       }
@@ -10714,6 +10716,7 @@ namespace Google.ProtocolBuffers.TestProtos {
         if (other.HasMyFloat) {
           MyFloat = other.MyFloat;
         }
+          this.MergeExtensionFields(other);
         this.MergeUnknownFields(other.UnknownFields);
         return this;
       }

+ 7 - 0
src/ProtocolBuffers/DescriptorProtos/DescriptorProtoFile.cs

@@ -3936,6 +3936,7 @@ namespace Google.ProtocolBuffers.DescriptorProtos {
         if (other.uninterpretedOption_.Count != 0) {
           base.AddRange(other.uninterpretedOption_, result.uninterpretedOption_);
         }
+          this.MergeExtensionFields(other);
         this.MergeUnknownFields(other.UnknownFields);
         return this;
       }
@@ -4273,6 +4274,7 @@ namespace Google.ProtocolBuffers.DescriptorProtos {
         if (other.uninterpretedOption_.Count != 0) {
           base.AddRange(other.uninterpretedOption_, result.uninterpretedOption_);
         }
+          this.MergeExtensionFields(other);
         this.MergeUnknownFields(other.UnknownFields);
         return this;
       }
@@ -4565,6 +4567,7 @@ namespace Google.ProtocolBuffers.DescriptorProtos {
         if (other.uninterpretedOption_.Count != 0) {
           base.AddRange(other.uninterpretedOption_, result.uninterpretedOption_);
         }
+          this.MergeExtensionFields(other);
         this.MergeUnknownFields(other.UnknownFields);
         return this;
       }
@@ -4839,6 +4842,7 @@ namespace Google.ProtocolBuffers.DescriptorProtos {
         if (other.uninterpretedOption_.Count != 0) {
           base.AddRange(other.uninterpretedOption_, result.uninterpretedOption_);
         }
+          this.MergeExtensionFields(other);
         this.MergeUnknownFields(other.UnknownFields);
         return this;
       }
@@ -5063,6 +5067,7 @@ namespace Google.ProtocolBuffers.DescriptorProtos {
         if (other.uninterpretedOption_.Count != 0) {
           base.AddRange(other.uninterpretedOption_, result.uninterpretedOption_);
         }
+          this.MergeExtensionFields(other);
         this.MergeUnknownFields(other.UnknownFields);
         return this;
       }
@@ -5287,6 +5292,7 @@ namespace Google.ProtocolBuffers.DescriptorProtos {
         if (other.uninterpretedOption_.Count != 0) {
           base.AddRange(other.uninterpretedOption_, result.uninterpretedOption_);
         }
+          this.MergeExtensionFields(other);
         this.MergeUnknownFields(other.UnknownFields);
         return this;
       }
@@ -5511,6 +5517,7 @@ namespace Google.ProtocolBuffers.DescriptorProtos {
         if (other.uninterpretedOption_.Count != 0) {
           base.AddRange(other.uninterpretedOption_, result.uninterpretedOption_);
         }
+          this.MergeExtensionFields(other);
         this.MergeUnknownFields(other.UnknownFields);
         return this;
       }

+ 2 - 5
src/ProtocolBuffers/DynamicMessage.cs

@@ -284,15 +284,12 @@ namespace Google.ProtocolBuffers {
           throw new ArgumentException("MergeFrom(IMessage) can only merge messages of the same type.");
         }
         fields.MergeFrom(other);
+        MergeUnknownFields(other.UnknownFields);
         return this;
       }
 
       public override Builder MergeFrom(DynamicMessage other) {
-        if (other.DescriptorForType != type) {
-          throw new ArgumentException("MergeFrom(IMessage) can only merge messages of the same type.");
-        }
-        fields.MergeFrom(other);
-        return this;
+        return MergeFrom((IMessage)other);
       }
 
       public override DynamicMessage Build() {

+ 5 - 1
src/ProtocolBuffers/ExtendableBuilder.cs

@@ -159,12 +159,16 @@ namespace Google.ProtocolBuffers {
     public override TBuilder AddRepeatedField(FieldDescriptor field, object value) {
       if (field.IsExtension) {
         ExtendableMessage<TMessage, TBuilder> message = MessageBeingBuilt;
-        message.VerifyContainingType(field); 
+        message.VerifyContainingType(field);
         message.Extensions.AddRepeatedField(field, value);
         return ThisBuilder;
       } else {
         return base.AddRepeatedField(field, value);
       }
     }
+
+    protected void MergeExtensionFields(ExtendableMessage<TMessage, TBuilder> other) {
+      MessageBeingBuilt.Extensions.MergeFrom(other.Extensions);
+    }
   }
 }