Visitar URL original
Support dynamic-typed members in the source generator and analyzer by 1AbdullahButt · Pull Request #2319 · MessagePack-CSharp/MessagePack-CSharp · GitHub
Skip to content
Open
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 @@ -76,7 +76,7 @@ internal static int GetArity(ITypeSymbol dataType)
{
INamedTypeSymbol namedType => namedType.Arity,
IArrayTypeSymbol arrayType => GetArity(arrayType.ElementType),
ITypeParameterSymbol => 0,
ITypeParameterSymbol or IDynamicTypeSymbol => 0,
_ => throw new NotSupportedException(),
};

Expand All @@ -85,7 +85,7 @@ internal static ImmutableArray<GenericTypeParameterInfo> GetTypeParameters(IType
{
INamedTypeSymbol namedType => namedType.TypeParameters.Select(t => GenericTypeParameterInfo.Create(t, recursionGuard)).ToImmutableArray(),
IArrayTypeSymbol arrayType => GetTypeParameters(arrayType.ElementType, recursionGuard),
ITypeParameterSymbol => ImmutableArray<GenericTypeParameterInfo>.Empty,
ITypeParameterSymbol or IDynamicTypeSymbol => ImmutableArray<GenericTypeParameterInfo>.Empty,
_ => throw new NotSupportedException(),
};

Expand All @@ -94,7 +94,7 @@ internal static ImmutableArray<QualifiedTypeName> GetTypeArguments(ITypeSymbol d
{
INamedTypeSymbol namedType => namedType.TypeArguments.Select(t => QualifiedTypeName.Create(t, recursionGuard)).ToImmutableArray(),
IArrayTypeSymbol arrayType => GetTypeArguments(arrayType.ElementType, recursionGuard),
ITypeParameterSymbol => ImmutableArray<QualifiedTypeName>.Empty,
ITypeParameterSymbol or IDynamicTypeSymbol => ImmutableArray<QualifiedTypeName>.Empty,
_ => throw new NotSupportedException(),
};
}
10 changes: 10 additions & 0 deletions src/MessagePack.SourceGenerator/CodeAnalysis/QualifiedTypeName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ namespace MessagePack.SourceGenerator.CodeAnalysis;
/// </summary>
public abstract record QualifiedTypeName : IComparable<QualifiedTypeName>
{
/// <summary>
/// The name used for <c>dynamic</c>, which is <see cref="object"/> at runtime.
/// </summary>
private static readonly QualifiedNamedTypeName DynamicTypeName = new(TypeKind.Class)
{
Name = "Object",
Container = new NamespaceTypeContainer("System"),
};

/// <summary>
/// Initializes a new instance of the <see cref="QualifiedTypeName"/> class.
/// </summary>
Expand All @@ -23,6 +32,7 @@ public static QualifiedTypeName Create(ITypeSymbol symbol, ImmutableStack<Generi
IArrayTypeSymbol arrayType => new QualifiedArrayTypeName(arrayType),
INamedTypeSymbol namedType => new QualifiedNamedTypeName(namedType, recursionGuard),
ITypeParameterSymbol typeParam => new TypeParameter(typeParam),
IDynamicTypeSymbol => DynamicTypeName,
_ => throw new NotSupportedException(),
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) All contributors. All rights reserved.
// Copyright (c) All contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#pragma warning disable SA1402 // File may only contain a single type
Expand Down Expand Up @@ -48,6 +48,7 @@ public class TypeCollector
"char",
"string",
"object",
"dynamic",
"System.Guid",
"System.TimeSpan",
"System.DateTime",
Expand All @@ -71,6 +72,7 @@ public class TypeCollector
"char[]",
"string[]",
"object[]",
"dynamic[]",
"System.DateTime[]",
"System.ArraySegment<byte>",
"System.Memory<byte>",
Expand Down
22 changes: 22 additions & 0 deletions tests/MessagePack.SourceGenerator.Tests/MsgPack00xAnalyzerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1109,4 +1109,26 @@ public class {|MsgPack003:Foo|}
MarkupOptions = MarkupOptions.UseFirstDescriptor,
}.RunAsync();
}

[Fact]
public async Task DynamicTypedMembers()
{
string input = Preamble + @"
using System.Collections.Generic;

[MessagePackObject(keyAsPropertyName: true)]
public class AnObject
{
public dynamic Value;

public dynamic[] Values { get; set; }

public List<dynamic> ValueList { get; set; }

public Dictionary<string, dynamic> ValueMap { get; set; }
}
";

await VerifyCS.VerifyAnalyzerAsync(input);
}
}