-
Notifications
You must be signed in to change notification settings - Fork 371
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
base: master
Are you sure you want to change the base?
Changes from all commits
087f11b
435933c
4a4a338
4658d9c
95d92b3
79f53f3
67099be
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -164,6 +164,65 @@ | |
| 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)); | ||
|
Check failure on line 188 in src/Dapr.Actors.Generators/ActorClientGenerator.cs
|
||
| else if (typeParam.HasValueTypeConstraint && !typeParam.HasUnmanagedTypeConstraint) | ||
| { | ||
| constraints.Add(SyntaxFactory.ClassOrStructConstraint(SyntaxKind.StructConstraint)); | ||
| } | ||
|
oising marked this conversation as resolved.
|
||
|
|
||
| // 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"))); | ||
| } | ||
|
Comment on lines
+200
to
+211
|
||
|
|
||
| // 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)) | ||
|
|
@@ -174,6 +233,7 @@ | |
| : 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), | ||
|
|
@@ -192,7 +252,7 @@ | |
| .ToFullString(); | ||
|
|
||
| context.AddSource($"{descriptor.FullyQualifiedTypeName}.g.cs", compilationOutput); | ||
| } | ||
|
Check failure on line 255 in src/Dapr.Actors.Generators/ActorClientGenerator.cs
|
||
| catch (DiagnosticsException e) | ||
| { | ||
| foreach (var diagnostic in e.Diagnostics) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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); | ||
| } | ||
|
Comment on lines
+896
to
+900
|
||
| }"; | ||
|
|
||
| var generatedSource = @"// <auto-generated/> | ||
| #nullable enable | ||
| namespace Test | ||
| { | ||
| public sealed class TestActorClient<TTrait> : Test.ITestActor<TTrait> where TTrait : Test.ITrait | ||
| { | ||
|
Comment on lines
+907
to
+908
|
||
| 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> | ||
| { | ||
|
Comment on lines
+954
to
+958
|
||
| 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); | ||
| } | ||
|
Comment on lines
+982
to
+985
|
||
|
|
||
| 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(); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The condition deciding whether to emit a
where ...clause doesn’t includeHasConstructorConstraint. If a type parameter only hasnew()(no other constraints), no constraint clause will be generated, even though the PR claimsnew()is supported.