Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ private bool CollectArray(IArrayTypeSymbol array)

var info = GenericSerializationInfo.Create(array, this.options.Generator.Resolver) with
{
Formatter = new QualifiedTypeName("MsgPack::Formatters", null, TypeKind.Class, formatterName, ImmutableArray.Create(elementTypeName.GetQualifiedName())),
Formatter = new QualifiedTypeName("MsgPack::Formatters", null, TypeKind.Class, formatterName, ImmutableArray.Create(elementTypeName.GetQualifiedName(genericStyle: GenericParameterStyle.Arguments))),
};
this.collectedGenericInfo.Add(info);
return true;
Expand Down
6 changes: 6 additions & 0 deletions src/MessagePack.SourceGenerator/Utils/AnalyzerUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ internal static IEnumerable<string> ResolverSymbolToInstanceExpression(SemanticM
}
}

// If the resolver has GeneratedMessagePackResolverAttribute, it has static Instance property
if (r.GetAttributes().Any(x => x.AttributeClass?.Name == GeneratedMessagePackResolverAttributeName && x.AttributeClass?.ContainingNamespace.GetFullNamespaceName() == AttributeNamespace))
{
return $"{r.GetCanonicalTypeFullName()}.Instance";
}

// Fallback to instantiating the resolver, if a constructor is available.
if (r.InstanceConstructors.FirstOrDefault(c => c.Parameters.Length == 0) is IMethodSymbol ctor)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public void CanFindFormatterFromVariousResolvers()

namespace Tests
{
[CompositeResolver(typeof(NativeGuidResolver), typeof(NativeDecimalResolver))]
[CompositeResolver(typeof(GeneratedMessagePackResolver), typeof(NativeGuidResolver), typeof(NativeDecimalResolver))]
internal partial class MyGeneratedCompositeResolver
{
}
Expand Down
4 changes: 4 additions & 0 deletions tests/MessagePack.SourceGenerator.Tests/GenerationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,12 +266,16 @@ public async Task ArrayTypedProperty()
{
string testSource = """
using MessagePack;
using System.Collections.Generic;

[MessagePackObject]
internal class ContainerObject
{
[Key(0)]
internal SubObject[] ArrayOfCustomObjects { get; set; }

[Key(1)]
internal List<SubObject>[] ArrayOfCustomObjectList { get; set; }
}

[MessagePackObject]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ public void Serialize(ref MsgPack::MessagePackWriter writer, global::ContainerOb
}

MsgPack::IFormatterResolver formatterResolver = options.Resolver;
writer.WriteArrayHeader(1);
writer.WriteArrayHeader(2);
MsgPack::FormatterResolverExtensions.GetFormatterWithVerify<global::SubObject[]>(formatterResolver).Serialize(ref writer, value.ArrayOfCustomObjects, options);
MsgPack::FormatterResolverExtensions.GetFormatterWithVerify<global::System.Collections.Generic.List<global::SubObject>[]>(formatterResolver).Serialize(ref writer, value.ArrayOfCustomObjectList, options);
}

public global::ContainerObject Deserialize(ref MsgPack::MessagePackReader reader, MsgPack::MessagePackSerializerOptions options)
Expand All @@ -44,6 +45,9 @@ public void Serialize(ref MsgPack::MessagePackWriter writer, global::ContainerOb
case 0:
____result.ArrayOfCustomObjects = MsgPack::FormatterResolverExtensions.GetFormatterWithVerify<global::SubObject[]>(formatterResolver).Deserialize(ref reader, options);
break;
case 1:
____result.ArrayOfCustomObjectList = MsgPack::FormatterResolverExtensions.GetFormatterWithVerify<global::System.Collections.Generic.List<global::SubObject>[]>(formatterResolver).Deserialize(ref reader, options);
break;
default:
reader.Skip();
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ static FormatterCache()

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

internal static object GetFormatter(global::System.Type t)
Expand All @@ -53,8 +55,10 @@ internal static object GetFormatter(global::System.Type t)
return closedKey switch
{
0 => new MsgPack::Formatters.ArrayFormatter<global::SubObject>(),
1 => new global::MessagePack.GeneratedMessagePackResolver.ContainerObjectFormatter(),
2 => new global::MessagePack.GeneratedMessagePackResolver.SubObjectFormatter(),
1 => new MsgPack::Formatters.ArrayFormatter<global::System.Collections.Generic.List<global::SubObject>>(),
2 => new MsgPack::Formatters.ListFormatter<global::SubObject>(),
3 => new global::MessagePack.GeneratedMessagePackResolver.ContainerObjectFormatter(),
4 => new global::MessagePack.GeneratedMessagePackResolver.SubObjectFormatter(),
_ => null, // unreachable
};
}
Expand Down