Merge extensions correctly

pull/288/head
Jon Skeet 16 years ago
parent 642a8140c8
commit 49fcd4f794
  1. 4
      src/ProtoGen/MessageGenerator.cs
  2. 4
      src/ProtocolBuffers.Test/GeneratedMessageTest.cs
  3. 1
      src/ProtocolBuffers.Test/TestProtos/UnitTestMessageSetProtoFile.cs
  4. 3
      src/ProtocolBuffers.Test/TestProtos/UnitTestProtoFile.cs
  5. 7
      src/ProtocolBuffers/DescriptorProtos/DescriptorProtoFile.cs
  6. 7
      src/ProtocolBuffers/DynamicMessage.cs
  7. 6
      src/ProtocolBuffers/ExtendableBuilder.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();

@ -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]

@ -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;
}

@ -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;
}

@ -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;
}

@ -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() {

@ -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);
}
}
}

Loading…
Cancel
Save