Skip to content

Commit 3c4c829

Browse files
authored
Merge pull request #898 from Cysharp/feature/RevertResolverGeneration
Revert to generating resolvers and type-hints.
2 parents 7662b89 + 4914b3b commit 3c4c829

File tree

339 files changed

+4365
-864
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

339 files changed

+4365
-864
lines changed

src/MagicOnion.Client.SourceGenerator/CodeAnalysis/SerializationFormatterNameMapper.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,17 @@ public interface IWellKnownSerializationTypes
2222
public class MessagePackFormatterNameMapper : ISerializationFormatterNameMapper
2323
{
2424
readonly string userDefinedFormatterNamespace;
25+
readonly bool allowToMapUserDefinedFormatter;
2526

2627
public IWellKnownSerializationTypes WellKnownTypes => MessagePackWellKnownSerializationTypes.Instance;
2728

28-
public MessagePackFormatterNameMapper(string userDefinedFormatterNamespace)
29+
public MessagePackFormatterNameMapper(string userDefinedFormatterNamespace, bool allowToMapUserDefinedFormatter)
2930
{
3031
userDefinedFormatterNamespace = string.IsNullOrWhiteSpace(userDefinedFormatterNamespace) ? "MessagePack.Formatters" : userDefinedFormatterNamespace;
3132
if (!userDefinedFormatterNamespace.StartsWith("global::")) userDefinedFormatterNamespace = "global::" + userDefinedFormatterNamespace;
3233

3334
this.userDefinedFormatterNamespace = userDefinedFormatterNamespace;
35+
this.allowToMapUserDefinedFormatter = allowToMapUserDefinedFormatter;
3436
}
3537

3638
public bool TryMapGeneric(MagicOnionTypeInfo type, [NotNullWhen(true)] out string? formatterName, [NotNullWhen(true)] out string? formatterConstructorArgs)
@@ -52,7 +54,7 @@ public bool TryMapGeneric(MagicOnionTypeInfo type, [NotNullWhen(true)] out strin
5254
formatterName = $"{mappedFormatterName}<{genericTypeArgs}>";
5355
formatterConstructorArgs = "()";
5456
}
55-
else
57+
else if (allowToMapUserDefinedFormatter)
5658
{
5759
// User-defined generic types
5860
formatterName = $"{userDefinedFormatterNamespace}{(string.IsNullOrWhiteSpace(userDefinedFormatterNamespace) ? "" : ".")}{type.ToDisplayName(MagicOnionTypeInfo.DisplayNameFormat.Namespace | MagicOnionTypeInfo.DisplayNameFormat.WithoutGenericArguments)}Formatter<{genericTypeArgs}>";

src/MagicOnion.Client.SourceGenerator/CodeGen/MagicOnionInitializerGenerator.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ public static string Build(GenerationContext generationContext, MagicOnionServic
1111

1212
writer.AppendLine($$"""
1313
// <auto-generated />
14-
#pragma warning disable CS0618 // 'member' is obsolete: 'text'
15-
#pragma warning disable CS0612 // 'member' is obsolete
16-
#pragma warning disable CS8019 // Unnecessary using directive.
14+
#pragma warning disable
15+
1716
""");
1817
if (!string.IsNullOrEmpty(generationContext.Namespace))
1918
{

src/MagicOnion.Client.SourceGenerator/CodeGen/MessagePack/MessagePackFormatterResolverGenerator.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace MagicOnion.Client.SourceGenerator.CodeGen.MessagePack;
55

6-
internal class MessagePackFormatterResolverGenerator(bool emitGenericFormatterInstantiationAndTypeHints) : ISerializerFormatterGenerator
6+
internal class MessagePackFormatterResolverGenerator() : ISerializerFormatterGenerator
77
{
88
public (string HintNameSuffix, string Source) Build(GenerationContext generationContext, SerializationFormatterCodeGenContext ctx)
99
{
@@ -49,10 +49,7 @@ partial class {{generationContext.InitializerPartialTypeName}}
4949

5050
EmitResolver(ctx, writer);
5151
EmitGetFormatterHelper(ctx, writer);
52-
if (emitGenericFormatterInstantiationAndTypeHints)
53-
{
54-
EmitTypeHints(ctx, writer);
55-
}
52+
EmitTypeHints(ctx, writer);
5653

5754
writer.AppendLineWithFormat($$"""
5855
}
@@ -97,9 +94,7 @@ static FormatterCache()
9794

9895
void EmitGetFormatterHelper(SerializationFormatterCodeGenContext ctx, StringBuilder writer)
9996
{
100-
var formatterRegistrations = emitGenericFormatterInstantiationAndTypeHints
101-
? ctx.FormatterRegistrations
102-
: ctx.FormatterRegistrations.Where(x => x is EnumSerializationInfo).ToArray();
97+
var formatterRegistrations = ctx.FormatterRegistrations;
10398

10499
writer.AppendLineWithFormat($$"""
105100
static class MessagePackGeneratedGetFormatterHelper

src/MagicOnion.Client.SourceGenerator/CodeGen/StaticMagicOnionClientGenerator.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ static void EmitHeader(GenerationContext generationContext, StringBuilder writer
3838
{
3939
writer.AppendLine("""
4040
// <auto-generated />
41-
#pragma warning disable CS0618 // 'member' is obsolete: 'text'
42-
#pragma warning disable CS0612 // 'member' is obsolete
43-
#pragma warning disable CS8019 // Unnecessary using directive.
41+
#pragma warning disable
4442
4543
""");
4644
}

src/MagicOnion.Client.SourceGenerator/CodeGen/StaticStreamingHubClientGenerator.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,7 @@ static void EmitHeader(GenerationContext generationContext, StringBuilder writer
4141
{
4242
writer.AppendLine("""
4343
// <auto-generated />
44-
#pragma warning disable CS0618 // 'member' is obsolete: 'text'
45-
#pragma warning disable CS0612 // 'member' is obsolete
46-
#pragma warning disable CS0414 // The private field 'field' is assigned but its value is never used
47-
#pragma warning disable CS8019 // Unnecessary using directive.
48-
#pragma warning disable CS1522 // Empty switch block
49-
#pragma warning disable CS1998 // This async method lacks 'await' operators and will run synchronously.
44+
#pragma warning disable
5045
5146
""");
5247
}

src/MagicOnion.Client.SourceGenerator/GenerationContext.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,15 @@ public enum SerializerType
3232
public record GenerationOptions(
3333
SerializerType Serializer,
3434
bool DisableAutoRegistration,
35-
string MessagePackFormatterNamespace,
3635
bool EnableStreamingHubDiagnosticHandler,
37-
string GenerateFileHintNamePrefix
38-
)
36+
string GenerateFileHintNamePrefix,
37+
IReadOnlyDictionary<string, object> AdditionalOptions)
3938
{
4039
public static GenerationOptions Default { get; } = new (
4140
SerializerType.MessagePack,
4241
DisableAutoRegistration: false,
43-
MessagePackFormatterNamespace: "MessagePack.Formatters",
4442
EnableStreamingHubDiagnosticHandler: false,
45-
GenerateFileHintNamePrefix: string.Empty
43+
GenerateFileHintNamePrefix: string.Empty,
44+
AdditionalOptions: new Dictionary<string, object>()
4645
);
4746
}

src/MagicOnion.Client.SourceGenerator/MagicOnionClientSourceGenerator.Emitter.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ public partial class MagicOnionClientSourceGenerator
1313
public const string MagicOnionClientGenerationAttributeName = $"{MagicOnionClientGenerationAttributeShortName}Attribute";
1414
public const string MagicOnionClientGenerationAttributeFullName = $"MagicOnion.Client.{MagicOnionClientGenerationAttributeName}";
1515

16+
public const string MagicOnionClientGenerationOptionAttributeShortName = "MagicOnionClientGenerationOption";
17+
public const string MagicOnionClientGenerationOptionAttributeName = $"{MagicOnionClientGenerationOptionAttributeShortName}Attribute";
18+
public const string MagicOnionClientGenerationOptionAttributeFullName = $"MagicOnion.Client.{MagicOnionClientGenerationOptionAttributeName}";
19+
1620
static class Emitter
1721
{
1822
public static void Emit(GenerationContext context, ImmutableArray<INamedTypeSymbol> interfaceSymbols, ReferenceSymbols referenceSymbols)
@@ -45,8 +49,10 @@ public static void Emit(GenerationContext context, ImmutableArray<INamedTypeSymb
4549
EnumFormatterGenerator: _ => string.Empty
4650
),
4751
SerializerType.MessagePack => (
48-
Mapper: new MessagePackFormatterNameMapper(context.Options.MessagePackFormatterNamespace),
49-
Generator: new MessagePackFormatterResolverGenerator(emitGenericFormatterInstantiationAndTypeHints: false),
52+
Mapper: new MessagePackFormatterNameMapper(
53+
(context.Options.AdditionalOptions.TryGetValue("MessagePack.FormatterNamespace", out var formatterNamespace) ? formatterNamespace as string : null) ?? "MessagePack.Formatters",
54+
context.Options.AdditionalOptions.TryGetValue("MessagePack.GenerateResolverForCustomFormatter", out var generateTypeHintsForCustomFormatter) && generateTypeHintsForCustomFormatter.Equals(true)),
55+
Generator: new MessagePackFormatterResolverGenerator(),
5056
EnumFormatterGenerator: x => MessagePackEnumFormatterGenerator.Build(context, x)
5157
),
5258
_ => throw new NotImplementedException(),

src/MagicOnion.Client.SourceGenerator/MagicOnionClientSourceGenerator.Parser.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,13 @@ static void Traverse(INamespaceOrTypeSymbol rootNamespaceOrTypeSymbol, List<INam
7474
}
7575
}
7676

77-
static GenerationOptions ParseClientGenerationOptions(AttributeData attr)
77+
static GenerationOptions ParseClientGenerationOptions(AttributeData attr, ClassDeclarationSyntax initializerClassDecl, SemanticModel semanticModel)
7878
{
79-
var options = GenerationOptions.Default;
79+
var initializerClassSymbol = semanticModel.GetDeclaredSymbol(initializerClassDecl) ?? throw new InvalidOperationException();
80+
var additionalOptionAttrs = initializerClassSymbol.GetAttributes().Where(x => x.AttributeClass?.Name == MagicOnionClientGenerationOptionAttributeName).ToArray();
81+
82+
var additionalOptions = new Dictionary<string, object>();
83+
var options = GenerationOptions.Default with { AdditionalOptions = additionalOptions };
8084

8185
foreach (var namedArg in attr.NamedArguments)
8286
{
@@ -90,8 +94,8 @@ static GenerationOptions ParseClientGenerationOptions(AttributeData attr)
9094
case nameof(GenerationOptions.Serializer):
9195
options = options with { Serializer = (SerializerType)(int)namedArg.Value.Value! };
9296
break;
93-
case nameof(GenerationOptions.MessagePackFormatterNamespace):
94-
options = options with { MessagePackFormatterNamespace = (string)namedArg.Value.Value! };
97+
case "MessagePackFormatterNamespace": // Backward compatibility
98+
additionalOptions["MessagePack.FormatterNamespace"] = (string)namedArg.Value.Value!;
9599
break;
96100
case nameof(GenerationOptions.EnableStreamingHubDiagnosticHandler):
97101
options = options with { EnableStreamingHubDiagnosticHandler = (bool)namedArg.Value.Value! };
@@ -102,6 +106,14 @@ static GenerationOptions ParseClientGenerationOptions(AttributeData attr)
102106
}
103107
}
104108

109+
if (additionalOptionAttrs.Length > 0)
110+
{
111+
foreach (var additionalOptionAttr in additionalOptionAttrs)
112+
{
113+
additionalOptions[additionalOptionAttr.ConstructorArguments[0].Value!.ToString()!] = additionalOptionAttr.ConstructorArguments[1].Value!;
114+
}
115+
}
116+
105117
return options;
106118
}
107119

src/MagicOnion.Client.SourceGenerator/MagicOnionClientSourceGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
2525
var attr = attrs.FirstOrDefault(x => x.AttributeClass?.Name == MagicOnionClientGenerationAttributeName);
2626
if (attr is null) return; // TODO: ReportDiagnostic
2727

28-
var options = ParseClientGenerationOptions(attr);
28+
var options = ParseClientGenerationOptions(attr, initializerClassDecl, semanticModel);
2929
if (!TryParseClientGenerationSpec(sourceProductionContext, semanticModel, initializerClassDecl, attr, out var spec))
3030
{
3131
return;

src/MagicOnion.Client/MagicOnionClientGenerationAttribute.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace MagicOnion.Client;
44
/// Marker attribute for generating clients of MagicOnion.
55
/// The source generator collects the classes specified by this attribute and uses them to generate source.
66
/// </summary>
7-
[AttributeUsage(global::System.AttributeTargets.Class, AllowMultiple = false)]
7+
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
88
public class MagicOnionClientGenerationAttribute : Attribute
99
{
1010
/// <summary>
@@ -20,6 +20,7 @@ public class MagicOnionClientGenerationAttribute : Attribute
2020
/// <summary>
2121
/// Gets or set the namespace of pre-generated MessagePackFormatters. The default value is <c>MessagePack.Formatters</c>.
2222
/// </summary>
23+
[Obsolete("This property is obsolete. Use [MagicOnionClientGenerationOption(\"MessagePack.FormatterNamespace\", \"MessagePack.Formatters\")] instead.")]
2324
public string MessagePackFormatterNamespace { get; set; } = "MessagePack.Formatters";
2425

2526
/// <summary>
@@ -44,3 +45,25 @@ public enum GenerateSerializerType
4445
MemoryPack = 1,
4546
}
4647
}
48+
49+
/// <summary>
50+
/// Specifies the options for generating clients of MagicOnion.
51+
/// </summary>
52+
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
53+
public class MagicOnionClientGenerationOptionAttribute : Attribute
54+
{
55+
/// <summary>
56+
/// Gets or sets the key of the option.
57+
/// </summary>
58+
public string Key { get; set; }
59+
/// <summary>
60+
/// Gets or sets the value of the option.
61+
/// </summary>
62+
public object? Value { get; set; }
63+
64+
public MagicOnionClientGenerationOptionAttribute(string key, object value)
65+
{
66+
Key = key;
67+
Value = value;
68+
}
69+
}

0 commit comments

Comments
 (0)