Commit 49fcd4f7 authored by Jon Skeet's avatar Jon Skeet

Merge extensions correctly

parent 642a8140
...@@ -351,6 +351,10 @@ namespace Google.ProtocolBuffers.ProtoGen { ...@@ -351,6 +351,10 @@ namespace Google.ProtocolBuffers.ProtoGen {
foreach (FieldDescriptor field in Descriptor.Fields) { foreach (FieldDescriptor field in Descriptor.Fields) {
SourceGenerators.CreateFieldGenerator(field).GenerateMergingCode(writer); 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("this.MergeUnknownFields(other.UnknownFields);");
writer.WriteLine("return this;"); writer.WriteLine("return this;");
writer.Outdent(); writer.Outdent();
......
...@@ -313,7 +313,6 @@ namespace Google.ProtocolBuffers { ...@@ -313,7 +313,6 @@ namespace Google.ProtocolBuffers {
.GetExtensionCount(UnitTestProtoFile.RepeatedInt32Extension)); .GetExtensionCount(UnitTestProtoFile.RepeatedInt32Extension));
} }
/* Reinstate this test in the commit where it's fixed...
[Test] [Test]
public void ExtensionMergeFrom() { public void ExtensionMergeFrom() {
TestAllExtensions original = TestAllExtensions.CreateBuilder() TestAllExtensions original = TestAllExtensions.CreateBuilder()
...@@ -323,7 +322,6 @@ namespace Google.ProtocolBuffers { ...@@ -323,7 +322,6 @@ namespace Google.ProtocolBuffers {
Assert.IsTrue((merged.HasExtension(UnitTestProtoFile.OptionalInt32Extension))); Assert.IsTrue((merged.HasExtension(UnitTestProtoFile.OptionalInt32Extension)));
Assert.AreEqual(1, (int)merged.GetExtension(UnitTestProtoFile.OptionalInt32Extension)); Assert.AreEqual(1, (int)merged.GetExtension(UnitTestProtoFile.OptionalInt32Extension));
} }
*/
/* Removed multiple files option for the moment /* Removed multiple files option for the moment
[Test] [Test]
......
...@@ -194,6 +194,7 @@ namespace Google.ProtocolBuffers.TestProtos { ...@@ -194,6 +194,7 @@ namespace Google.ProtocolBuffers.TestProtos {
public override Builder MergeFrom(TestMessageSet other) { public override Builder MergeFrom(TestMessageSet other) {
if (other == TestMessageSet.DefaultInstance) return this; if (other == TestMessageSet.DefaultInstance) return this;
this.MergeExtensionFields(other);
this.MergeUnknownFields(other.UnknownFields); this.MergeUnknownFields(other.UnknownFields);
return this; return this;
} }
......
...@@ -4884,6 +4884,7 @@ namespace Google.ProtocolBuffers.TestProtos { ...@@ -4884,6 +4884,7 @@ namespace Google.ProtocolBuffers.TestProtos {
public override Builder MergeFrom(TestAllExtensions other) { public override Builder MergeFrom(TestAllExtensions other) {
if (other == TestAllExtensions.DefaultInstance) return this; if (other == TestAllExtensions.DefaultInstance) return this;
this.MergeExtensionFields(other);
this.MergeUnknownFields(other.UnknownFields); this.MergeUnknownFields(other.UnknownFields);
return this; return this;
} }
...@@ -7597,6 +7598,7 @@ namespace Google.ProtocolBuffers.TestProtos { ...@@ -7597,6 +7598,7 @@ namespace Google.ProtocolBuffers.TestProtos {
public override Builder MergeFrom(TestEmptyMessageWithExtensions other) { public override Builder MergeFrom(TestEmptyMessageWithExtensions other) {
if (other == TestEmptyMessageWithExtensions.DefaultInstance) return this; if (other == TestEmptyMessageWithExtensions.DefaultInstance) return this;
this.MergeExtensionFields(other);
this.MergeUnknownFields(other.UnknownFields); this.MergeUnknownFields(other.UnknownFields);
return this; return this;
} }
...@@ -10714,6 +10716,7 @@ namespace Google.ProtocolBuffers.TestProtos { ...@@ -10714,6 +10716,7 @@ namespace Google.ProtocolBuffers.TestProtos {
if (other.HasMyFloat) { if (other.HasMyFloat) {
MyFloat = other.MyFloat; MyFloat = other.MyFloat;
} }
this.MergeExtensionFields(other);
this.MergeUnknownFields(other.UnknownFields); this.MergeUnknownFields(other.UnknownFields);
return this; return this;
} }
......
...@@ -3936,6 +3936,7 @@ namespace Google.ProtocolBuffers.DescriptorProtos { ...@@ -3936,6 +3936,7 @@ namespace Google.ProtocolBuffers.DescriptorProtos {
if (other.uninterpretedOption_.Count != 0) { if (other.uninterpretedOption_.Count != 0) {
base.AddRange(other.uninterpretedOption_, result.uninterpretedOption_); base.AddRange(other.uninterpretedOption_, result.uninterpretedOption_);
} }
this.MergeExtensionFields(other);
this.MergeUnknownFields(other.UnknownFields); this.MergeUnknownFields(other.UnknownFields);
return this; return this;
} }
...@@ -4273,6 +4274,7 @@ namespace Google.ProtocolBuffers.DescriptorProtos { ...@@ -4273,6 +4274,7 @@ namespace Google.ProtocolBuffers.DescriptorProtos {
if (other.uninterpretedOption_.Count != 0) { if (other.uninterpretedOption_.Count != 0) {
base.AddRange(other.uninterpretedOption_, result.uninterpretedOption_); base.AddRange(other.uninterpretedOption_, result.uninterpretedOption_);
} }
this.MergeExtensionFields(other);
this.MergeUnknownFields(other.UnknownFields); this.MergeUnknownFields(other.UnknownFields);
return this; return this;
} }
...@@ -4565,6 +4567,7 @@ namespace Google.ProtocolBuffers.DescriptorProtos { ...@@ -4565,6 +4567,7 @@ namespace Google.ProtocolBuffers.DescriptorProtos {
if (other.uninterpretedOption_.Count != 0) { if (other.uninterpretedOption_.Count != 0) {
base.AddRange(other.uninterpretedOption_, result.uninterpretedOption_); base.AddRange(other.uninterpretedOption_, result.uninterpretedOption_);
} }
this.MergeExtensionFields(other);
this.MergeUnknownFields(other.UnknownFields); this.MergeUnknownFields(other.UnknownFields);
return this; return this;
} }
...@@ -4839,6 +4842,7 @@ namespace Google.ProtocolBuffers.DescriptorProtos { ...@@ -4839,6 +4842,7 @@ namespace Google.ProtocolBuffers.DescriptorProtos {
if (other.uninterpretedOption_.Count != 0) { if (other.uninterpretedOption_.Count != 0) {
base.AddRange(other.uninterpretedOption_, result.uninterpretedOption_); base.AddRange(other.uninterpretedOption_, result.uninterpretedOption_);
} }
this.MergeExtensionFields(other);
this.MergeUnknownFields(other.UnknownFields); this.MergeUnknownFields(other.UnknownFields);
return this; return this;
} }
...@@ -5063,6 +5067,7 @@ namespace Google.ProtocolBuffers.DescriptorProtos { ...@@ -5063,6 +5067,7 @@ namespace Google.ProtocolBuffers.DescriptorProtos {
if (other.uninterpretedOption_.Count != 0) { if (other.uninterpretedOption_.Count != 0) {
base.AddRange(other.uninterpretedOption_, result.uninterpretedOption_); base.AddRange(other.uninterpretedOption_, result.uninterpretedOption_);
} }
this.MergeExtensionFields(other);
this.MergeUnknownFields(other.UnknownFields); this.MergeUnknownFields(other.UnknownFields);
return this; return this;
} }
...@@ -5287,6 +5292,7 @@ namespace Google.ProtocolBuffers.DescriptorProtos { ...@@ -5287,6 +5292,7 @@ namespace Google.ProtocolBuffers.DescriptorProtos {
if (other.uninterpretedOption_.Count != 0) { if (other.uninterpretedOption_.Count != 0) {
base.AddRange(other.uninterpretedOption_, result.uninterpretedOption_); base.AddRange(other.uninterpretedOption_, result.uninterpretedOption_);
} }
this.MergeExtensionFields(other);
this.MergeUnknownFields(other.UnknownFields); this.MergeUnknownFields(other.UnknownFields);
return this; return this;
} }
...@@ -5511,6 +5517,7 @@ namespace Google.ProtocolBuffers.DescriptorProtos { ...@@ -5511,6 +5517,7 @@ namespace Google.ProtocolBuffers.DescriptorProtos {
if (other.uninterpretedOption_.Count != 0) { if (other.uninterpretedOption_.Count != 0) {
base.AddRange(other.uninterpretedOption_, result.uninterpretedOption_); base.AddRange(other.uninterpretedOption_, result.uninterpretedOption_);
} }
this.MergeExtensionFields(other);
this.MergeUnknownFields(other.UnknownFields); this.MergeUnknownFields(other.UnknownFields);
return this; return this;
} }
......
...@@ -284,15 +284,12 @@ namespace Google.ProtocolBuffers { ...@@ -284,15 +284,12 @@ namespace Google.ProtocolBuffers {
throw new ArgumentException("MergeFrom(IMessage) can only merge messages of the same type."); throw new ArgumentException("MergeFrom(IMessage) can only merge messages of the same type.");
} }
fields.MergeFrom(other); fields.MergeFrom(other);
MergeUnknownFields(other.UnknownFields);
return this; return this;
} }
public override Builder MergeFrom(DynamicMessage other) { public override Builder MergeFrom(DynamicMessage other) {
if (other.DescriptorForType != type) { return MergeFrom((IMessage)other);
throw new ArgumentException("MergeFrom(IMessage) can only merge messages of the same type.");
}
fields.MergeFrom(other);
return this;
} }
public override DynamicMessage Build() { public override DynamicMessage Build() {
......
...@@ -166,5 +166,9 @@ namespace Google.ProtocolBuffers { ...@@ -166,5 +166,9 @@ namespace Google.ProtocolBuffers {
return base.AddRepeatedField(field, value); return base.AddRepeatedField(field, value);
} }
} }
protected void MergeExtensionFields(ExtendableMessage<TMessage, TBuilder> other) {
MessageBeingBuilt.Extensions.MergeFrom(other.Extensions);
}
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment