Skip to content

Commit

Permalink
Overcome the ref struct limitation for pre-roslyn compilers by intr…
Browse files Browse the repository at this point in the history
…oducing GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE for generated code (#7490)

* enable compatibility mode in codegen

* regenerate protos

* improve readability

* more robust way of figuring out path to old C# compiler

* add recent C# changes
  • Loading branch information
jtattermusch committed Jun 2, 2020
1 parent c0b79c5 commit 7cb5597
Show file tree
Hide file tree
Showing 49 changed files with 12,165 additions and 643 deletions.
11 changes: 11 additions & 0 deletions CHANGES.txt
Expand Up @@ -13,6 +13,17 @@ Unreleased Changes
* Improved the error message when AttributeError is returned from __getattr__
in EnumTypeWrapper.

C#:
* Dropped support for netstandard1.0 (replaced by support for netstandard1.1).
This was required to modernize the parsing stack to use the `Span<byte>`
type internally. (#7351)
* Add `ParseFrom(ReadOnlySequence<byte>)` method to enable GC friendly
parsing with reduced allocations and buffer copies. (#7351)
* Add `GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE` define to make
generated code compatible with old C# compilers (pre-roslyn compilers
from .NET framework and old versions of mono) that do not support
ref structs. (#7490)

2020-05-26 version 3.12.2 (C++/Java/Python/PHP/Objective-C/C#/Ruby/JavaScript)

C++
Expand Down
1 change: 1 addition & 0 deletions Makefile.am
Expand Up @@ -127,6 +127,7 @@ csharp_EXTRA_DIST= \
csharp/src/Google.Protobuf.Test/MessageParsingHelpers.cs \
csharp/src/Google.Protobuf.Test/Proto3OptionalTest.cs \
csharp/src/Google.Protobuf.Test/ReadOnlySequenceFactory.cs \
csharp/src/Google.Protobuf.Test/RefStructCompatibilityTest.cs \
csharp/src/Google.Protobuf.Test/Reflection/CustomOptionsTest.cs \
csharp/src/Google.Protobuf.Test/Reflection/DescriptorDeclarationTest.cs \
csharp/src/Google.Protobuf.Test/Reflection/DescriptorsTest.cs \
Expand Down
92 changes: 89 additions & 3 deletions csharp/src/AddressBook/Addressbook.cs
Expand Up @@ -49,7 +49,11 @@ public static partial class AddressbookReflection {
/// <summary>
/// [START messages]
/// </summary>
public sealed partial class Person : pb::IMessage<Person>, pb::IBufferMessage {
public sealed partial class Person : pb::IMessage<Person>
#if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE
, pb::IBufferMessage
#endif
{
private static readonly pb::MessageParser<Person> _parser = new pb::MessageParser<Person>(() => new Person());
private pb::UnknownFieldSet _unknownFields;
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
Expand Down Expand Up @@ -256,9 +260,44 @@ public sealed partial class Person : pb::IMessage<Person>, pb::IBufferMessage {

[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
public void MergeFrom(pb::CodedInputStream input) {
#if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE
input.ReadRawMessage(this);
#else
uint tag;
while ((tag = input.ReadTag()) != 0) {
switch(tag) {
default:
_unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input);
break;
case 10: {
Name = input.ReadString();
break;
}
case 16: {
Id = input.ReadInt32();
break;
}
case 26: {
Email = input.ReadString();
break;
}
case 34: {
phones_.AddEntriesFrom(input, _repeated_phones_codec);
break;
}
case 42: {
if (lastUpdated_ == null) {
LastUpdated = new global::Google.Protobuf.WellKnownTypes.Timestamp();
}
input.ReadMessage(LastUpdated);
break;
}
}
}
#endif
}

#if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
void pb::IBufferMessage.InternalMergeFrom(ref pb::ParseContext input) {
uint tag;
Expand Down Expand Up @@ -293,6 +332,7 @@ public sealed partial class Person : pb::IMessage<Person>, pb::IBufferMessage {
}
}
}
#endif

#region Nested types
/// <summary>Container for nested types declared in the Person message type.</summary>
Expand All @@ -304,7 +344,11 @@ public enum PhoneType {
[pbr::OriginalName("WORK")] Work = 2,
}

public sealed partial class PhoneNumber : pb::IMessage<PhoneNumber>, pb::IBufferMessage {
public sealed partial class PhoneNumber : pb::IMessage<PhoneNumber>
#if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE
, pb::IBufferMessage
#endif
{
private static readonly pb::MessageParser<PhoneNumber> _parser = new pb::MessageParser<PhoneNumber>(() => new PhoneNumber());
private pb::UnknownFieldSet _unknownFields;
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
Expand Down Expand Up @@ -441,9 +485,29 @@ public sealed partial class PhoneNumber : pb::IMessage<PhoneNumber>, pb::IBuffer

[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
public void MergeFrom(pb::CodedInputStream input) {
#if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE
input.ReadRawMessage(this);
#else
uint tag;
while ((tag = input.ReadTag()) != 0) {
switch(tag) {
default:
_unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input);
break;
case 10: {
Number = input.ReadString();
break;
}
case 16: {
Type = (global::Google.Protobuf.Examples.AddressBook.Person.Types.PhoneType) input.ReadEnum();
break;
}
}
}
#endif
}

#if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
void pb::IBufferMessage.InternalMergeFrom(ref pb::ParseContext input) {
uint tag;
Expand All @@ -463,6 +527,7 @@ public sealed partial class PhoneNumber : pb::IMessage<PhoneNumber>, pb::IBuffer
}
}
}
#endif

}

Expand All @@ -474,7 +539,11 @@ public sealed partial class PhoneNumber : pb::IMessage<PhoneNumber>, pb::IBuffer
/// <summary>
/// Our address book file is just one of these.
/// </summary>
public sealed partial class AddressBook : pb::IMessage<AddressBook>, pb::IBufferMessage {
public sealed partial class AddressBook : pb::IMessage<AddressBook>
#if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE
, pb::IBufferMessage
#endif
{
private static readonly pb::MessageParser<AddressBook> _parser = new pb::MessageParser<AddressBook>(() => new AddressBook());
private pb::UnknownFieldSet _unknownFields;
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
Expand Down Expand Up @@ -579,9 +648,25 @@ public sealed partial class AddressBook : pb::IMessage<AddressBook>, pb::IBuffer

[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
public void MergeFrom(pb::CodedInputStream input) {
#if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE
input.ReadRawMessage(this);
#else
uint tag;
while ((tag = input.ReadTag()) != 0) {
switch(tag) {
default:
_unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input);
break;
case 10: {
people_.AddEntriesFrom(input, _repeated_people_codec);
break;
}
}
}
#endif
}

#if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
void pb::IBufferMessage.InternalMergeFrom(ref pb::ParseContext input) {
uint tag;
Expand All @@ -597,6 +682,7 @@ public sealed partial class AddressBook : pb::IMessage<AddressBook>, pb::IBuffer
}
}
}
#endif

}

Expand Down

0 comments on commit 7cb5597

Please sign in to comment.