Skip to content

Add support for generic constraints to Actor source generator #1535

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
57 changes: 57 additions & 0 deletions src/Dapr.Actors.Generators/ActorClientGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,62 @@ private static void GenerateActorClientCode(SourceProductionContext context, Act
var actorClientClassTypeParameters = descriptor.InterfaceType.TypeParameters
.Select(x => SyntaxFactory.TypeParameter(x.ToString()));

// Create constraint clauses for type parameters
var constraintClauses = new List<TypeParameterConstraintClauseSyntax>();

// For each type parameter, create constraint clauses based on the interface's constraints
foreach (var typeParam in descriptor.InterfaceType.TypeParameters)
{
if (typeParam.HasReferenceTypeConstraint ||
typeParam.HasValueTypeConstraint ||
typeParam.HasUnmanagedTypeConstraint ||
typeParam.HasNotNullConstraint ||
typeParam.ConstraintTypes.Length > 0)
{
var constraints = new List<TypeParameterConstraintSyntax>();

// Add class/struct constraints
if (typeParam.HasReferenceTypeConstraint)
{
constraints.Add(SyntaxFactory.ClassOrStructConstraint(SyntaxKind.ClassConstraint));
}
else if (typeParam.HasValueTypeConstraint)
{
constraints.Add(SyntaxFactory.ClassOrStructConstraint(SyntaxKind.StructConstraint));
}

// Add unmanaged constraint
if (typeParam.HasUnmanagedTypeConstraint)
{
constraints.Add(SyntaxFactory.TypeConstraint(SyntaxFactory.IdentifierName("unmanaged")));
}

// Add type constraints (e.g., where T : IInterface)
foreach (var constraintType in typeParam.ConstraintTypes)
{
constraints.Add(SyntaxFactory.TypeConstraint(
SyntaxFactory.ParseTypeName(constraintType.ToString())));
}

// Add notnull constraint
if (typeParam.HasNotNullConstraint)
{
constraints.Add(SyntaxFactory.TypeConstraint(SyntaxFactory.IdentifierName("notnull")));
}

// Add new() constraint - must be last
if (typeParam.HasConstructorConstraint)
{
constraints.Add(SyntaxFactory.ConstructorConstraint());
}

constraintClauses.Add(
SyntaxFactory.TypeParameterConstraintClause(
SyntaxFactory.IdentifierName(typeParam.Name),
SyntaxFactory.SeparatedList(constraints)));
}
}

var actorClientClassDeclaration = (actorClientClassTypeParameters.Count() == 0)
? SyntaxFactory.ClassDeclaration(descriptor.ClientTypeName)
.WithModifiers(SyntaxFactory.TokenList(actorClientClassModifiers))
Expand All @@ -174,6 +230,7 @@ private static void GenerateActorClientCode(SourceProductionContext context, Act
: SyntaxFactory.ClassDeclaration(descriptor.ClientTypeName)
.WithModifiers(SyntaxFactory.TokenList(actorClientClassModifiers))
.WithTypeParameterList(SyntaxFactory.TypeParameterList(SyntaxFactory.SeparatedList(actorClientClassTypeParameters)))
.WithConstraintClauses(SyntaxFactory.List(constraintClauses)) // Add constraint clauses to the class
.WithMembers(SyntaxFactory.List(actorMembers))
.WithBaseList(SyntaxFactory.BaseList(
SyntaxFactory.Token(SyntaxKind.ColonToken),
Expand Down
116 changes: 116 additions & 0 deletions test/Dapr.Actors.Generators.Test/ActorClientGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -877,4 +877,120 @@ public interface ITestActor

await test.RunAsync();
}

[Fact]
public async Task TestGenericWithConstraints()
{
var originalSource = @"
using Dapr.Actors.Generators;
using System.Threading.Tasks;

namespace Test
{
public interface ITrait
{
string GetName();
}

[GenerateActorClient]
public interface ITestActor<in TTrait> where TTrait : ITrait
{
Task<bool> SetTrait(TTrait trait);
Task<ITrait> GetTrait(string name);
}
}";

var generatedSource = @"// <auto-generated/>
#nullable enable
namespace Test
{
public sealed class TestActorClient<TTrait> : Test.ITestActor<TTrait> where TTrait : Test.ITrait
{
private readonly Dapr.Actors.Client.ActorProxy actorProxy;
public TestActorClient(Dapr.Actors.Client.ActorProxy actorProxy)
{
if (actorProxy is null)
{
throw new System.ArgumentNullException(nameof(actorProxy));
}

this.actorProxy = actorProxy;
}

public System.Threading.Tasks.Task<Test.ITrait> GetTrait(string name)
{
return this.actorProxy.InvokeMethodAsync<string, Test.ITrait>(""GetTrait"", name);
}

public System.Threading.Tasks.Task<bool> SetTrait(TTrait trait)
{
return this.actorProxy.InvokeMethodAsync<TTrait, bool>(""SetTrait"", trait);
}
}
}";

await CreateTest(originalSource, "Test.TestActorClient.g.cs", generatedSource).RunAsync();
}

[Fact]
public async Task TestGenericWithMultipleTypeParametersAndConstraints()
{
var originalSource = @"
using Dapr.Actors.Generators;
using System.Threading.Tasks;

namespace Test
{
public interface ITrait
{
string GetName();
}

public interface IValidator<T>
{
bool Validate(T item);
}

[GenerateActorClient]
public interface ITestActor<TTrait, TValidator>
where TTrait : ITrait, new()
where TValidator : class, IValidator<TTrait>
{
Task<bool> SetTrait(TTrait trait);
Task<TTrait> GetValidatedTrait(string name);
}
}";

var generatedSource = @"// <auto-generated/>
#nullable enable
namespace Test
{
public sealed class TestActorClient<TTrait, TValidator> : Test.ITestActor<TTrait, TValidator> where TTrait : Test.ITrait, new()
where TValidator : class, Test.IValidator<TTrait>
{
private readonly Dapr.Actors.Client.ActorProxy actorProxy;
public TestActorClient(Dapr.Actors.Client.ActorProxy actorProxy)
{
if (actorProxy is null)
{
throw new System.ArgumentNullException(nameof(actorProxy));
}

this.actorProxy = actorProxy;
}

public System.Threading.Tasks.Task<TTrait> GetValidatedTrait(string name)
{
return this.actorProxy.InvokeMethodAsync<string, TTrait>(""GetValidatedTrait"", name);
}

public System.Threading.Tasks.Task<bool> SetTrait(TTrait trait)
{
return this.actorProxy.InvokeMethodAsync<TTrait, bool>(""SetTrait"", trait);
}
}
}";

await CreateTest(originalSource, "Test.TestActorClient.g.cs", generatedSource).RunAsync();
}
}
Loading