From d3eddf7e2d51624f03fbee65b285ae2fa086217e Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Fri, 29 May 2020 04:31:06 -0400 Subject: [PATCH] make everything build, with some TODOs --- .../LegacyGeneratedCodeTest.cs | 4 ++++ .../src/Google.Protobuf/CodedOutputStream.cs | 23 +++++++++++++++++++ .../Google.Protobuf/Collections/MapField.cs | 11 +++++++-- csharp/src/Google.Protobuf/IBufferMessage.cs | 8 ++++++- 4 files changed, 43 insertions(+), 3 deletions(-) diff --git a/csharp/src/Google.Protobuf.Test/LegacyGeneratedCodeTest.cs b/csharp/src/Google.Protobuf.Test/LegacyGeneratedCodeTest.cs index 9c9846c8f5..4c1218ee15 100644 --- a/csharp/src/Google.Protobuf.Test/LegacyGeneratedCodeTest.cs +++ b/csharp/src/Google.Protobuf.Test/LegacyGeneratedCodeTest.cs @@ -228,6 +228,10 @@ namespace Google.Protobuf } } } + + void pb::IBufferMessage.InternalWriteTo(ref pb::WriteContext output) { + // TODO: implement this, add tests!!! + } } } } \ No newline at end of file diff --git a/csharp/src/Google.Protobuf/CodedOutputStream.cs b/csharp/src/Google.Protobuf/CodedOutputStream.cs index 0916302146..3384a3577d 100644 --- a/csharp/src/Google.Protobuf/CodedOutputStream.cs +++ b/csharp/src/Google.Protobuf/CodedOutputStream.cs @@ -280,6 +280,29 @@ namespace Google.Protobuf } } + /// + /// Writes a message, without a tag, to the stream. + /// Only the message data is written, without a length-delimiter. + /// + /// The value to write + public void WriteRawMessage(IMessage value) + { + // TODO(jtattermusch): if the message doesn't implement IBufferMessage (and thus does not provide the InternalWriteTo method), + // what we're doing here works fine, but could be more efficient. + // For now, this inefficiency is fine, considering this is only a backward-compatibility scenario (and regenerating the code fixes it). + var span = new Span(buffer); + WriteContext.Initialize(ref span, ref state, out WriteContext ctx); + try + { + // TODO: fix fix fix + WritingPrimitivesMessages.WriteMessage(ref ctx, value); + } + finally + { + ctx.CopyStateTo(this); + } + } + /// /// Writes a group, without a tag, to the stream. /// diff --git a/csharp/src/Google.Protobuf/Collections/MapField.cs b/csharp/src/Google.Protobuf/Collections/MapField.cs index cf1e22a068..392e7ea49f 100644 --- a/csharp/src/Google.Protobuf/Collections/MapField.cs +++ b/csharp/src/Google.Protobuf/Collections/MapField.cs @@ -740,8 +740,15 @@ namespace Google.Protobuf.Collections public void WriteTo(CodedOutputStream output) { - codec.keyCodec.WriteTagAndValue(output, Key); - codec.valueCodec.WriteTagAndValue(output, Value); + // Message adapter is an internal class and we know that all the writing will happen via InternalWriteTo. + throw new NotImplementedException(); + } + + [SecuritySafeCritical] + public void InternalWriteTo(ref WriteContext ctx) + { + codec.keyCodec.WriteTagAndValue(ref ctx, Key); + codec.valueCodec.WriteTagAndValue(ref ctx, Value); } public int CalculateSize() diff --git a/csharp/src/Google.Protobuf/IBufferMessage.cs b/csharp/src/Google.Protobuf/IBufferMessage.cs index f6d174b3b7..c99a7d79fc 100644 --- a/csharp/src/Google.Protobuf/IBufferMessage.cs +++ b/csharp/src/Google.Protobuf/IBufferMessage.cs @@ -35,7 +35,7 @@ namespace Google.Protobuf #if GOOGLE_PROTOBUF_SUPPORT_SYSTEM_MEMORY /// /// Interface for a Protocol Buffers message, supporting - /// parsing from . + /// parsing from and writing to . /// public interface IBufferMessage : IMessage { @@ -44,6 +44,12 @@ namespace Google.Protobuf /// Users should never invoke this method directly. /// void InternalMergeFrom(ref ParseContext ctx); + + /// + /// Internal implementation of writing this message to a given write context. + /// Users should never invoke this method directly. + /// + void InternalWriteTo(ref WriteContext ctx); } #endif }