Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/daprclient-docs' into daprclient…
Browse files Browse the repository at this point in the history
…-docs
  • Loading branch information
WhitWaldo committed Dec 10, 2024
2 parents 6a30087 + a5e1506 commit db59897
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ Run the following command to start a workflow.
{{% codetab %}}

```bash
curl -i -X POST http://localhost:3500/v1.0-beta1/workflows/dapr/OrderProcessingWorkflow/start?instanceID=12345678 \
curl -i -X POST http://localhost:3500/v1.0/workflows/dapr/OrderProcessingWorkflow/start?instanceID=12345678 \
-H "Content-Type: application/json" \
-d '{"Name": "Paperclips", "TotalCost": 99.95, "Quantity": 1}'
```
Expand All @@ -99,7 +99,7 @@ curl -i -X POST http://localhost:3500/v1.0-beta1/workflows/dapr/OrderProcessingW
{{% codetab %}}

```powershell
curl -i -X POST http://localhost:3500/v1.0-beta1/workflows/dapr/OrderProcessingWorkflow/start?instanceID=12345678 `
curl -i -X POST http://localhost:3500/v1.0/workflows/dapr/OrderProcessingWorkflow/start?instanceID=12345678 `
-H "Content-Type: application/json" `
-d '{"Name": "Paperclips", "TotalCost": 99.95, "Quantity": 1}'
```
Expand All @@ -117,7 +117,7 @@ If successful, you should see a response like the following:
Send an HTTP request to get the status of the workflow that was started:

```bash
curl -i -X GET http://localhost:3500/v1.0-beta1/workflows/dapr/12345678
curl -i -X GET http://localhost:3500/v1.0/workflows/dapr/12345678
```

The workflow is designed to take several seconds to complete. If the workflow hasn't completed when you issue the HTTP request, you'll see the following JSON response (formatted for readability) with workflow status as `RUNNING`:
Expand Down
27 changes: 27 additions & 0 deletions examples/GeneratedActor/ActorClient/IGenericClientActor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// ------------------------------------------------------------------------
// Copyright 2023 The Dapr Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------

using Dapr.Actors.Generators;

namespace GeneratedActor
{
[GenerateActorClient]
internal interface IGenericClientActor<TGenericType1, TGenericType2>
{
[ActorMethod(Name = "GetState")]
Task<TGenericType1> GetStateAsync(CancellationToken cancellationToken = default);

[ActorMethod(Name = "SetState")]
Task SetStateAsync(TGenericType2 state, CancellationToken cancellationToken = default);
}
}
23 changes: 17 additions & 6 deletions src/Dapr.Actors.Generators/ActorClientGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,23 @@ private static void GenerateActorClientCode(SourceProductionContext context, Act
.Append(SyntaxKind.SealedKeyword)
.Select(sk => SyntaxFactory.Token(sk));

var actorClientClassDeclaration = SyntaxFactory.ClassDeclaration(descriptor.ClientTypeName)
.WithModifiers(SyntaxFactory.TokenList(actorClientClassModifiers))
.WithMembers(SyntaxFactory.List(actorMembers))
.WithBaseList(SyntaxFactory.BaseList(
SyntaxFactory.Token(SyntaxKind.ColonToken),
SyntaxFactory.SeparatedList<BaseTypeSyntax>(new[] { actorClientBaseInterface })));
var actorClientClassTypeParameters = descriptor.InterfaceType.TypeParameters
.Select(x => SyntaxFactory.TypeParameter(x.ToString()));

var actorClientClassDeclaration = (actorClientClassTypeParameters.Count() == 0)
? SyntaxFactory.ClassDeclaration(descriptor.ClientTypeName)
.WithModifiers(SyntaxFactory.TokenList(actorClientClassModifiers))
.WithMembers(SyntaxFactory.List(actorMembers))
.WithBaseList(SyntaxFactory.BaseList(
SyntaxFactory.Token(SyntaxKind.ColonToken),
SyntaxFactory.SeparatedList<BaseTypeSyntax>(new[] { actorClientBaseInterface })))
: SyntaxFactory.ClassDeclaration(descriptor.ClientTypeName)
.WithModifiers(SyntaxFactory.TokenList(actorClientClassModifiers))
.WithTypeParameterList(SyntaxFactory.TypeParameterList(SyntaxFactory.SeparatedList(actorClientClassTypeParameters)))
.WithMembers(SyntaxFactory.List(actorMembers))
.WithBaseList(SyntaxFactory.BaseList(
SyntaxFactory.Token(SyntaxKind.ColonToken),
SyntaxFactory.SeparatedList<BaseTypeSyntax>(new[] { actorClientBaseInterface })));

var namespaceDeclaration = SyntaxFactory.NamespaceDeclaration(SyntaxFactory.ParseName(descriptor.NamespaceName))
.WithMembers(SyntaxFactory.List<MemberDeclarationSyntax>(new[] { actorClientClassDeclaration }))
Expand Down
172 changes: 172 additions & 0 deletions test/Dapr.Actors.Generators.Test/ActorClientGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,92 @@ public System.Threading.Tasks.Task TestMethod()
await CreateTest(originalSource, "Test.TestActorClient.g.cs", generatedSource).RunAsync();
}

[Fact]
public async Task TestSingleGenericInternalInterface()
{
var originalSource = @"
using Dapr.Actors.Generators;
using System.Threading.Tasks;
namespace Test
{
[GenerateActorClient]
internal interface ITestActor<TGenericType>
{
Task TestMethod();
}
}";

var generatedSource = @"// <auto-generated/>
#nullable enable
namespace Test
{
internal sealed class TestActorClient<TGenericType> : Test.ITestActor<TGenericType>
{
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 TestMethod()
{
return this.actorProxy.InvokeMethodAsync(""TestMethod"");
}
}
}";

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

[Fact]
public async Task TestMultipleGenericsInternalInterface()
{
var originalSource = @"
using Dapr.Actors.Generators;
using System.Threading.Tasks;
namespace Test
{
[GenerateActorClient]
internal interface ITestActor<TGenericType1, TGenericType2>
{
Task TestMethod();
}
}";

var generatedSource = @"// <auto-generated/>
#nullable enable
namespace Test
{
internal sealed class TestActorClient<TGenericType1, TGenericType2> : Test.ITestActor<TGenericType1, TGenericType2>
{
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 TestMethod()
{
return this.actorProxy.InvokeMethodAsync(""TestMethod"");
}
}
}";

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

[Fact]
public async Task TestRenamedClient()
{
Expand Down Expand Up @@ -211,6 +297,92 @@ public System.Threading.Tasks.Task TestMethod()
await CreateTest(originalSource, "Test.MyTestActorClient.g.cs", generatedSource).RunAsync();
}

[Fact]
public async Task TestSingleGenericRenamedClient()
{
var originalSource = @"
using Dapr.Actors.Generators;
using System.Threading.Tasks;
namespace Test
{
[GenerateActorClient(Name = ""MyTestActorClient"")]
internal interface ITestActor<TGenericType>
{
Task TestMethod();
}
}";

var generatedSource = @"// <auto-generated/>
#nullable enable
namespace Test
{
internal sealed class MyTestActorClient<TGenericType> : Test.ITestActor<TGenericType>
{
private readonly Dapr.Actors.Client.ActorProxy actorProxy;
public MyTestActorClient(Dapr.Actors.Client.ActorProxy actorProxy)
{
if (actorProxy is null)
{
throw new System.ArgumentNullException(nameof(actorProxy));
}
this.actorProxy = actorProxy;
}
public System.Threading.Tasks.Task TestMethod()
{
return this.actorProxy.InvokeMethodAsync(""TestMethod"");
}
}
}";

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

[Fact]
public async Task TestMultipleGenericsRenamedClient()
{
var originalSource = @"
using Dapr.Actors.Generators;
using System.Threading.Tasks;
namespace Test
{
[GenerateActorClient(Name = ""MyTestActorClient"")]
internal interface ITestActor<TGenericType1, TGenericType2>
{
Task TestMethod();
}
}";

var generatedSource = @"// <auto-generated/>
#nullable enable
namespace Test
{
internal sealed class MyTestActorClient<TGenericType1, TGenericType2> : Test.ITestActor<TGenericType1, TGenericType2>
{
private readonly Dapr.Actors.Client.ActorProxy actorProxy;
public MyTestActorClient(Dapr.Actors.Client.ActorProxy actorProxy)
{
if (actorProxy is null)
{
throw new System.ArgumentNullException(nameof(actorProxy));
}
this.actorProxy = actorProxy;
}
public System.Threading.Tasks.Task TestMethod()
{
return this.actorProxy.InvokeMethodAsync(""TestMethod"");
}
}
}";

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

[Fact]
public async Task TestCustomNamespace()
{
Expand Down

0 comments on commit db59897

Please sign in to comment.