Skip to content

Commit

Permalink
Moved generator costants to dedicated class
Browse files Browse the repository at this point in the history
Signed-off-by: Manuel Menegazzo <manuel.menegazzo@outlook.com>
  • Loading branch information
m3nax committed Sep 3, 2024
1 parent 0dd10d8 commit 330a188
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 33 deletions.
61 changes: 28 additions & 33 deletions src/Dapr.Actors.Generators/ActorClientGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
// ------------------------------------------------------------------------

using System.Collections.Immutable;
using System.Text;
using Dapr.Actors.Generators.Diagnostics;
using Dapr.Actors.Generators.Extensions;
using Dapr.Actors.Generators.Helpers;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;

namespace Dapr.Actors.Generators;

Expand All @@ -27,15 +29,7 @@ namespace Dapr.Actors.Generators;
[Generator]
public sealed class ActorClientGenerator : IIncrementalGenerator
{
private const string GeneratorsNamespace = "Dapr.Actors.Generators";

private const string ActorMethodAttributeTypeName = "ActorMethodAttribute";
private const string ActorMethodAttributeFullTypeName = GeneratorsNamespace + "." + ActorMethodAttributeTypeName;

private const string GenerateActorClientAttribute = "GenerateActorClientAttribute";
private const string GenerateActorClientAttributeFullTypeName = GeneratorsNamespace + "." + GenerateActorClientAttribute;

private static string ActorMethodAttributeSourceText(string generatorNamespace)
private static SourceText ActorMethodAttributeSourceText(string generatorNamespace)
{
if (generatorNamespace == null)
{
Expand All @@ -52,16 +46,20 @@ private static string ActorMethodAttributeSourceText(string generatorNamespace)
namespace {generatorNamespace}
{{
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
internal sealed class ActorMethodAttribute : Attribute
internal sealed class {GeneratorConstants.ActorMethodAttributeTypeName} : Attribute
{{
public string? Name {{ get; set; }}
}}
}}";

return source;
return SourceText.From(
SyntaxFactory.ParseCompilationUnit(source)
.NormalizeWhitespace()
.ToFullString(),
Encoding.UTF8);
}

private static string GenerateActorClientAttributeSourceText(string generatorNamespace)
private static SourceText GenerateActorClientAttributeSourceText(string generatorNamespace)
{
if (generatorNamespace == null)
{
Expand All @@ -78,33 +76,44 @@ private static string GenerateActorClientAttributeSourceText(string generatorNam
namespace {generatorNamespace}
{{
[AttributeUsage(AttributeTargets.Interface, AllowMultiple = false, Inherited = false)]
internal sealed class GenerateActorClientAttribute : Attribute
internal sealed class {GeneratorConstants.GenerateActorClientAttributeTypeName} : Attribute
{{
public string? Name {{ get; set; }}
public string? Namespace {{ get; set; }}
}}
}}";

return source;
return SourceText.From(
SyntaxFactory.ParseCompilationUnit(source)
.NormalizeWhitespace()
.ToFullString(),
Encoding.UTF8);
}

/// <inheritdoc />
public void Initialize(IncrementalGeneratorInitializationContext context)
{
// Register the source output that generates the attribute definitions for ActorMethodAttribute and GenerateActorClientAttribute.
context.RegisterPostInitializationOutput(context =>
{
context.AddSource($"{ActorMethodAttributeFullTypeName}.g.cs", ActorMethodAttributeSourceText(GeneratorsNamespace));
context.AddSource($"{GenerateActorClientAttributeFullTypeName}.g.cs", GenerateActorClientAttributeSourceText(GeneratorsNamespace));
context.AddSource(
$"{GeneratorConstants.ActorMethodAttributeFullTypeName}.g.cs",
ActorMethodAttributeSourceText(GeneratorConstants.GeneratorsNamespace));

context.AddSource(
$"{GeneratorConstants.GenerateActorClientAttributeFullTypeName}.g.cs",
GenerateActorClientAttributeSourceText(GeneratorConstants.GeneratorsNamespace));
});

// Register the attribute that triggers the generation of actor clients.
// Register the value provider that triggers the generation of actor clients based on the GenerateActorClientAttribute.
IncrementalValuesProvider<ActorClientDescriptor?> actorClientsToGenerate = context.SyntaxProvider
.ForAttributeWithMetadataName(
GenerateActorClientAttributeFullTypeName,
GeneratorConstants.GenerateActorClientAttributeFullTypeName,
predicate: static (_, _) => true,
transform: static (gasc, cancellationToken) => GetActorClientDescription(gasc, cancellationToken));

// Register the source output that generates the actor clients.
context.RegisterSourceOutput(actorClientsToGenerate, GenerateActorClientCode);
}

Expand Down Expand Up @@ -159,7 +168,7 @@ static void GenerateActorClientCode(SourceProductionContext context, ActorClient

try
{
var actorMethodAttributeSymbol = descriptor.Compilation.GetTypeByMetadataName(ActorMethodAttributeFullTypeName)
var actorMethodAttributeSymbol = descriptor.Compilation.GetTypeByMetadataName(GeneratorConstants.ActorMethodAttributeFullTypeName)
?? throw new InvalidOperationException("Could not find ActorMethodAttribute.");

var cancellationTokenSymbol = descriptor.Compilation.GetTypeByMetadataName("System.Threading.CancellationToken")
Expand Down Expand Up @@ -323,20 +332,6 @@ private static IEnumerable<SyntaxKind> GetSyntaxKinds(Accessibility accessibilit
return syntaxKinds;
}

// TODO: check there is a better way to get the accessibility
private static string GetTextAccessibility(Accessibility accessibility)
{
return accessibility switch
{
Accessibility.Public => "public",
Accessibility.Internal => "internal",
Accessibility.Private => "private",
Accessibility.Protected => "protected",
Accessibility.ProtectedAndInternal => "protected internal",
_ => throw new InvalidOperationException("Unexpected accessibility.")
};
}

private static string GenerateMethodImplementation(
SourceProductionContext context,
IMethodSymbol method,
Expand Down
33 changes: 33 additions & 0 deletions src/Dapr.Actors.Generators/GeneratorConstants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace Dapr.Actors.Generators
{
/// <summary>
/// Constants used by the code generator.
/// </summary>
internal static class GeneratorConstants
{
/// <summary>
/// The namespace used by the generated code.
/// </summary>
public const string GeneratorsNamespace = "Dapr.Actors.Generators";

/// <summary>
/// The name of the attribute used to mark actor interfaces.
/// </summary>
public const string ActorMethodAttributeTypeName = "ActorMethodAttribute";

/// <summary>
/// The full type name of the attribute used to mark actor interfaces.
/// </summary>
public const string ActorMethodAttributeFullTypeName = GeneratorsNamespace + "." + ActorMethodAttributeTypeName;

/// <summary>
/// The name of the attribute used to mark actor interfaces.
/// </summary>
public const string GenerateActorClientAttributeTypeName = "GenerateActorClientAttribute";

/// <summary>
/// The full type name of the attribute used to mark actor interfaces.
/// </summary>
public const string GenerateActorClientAttributeFullTypeName = GeneratorsNamespace + "." + GenerateActorClientAttributeTypeName;
}
}

0 comments on commit 330a188

Please sign in to comment.