Skip to content

Commit

Permalink
Merge pull request MessagePack-CSharp#1288 from AArnott/fix1274
Browse files Browse the repository at this point in the history
Add support for C# 9 record types in mpc
  • Loading branch information
AArnott authored Jul 17, 2021
2 parents 66abae0 + 4ff278b commit 3704099
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 8 deletions.
59 changes: 58 additions & 1 deletion sandbox/TestData2/Generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ internal static class GeneratedResolverGetFormatterHelper

static GeneratedResolverGetFormatterHelper()
{
lookup = new global::System.Collections.Generic.Dictionary<Type, int>(13)
lookup = new global::System.Collections.Generic.Dictionary<Type, int>(14)
{
{ typeof(global::System.Collections.Generic.List<global::TestData2.A>), 0 },
{ typeof(global::System.Collections.Generic.List<global::TestData2.B>), 1 },
Expand All @@ -64,6 +64,7 @@ static GeneratedResolverGetFormatterHelper()
{ typeof(global::TestData2.Nest2.IdType), 10 },
{ typeof(global::TestData2.PropNameCheck1), 11 },
{ typeof(global::TestData2.PropNameCheck2), 12 },
{ typeof(global::TestData2.Record), 13 },
};
}

Expand All @@ -90,6 +91,7 @@ internal static object GetFormatter(Type t)
case 10: return new MessagePack.Formatters.TestData2.Nest2_IdTypeFormatter();
case 11: return new MessagePack.Formatters.TestData2.PropNameCheck1Formatter();
case 12: return new MessagePack.Formatters.TestData2.PropNameCheck2Formatter();
case 13: return new MessagePack.Formatters.TestData2.RecordFormatter();
default: return null;
}
}
Expand Down Expand Up @@ -711,5 +713,60 @@ public void Serialize(ref global::MessagePack.MessagePackWriter writer, global::
return ____result;
}
}

public sealed class RecordFormatter : global::MessagePack.Formatters.IMessagePackFormatter<global::TestData2.Record>
{
// SomeProperty
private static global::System.ReadOnlySpan<byte> GetSpan_SomeProperty() => new byte[1 + 12] { 172, 83, 111, 109, 101, 80, 114, 111, 112, 101, 114, 116, 121 };

public void Serialize(ref global::MessagePack.MessagePackWriter writer, global::TestData2.Record value, global::MessagePack.MessagePackSerializerOptions options)
{
if (value is null)
{
writer.WriteNil();
return;
}

var formatterResolver = options.Resolver;
writer.WriteMapHeader(1);
writer.WriteRaw(GetSpan_SomeProperty());
formatterResolver.GetFormatterWithVerify<string>().Serialize(ref writer, value.SomeProperty, options);
}

public global::TestData2.Record Deserialize(ref global::MessagePack.MessagePackReader reader, global::MessagePack.MessagePackSerializerOptions options)
{
if (reader.TryReadNil())
{
return null;
}

options.Security.DepthStep(ref reader);
var formatterResolver = options.Resolver;
var length = reader.ReadMapHeader();
var __SomeProperty__ = default(string);

for (int i = 0; i < length; i++)
{
var stringKey = global::MessagePack.Internal.CodeGenHelpers.ReadStringSpan(ref reader);
switch (stringKey.Length)
{
default:
FAIL:
reader.Skip();
continue;
case 12:
if (!global::System.MemoryExtensions.SequenceEqual(stringKey, GetSpan_SomeProperty().Slice(1))) { goto FAIL; }

__SomeProperty__ = formatterResolver.GetFormatterWithVerify<string>().Deserialize(ref reader, options);
continue;

}
}

var ____result = new global::TestData2.Record(__SomeProperty__);
reader.Depth--;
return ____result;
}
}
}

10 changes: 10 additions & 0 deletions sandbox/TestData2/Record.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (c) All contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using MessagePack;

namespace TestData2
{
[MessagePackObject]
public record Record([property: Key("SomeProperty")] string SomeProperty);
}
2 changes: 2 additions & 0 deletions sandbox/TestData2/TestData2.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net461</TargetFramework>
<LangVersion>9</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MessagePack" Version="2.1.90" />
<PackageReference Include="IsExternalInit" Version="1.0.1" />
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.3" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
</ItemGroup>
Expand Down
8 changes: 4 additions & 4 deletions src/MessagePack.Generator/MessagePack.Generator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@

<ItemGroup>
<PackageReference Include="ConsoleAppFramework" Version="2.0.0" />
<PackageReference Include="Microsoft.Build.Locator" Version="1.2.6" />
<PackageReference Include="Microsoft.Build.Framework" Version="16.0.461" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.Build" Version="16.0.461" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="3.6.0" />
<PackageReference Include="Microsoft.Build.Locator" Version="1.4.1" />
<PackageReference Include="Microsoft.Build.Framework" Version="16.5.0" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.Build" Version="16.5.0" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="3.10.0" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 3 additions & 1 deletion src/MessagePack.GeneratorCore/CodeAnalysis/TypeCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,9 @@ public class TypeCollector
private readonly HashSet<string> externalIgnoreTypeNames;

// visitor workspace:
private readonly HashSet<ITypeSymbol> alreadyCollected = new();
#pragma warning disable RS1024 // Compare symbols correctly (https://github.com/dotnet/roslyn-analyzers/issues/5246)
private readonly HashSet<ITypeSymbol> alreadyCollected = new(SymbolEqualityComparer.Default);
#pragma warning restore RS1024 // Compare symbols correctly
private readonly List<ObjectSerializationInfo> collectedObjectInfo = new();
private readonly List<EnumSerializationInfo> collectedEnumInfo = new();
private readonly List<GenericSerializationInfo> collectedGenericInfo = new();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.6.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.10.0" />
<PackageReference Include="Microsoft.Build" Version="15.9.20" />
<PackageReference Include="System.CodeDom" Version="4.7.0" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.6.0" />

<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />

Expand Down

0 comments on commit 3704099

Please sign in to comment.