Skip to content

Commit

Permalink
Fix the last bug with solution build
Browse files Browse the repository at this point in the history
  • Loading branch information
AArnott committed Apr 22, 2024
1 parent e460028 commit cc9590f
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 12 deletions.
22 changes: 10 additions & 12 deletions src/MessagePack.SourceGenerator/CodeAnalysis/QualifiedTypeName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,24 @@ public record QualifiedTypeName : IComparable<QualifiedTypeName>
public QualifiedTypeName(ITypeSymbol symbol)
{
this.Kind = symbol.TypeKind;
ITypeSymbol symbolToConsider = symbol;
if (symbol is IArrayTypeSymbol arrayType)
{
this.ArrayRank = arrayType.Rank;
this.Namespace = arrayType.ElementType.ContainingNamespace.GetFullNamespaceName();
this.Name = arrayType.ElementType.Name;
symbolToConsider = arrayType.ElementType;
}

this.Name = symbolToConsider.Name;
if (symbolToConsider.ContainingType is not null)
{
this.NestingType = new(symbolToConsider.ContainingType);
}
else
{
this.Name = symbol.Name;
if (symbol.ContainingType is not null)
{
this.NestingType = new(symbol.ContainingType);
}
else
{
this.Namespace = symbol.ContainingNamespace.GetFullNamespaceName();
}
this.Namespace = symbolToConsider.ContainingNamespace.GetFullNamespaceName();
}

this.TypeParameters = CodeAnalysisUtilities.GetTypeParameters(symbol);
this.TypeParameters = CodeAnalysisUtilities.GetTypeParameters(symbolToConsider);
}

/// <inheritdoc cref="QualifiedTypeName(string?, QualifiedTypeName?, TypeKind, string, ImmutableArray{string})"/>
Expand Down
25 changes: 25 additions & 0 deletions tests/MessagePack.SourceGenerator.Tests/GenerationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -503,4 +503,29 @@ internal class C
""";
await VerifyCS.Test.RunDefaultAsync(this.testOutputHelper, testSource);
}

[Fact]
public async Task NestedMessagePackObjects_Array()
{
string testSource = """
using MessagePack;
using System;

namespace A
{
[MessagePackObject]
internal class B
{
[MessagePackObject]
internal class C
{
}

[Key(0)]
internal C[] array;
}
}
""";
await VerifyCS.Test.RunDefaultAsync(this.testOutputHelper, testSource);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// <auto-generated />

#pragma warning disable 618, 612, 414, 168, CS1591, SA1129, SA1309, SA1312, SA1403, SA1649

#pragma warning disable CS8669 // We may leak nullable annotations into generated code.

using MsgPack = global::MessagePack;

namespace MessagePack {
partial class GeneratedMessagePackResolver {
internal partial class A {
internal partial class B {

internal sealed class CFormatter : MsgPack::Formatters.IMessagePackFormatter<global::A.B.C>
{

public void Serialize(ref MsgPack::MessagePackWriter writer, global::A.B.C value, MsgPack::MessagePackSerializerOptions options)
{
if (value == null)
{
writer.WriteNil();
return;
}

writer.WriteArrayHeader(0);
}

public global::A.B.C Deserialize(ref MsgPack::MessagePackReader reader, MsgPack::MessagePackSerializerOptions options)
{
if (reader.TryReadNil())
{
return null;
}

reader.Skip();
return new global::A.B.C();
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// <auto-generated />

#pragma warning disable 618, 612, 414, 168, CS1591, SA1129, SA1309, SA1312, SA1403, SA1649

#pragma warning disable CS8669 // We may leak nullable annotations into generated code.

using MsgPack = global::MessagePack;

namespace MessagePack {
partial class GeneratedMessagePackResolver {
internal partial class A {

internal sealed class BFormatter : MsgPack::Formatters.IMessagePackFormatter<global::A.B>
{

public void Serialize(ref MsgPack::MessagePackWriter writer, global::A.B value, MsgPack::MessagePackSerializerOptions options)
{
if (value == null)
{
writer.WriteNil();
return;
}

MsgPack::IFormatterResolver formatterResolver = options.Resolver;
writer.WriteArrayHeader(1);
MsgPack::FormatterResolverExtensions.GetFormatterWithVerify<global::A.B.C[]>(formatterResolver).Serialize(ref writer, value.array, options);
}

public global::A.B Deserialize(ref MsgPack::MessagePackReader reader, MsgPack::MessagePackSerializerOptions options)
{
if (reader.TryReadNil())
{
return null;
}

options.Security.DepthStep(ref reader);
MsgPack::IFormatterResolver formatterResolver = options.Resolver;
var length = reader.ReadArrayHeader();
var ____result = new global::A.B();

for (int i = 0; i < length; i++)
{
switch (i)
{
case 0:
____result.array = MsgPack::FormatterResolverExtensions.GetFormatterWithVerify<global::A.B.C[]>(formatterResolver).Deserialize(ref reader, options);
break;
default:
reader.Skip();
break;
}
}

reader.Depth--;
return ____result;
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// <auto-generated />

#pragma warning disable 618, 612, 414, 168, CS1591, SA1129, SA1309, SA1312, SA1403, SA1649

using MsgPack = global::MessagePack;

[assembly: MsgPack::Internal.GeneratedAssemblyMessagePackResolverAttribute(typeof(MessagePack.GeneratedMessagePackResolver), 3, 0)]

namespace MessagePack {

/// <summary>A MessagePack resolver that uses generated formatters for types in this assembly.</summary>
partial class GeneratedMessagePackResolver : MsgPack::IFormatterResolver
{
/// <summary>An instance of this resolver that only returns formatters specifically generated for types in this assembly.</summary>
public static readonly MsgPack::IFormatterResolver Instance = new GeneratedMessagePackResolver();

private GeneratedMessagePackResolver()
{
}

public MsgPack::Formatters.IMessagePackFormatter<T> GetFormatter<T>()
{
return FormatterCache<T>.Formatter;
}

private static class FormatterCache<T>
{
internal static readonly MsgPack::Formatters.IMessagePackFormatter<T> Formatter;

static FormatterCache()
{
var f = GeneratedMessagePackResolverGetFormatterHelper.GetFormatter(typeof(T));
if (f != null)
{
Formatter = (MsgPack::Formatters.IMessagePackFormatter<T>)f;
}
}
}

private static class GeneratedMessagePackResolverGetFormatterHelper
{
private static readonly global::System.Collections.Generic.Dictionary<global::System.Type, int> closedTypeLookup = new(3)
{
{ typeof(global::A.B.C[]), 0 },
{ typeof(global::A.B.C), 1 },
{ typeof(global::A.B), 2 },
};

internal static object GetFormatter(global::System.Type t)
{
if (closedTypeLookup.TryGetValue(t, out int closedKey))
{
return closedKey switch
{
0 => new MsgPack::Formatters.ArrayFormatter<global::A.B.C>(),
1 => new global::MessagePack.GeneratedMessagePackResolver.A.B.CFormatter(),
2 => new global::MessagePack.GeneratedMessagePackResolver.A.BFormatter(),
_ => null, // unreachable
};
}

return null;
}
}
}

}

0 comments on commit cc9590f

Please sign in to comment.