Skip to content

Commit

Permalink
Add ActivityInputConverter (#2708)
Browse files Browse the repository at this point in the history
* Add ActivityInputConverter

* Handle null input for Activity
  • Loading branch information
jviau authored Jan 8, 2024
1 parent e4c7f3d commit 86da162
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 6 deletions.
41 changes: 41 additions & 0 deletions src/Worker.Extensions.DurableTask/ActivityInputConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using System.Threading.Tasks;
using Microsoft.Azure.Functions.Worker.Converters;
using Microsoft.DurableTask.Worker;
using Microsoft.Extensions.Options;

namespace Microsoft.Azure.Functions.Worker.Extensions.DurableTask;

internal class ActivityInputConverter : IInputConverter
{
private readonly DurableTaskWorkerOptions options;

public ActivityInputConverter(IOptions<DurableTaskWorkerOptions> options)
{
this.options = options?.Value ?? throw new ArgumentNullException(nameof(options));
}

public ValueTask<ConversionResult> ConvertAsync(ConverterContext context)
{
if (context is null)
{
throw new ArgumentNullException(nameof(context));
}

if (context.Source is null)
{
return new(ConversionResult.Success(null));
}

if (context.Source is not string source)
{
throw new InvalidOperationException($"Expected converter source to be a string, received {context.Source?.GetType()}.");
}

object? value = this.options.DataConverter.Deserialize(source, context.TargetType);
return new(ConversionResult.Success(value));
}
}
4 changes: 4 additions & 0 deletions src/Worker.Extensions.DurableTask/ActivityTriggerAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

using System;
using System.Diagnostics;
using Microsoft.Azure.Functions.Worker.Converters;
using Microsoft.Azure.Functions.Worker.Extensions.Abstractions;
using Microsoft.Azure.Functions.Worker.Extensions.DurableTask;

namespace Microsoft.Azure.Functions.Worker;

Expand All @@ -12,6 +14,8 @@ namespace Microsoft.Azure.Functions.Worker;
/// </summary>
[AttributeUsage(AttributeTargets.Parameter)]
[DebuggerDisplay("{Activity}")]
[InputConverter(typeof(ActivityInputConverter))]
[ConverterFallbackBehavior(ConverterFallbackBehavior.Disallow)]
public sealed class ActivityTriggerAttribute : TriggerBindingAttribute
{
/// <summary>
Expand Down
4 changes: 4 additions & 0 deletions src/Worker.Extensions.DurableTask/DurableClientAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using Microsoft.Azure.Functions.Worker.Converters;
using Microsoft.Azure.Functions.Worker.Extensions.Abstractions;
using Microsoft.Azure.Functions.Worker.Extensions.DurableTask;

namespace Microsoft.Azure.Functions.Worker;

/// <summary>
/// Azure Functions attribute for binding a function parameter to a Durable Task client object.
/// </summary>
[InputConverter(typeof(DurableTaskClientConverter))]
[ConverterFallbackBehavior(ConverterFallbackBehavior.Disallow)]
public sealed class DurableClientAttribute : InputBindingAttribute
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ public override void Configure(IFunctionsWorkerApplicationBuilder applicationBui

applicationBuilder.Services.Configure<WorkerOptions>(o =>
{
o.InputConverters.RegisterAt<DurableTaskClientConverter>(0);
o.InputConverters.Register<OrchestrationInputConverter>();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public override IDictionary<string, StringValues> Read(
var valueList = new List<string>();
while (reader.Read() && reader.TokenType != JsonTokenType.EndObject)
{
string? propertyName = reader.GetString();
string propertyName = reader.GetString()!;

reader.Read();

Expand All @@ -42,7 +42,7 @@ public override IDictionary<string, StringValues> Read(
{
while (reader.Read() && reader.TokenType != JsonTokenType.EndArray)
{
valueList.Add(reader.GetString());
valueList.Add(reader.GetString()!);
}

values = new StringValues(valueList.ToArray());
Expand All @@ -62,8 +62,7 @@ public override void Write(
{
writer.WriteStartObject();

var headers = (IDictionary<string, StringValues>)value;
foreach (var pair in headers)
foreach (KeyValuePair<string, StringValues> pair in value)
{
if (pair.Value.Count == 1)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Functions.Worker.Core" Version="1.15.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Core" Version="1.16.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Abstractions" Version="1.3.0" />
<PackageReference Include="Microsoft.DurableTask.Client.Grpc" Version="1.1.0" />
<PackageReference Include="Microsoft.DurableTask.Worker.Grpc" Version="1.1.0" />
Expand Down

0 comments on commit 86da162

Please sign in to comment.