Skip to content

Commit

Permalink
Add unknown field support for csharp (#3936)
Browse files Browse the repository at this point in the history
 Add unknown field support for csharp
  • Loading branch information
anandolee committed Dec 13, 2017
1 parent 0a7120a commit bfd254e
Show file tree
Hide file tree
Showing 32 changed files with 3,017 additions and 345 deletions.
6 changes: 5 additions & 1 deletion Makefile.am
Expand Up @@ -121,12 +121,14 @@ csharp_EXTRA_DIST= \
csharp/src/Google.Protobuf.Test/WellKnownTypes/FieldMaskTest.cs \
csharp/src/Google.Protobuf.Test/WellKnownTypes/TimestampTest.cs \
csharp/src/Google.Protobuf.Test/WellKnownTypes/WrappersTest.cs \
csharp/src/Google.Protobuf.Test/UnknownFieldSetTest.cs \
csharp/src/Google.Protobuf.sln \
csharp/src/Google.Protobuf/ByteArray.cs \
csharp/src/Google.Protobuf/ByteString.cs \
csharp/src/Google.Protobuf/CodedInputStream.cs \
csharp/src/Google.Protobuf/CodedOutputStream.ComputeSize.cs \
csharp/src/Google.Protobuf/CodedOutputStream.cs \
csharp/src/Google.Protobuf/Collections/Lists.cs \
csharp/src/Google.Protobuf/Collections/MapField.cs \
csharp/src/Google.Protobuf/Collections/ProtobufEqualityComparers.cs \
csharp/src/Google.Protobuf/Collections/ReadOnlyDictionary.cs \
Expand Down Expand Up @@ -196,7 +198,9 @@ csharp_EXTRA_DIST= \
csharp/src/Google.Protobuf/WellKnownTypes/ValuePartial.cs \
csharp/src/Google.Protobuf/WellKnownTypes/Wrappers.cs \
csharp/src/Google.Protobuf/WellKnownTypes/WrappersPartial.cs \
csharp/src/Google.Protobuf/WireFormat.cs
csharp/src/Google.Protobuf/WireFormat.cs \
csharp/src/Google.Protobuf/UnknownField.cs \
csharp/src/Google.Protobuf/UnknownFieldSet.cs

java_EXTRA_DIST= \
java/README.md \
Expand Down
1 change: 0 additions & 1 deletion conformance/failure_list_csharp.txt
@@ -1,3 +1,2 @@
Recommended.Proto3.JsonInput.BytesFieldBase64Url.JsonOutput
Recommended.Proto3.JsonInput.BytesFieldBase64Url.ProtobufOutput
Required.Proto3.ProtobufInput.UnknownVarint.ProtobufOutput
Expand Up @@ -638,7 +638,7 @@ public void OneofSerialization_DefaultValue()
}

[Test]
public void IgnoreUnknownFields_RealDataStillRead()
public void DiscardUnknownFields_RealDataStillRead()
{
var message = SampleMessages.CreateFullTestAllTypes();
var stream = new MemoryStream();
Expand All @@ -652,16 +652,18 @@ public void IgnoreUnknownFields_RealDataStillRead()

stream.Position = 0;
var parsed = TestAllTypes.Parser.ParseFrom(stream);
Assert.AreEqual(message, parsed);
// TODO(jieluo): Add test back after DiscardUnknownFields is supported
// Assert.AreEqual(message, parsed);
}

[Test]
public void IgnoreUnknownFields_AllTypes()
public void DiscardUnknownFields_AllTypes()
{
// Simple way of ensuring we can skip all kinds of fields.
var data = SampleMessages.CreateFullTestAllTypes().ToByteArray();
var empty = Empty.Parser.ParseFrom(data);
Assert.AreEqual(new Empty(), empty);
// TODO(jieluo): Add test back after DiscardUnknownField is supported.
// Assert.AreEqual(new Empty(), empty);
}

// This was originally seen as a conformance test failure.
Expand Down Expand Up @@ -720,4 +722,4 @@ public void CustomDiagnosticMessage_DirectToTextWriterCall()
Assert.AreEqual("{ \"c\": 31 }", writer.ToString());
}
}
}
}
1 change: 1 addition & 0 deletions csharp/protos/unittest_proto3.proto
Expand Up @@ -377,3 +377,4 @@ service TestService {
message BarRequest {}
message BarResponse {}

message TestEmptyMessage {}
48 changes: 42 additions & 6 deletions csharp/src/AddressBook/Addressbook.cs
Expand Up @@ -49,6 +49,7 @@ public static partial class AddressbookReflection {
/// </summary>
public sealed partial class Person : pb::IMessage<Person> {
private static readonly pb::MessageParser<Person> _parser = new pb::MessageParser<Person>(() => new Person());
private pb::UnknownFieldSet _unknownFields;
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
public static pb::MessageParser<Person> Parser { get { return _parser; } }

Expand Down Expand Up @@ -76,6 +77,7 @@ public sealed partial class Person : pb::IMessage<Person> {
email_ = other.email_;
phones_ = other.phones_.Clone();
LastUpdated = other.lastUpdated_ != null ? other.LastUpdated.Clone() : null;
_unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields);
}

[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
Expand Down Expand Up @@ -158,7 +160,7 @@ public sealed partial class Person : pb::IMessage<Person> {
if (Email != other.Email) return false;
if(!phones_.Equals(other.phones_)) return false;
if (!object.Equals(LastUpdated, other.LastUpdated)) return false;
return true;
return Equals(_unknownFields, other._unknownFields);
}

[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
Expand All @@ -169,6 +171,9 @@ public sealed partial class Person : pb::IMessage<Person> {
if (Email.Length != 0) hash ^= Email.GetHashCode();
hash ^= phones_.GetHashCode();
if (lastUpdated_ != null) hash ^= LastUpdated.GetHashCode();
if (_unknownFields != null) {
hash ^= _unknownFields.GetHashCode();
}
return hash;
}

Expand Down Expand Up @@ -196,6 +201,9 @@ public sealed partial class Person : pb::IMessage<Person> {
output.WriteRawTag(42);
output.WriteMessage(LastUpdated);
}
if (_unknownFields != null) {
_unknownFields.WriteTo(output);
}
}

[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
Expand All @@ -214,6 +222,9 @@ public sealed partial class Person : pb::IMessage<Person> {
if (lastUpdated_ != null) {
size += 1 + pb::CodedOutputStream.ComputeMessageSize(LastUpdated);
}
if (_unknownFields != null) {
size += _unknownFields.CalculateSize();
}
return size;
}

Expand All @@ -238,6 +249,7 @@ public sealed partial class Person : pb::IMessage<Person> {
}
LastUpdated.MergeFrom(other.LastUpdated);
}
_unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields);
}

[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
Expand All @@ -246,7 +258,7 @@ public sealed partial class Person : pb::IMessage<Person> {
while ((tag = input.ReadTag()) != 0) {
switch(tag) {
default:
input.SkipLastField();
_unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input);
break;
case 10: {
Name = input.ReadString();
Expand Down Expand Up @@ -287,6 +299,7 @@ public enum PhoneType {

public sealed partial class PhoneNumber : pb::IMessage<PhoneNumber> {
private static readonly pb::MessageParser<PhoneNumber> _parser = new pb::MessageParser<PhoneNumber>(() => new PhoneNumber());
private pb::UnknownFieldSet _unknownFields;
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
public static pb::MessageParser<PhoneNumber> Parser { get { return _parser; } }

Expand All @@ -311,6 +324,7 @@ public sealed partial class PhoneNumber : pb::IMessage<PhoneNumber> {
public PhoneNumber(PhoneNumber other) : this() {
number_ = other.number_;
type_ = other.type_;
_unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields);
}

[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
Expand Down Expand Up @@ -355,14 +369,17 @@ public sealed partial class PhoneNumber : pb::IMessage<PhoneNumber> {
}
if (Number != other.Number) return false;
if (Type != other.Type) return false;
return true;
return Equals(_unknownFields, other._unknownFields);
}

[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
public override int GetHashCode() {
int hash = 1;
if (Number.Length != 0) hash ^= Number.GetHashCode();
if (Type != 0) hash ^= Type.GetHashCode();
if (_unknownFields != null) {
hash ^= _unknownFields.GetHashCode();
}
return hash;
}

Expand All @@ -381,6 +398,9 @@ public sealed partial class PhoneNumber : pb::IMessage<PhoneNumber> {
output.WriteRawTag(16);
output.WriteEnum((int) Type);
}
if (_unknownFields != null) {
_unknownFields.WriteTo(output);
}
}

[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
Expand All @@ -392,6 +412,9 @@ public sealed partial class PhoneNumber : pb::IMessage<PhoneNumber> {
if (Type != 0) {
size += 1 + pb::CodedOutputStream.ComputeEnumSize((int) Type);
}
if (_unknownFields != null) {
size += _unknownFields.CalculateSize();
}
return size;
}

Expand All @@ -406,6 +429,7 @@ public sealed partial class PhoneNumber : pb::IMessage<PhoneNumber> {
if (other.Type != 0) {
Type = other.Type;
}
_unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields);
}

[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
Expand All @@ -414,7 +438,7 @@ public sealed partial class PhoneNumber : pb::IMessage<PhoneNumber> {
while ((tag = input.ReadTag()) != 0) {
switch(tag) {
default:
input.SkipLastField();
_unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input);
break;
case 10: {
Number = input.ReadString();
Expand All @@ -440,6 +464,7 @@ public sealed partial class PhoneNumber : pb::IMessage<PhoneNumber> {
/// </summary>
public sealed partial class AddressBook : pb::IMessage<AddressBook> {
private static readonly pb::MessageParser<AddressBook> _parser = new pb::MessageParser<AddressBook>(() => new AddressBook());
private pb::UnknownFieldSet _unknownFields;
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
public static pb::MessageParser<AddressBook> Parser { get { return _parser; } }

Expand All @@ -463,6 +488,7 @@ public sealed partial class AddressBook : pb::IMessage<AddressBook> {
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
public AddressBook(AddressBook other) : this() {
people_ = other.people_.Clone();
_unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields);
}

[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
Expand Down Expand Up @@ -494,13 +520,16 @@ public sealed partial class AddressBook : pb::IMessage<AddressBook> {
return true;
}
if(!people_.Equals(other.people_)) return false;
return true;
return Equals(_unknownFields, other._unknownFields);
}

[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
public override int GetHashCode() {
int hash = 1;
hash ^= people_.GetHashCode();
if (_unknownFields != null) {
hash ^= _unknownFields.GetHashCode();
}
return hash;
}

Expand All @@ -512,12 +541,18 @@ public sealed partial class AddressBook : pb::IMessage<AddressBook> {
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
public void WriteTo(pb::CodedOutputStream output) {
people_.WriteTo(output, _repeated_people_codec);
if (_unknownFields != null) {
_unknownFields.WriteTo(output);
}
}

[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
public int CalculateSize() {
int size = 0;
size += people_.CalculateSize(_repeated_people_codec);
if (_unknownFields != null) {
size += _unknownFields.CalculateSize();
}
return size;
}

Expand All @@ -527,6 +562,7 @@ public sealed partial class AddressBook : pb::IMessage<AddressBook> {
return;
}
people_.Add(other.people_);
_unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields);
}

[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
Expand All @@ -535,7 +571,7 @@ public sealed partial class AddressBook : pb::IMessage<AddressBook> {
while ((tag = input.ReadTag()) != 0) {
switch(tag) {
default:
input.SkipLastField();
_unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input);
break;
case 10: {
people_.AddEntriesFrom(input, _repeated_people_codec);
Expand Down

0 comments on commit bfd254e

Please sign in to comment.