Skip to content

Commit

Permalink
Refactor Tables converter & update tests (#1716, #1723, #1727)
Browse files Browse the repository at this point in the history
  • Loading branch information
liliankasem committed Aug 15, 2023
1 parent 5999be5 commit 5f6baa0
Show file tree
Hide file tree
Showing 21 changed files with 311 additions and 418 deletions.
6 changes: 4 additions & 2 deletions extensions/Worker.Extensions.Kafka/release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
- My change description (#PR/#issue)
-->

### Microsoft.Azure.Functions.Worker.Extensions.Kafka <version>
### Microsoft.Azure.Functions.Worker.Extensions.Kafka 3.9.0

- <entry>
- Add `BindingCapabilities` attribute to KafkaTrigger to express function-level retry capabilities. (#1457)
- Updated Kafka Extension nuget package to most recent version (3.9.0) (#1713)
- Exposed an important scaling parameter `LagThreshold`` to let a user configure their scaling preferences within Kafka Trigger function. (#1713)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
Expand Down Expand Up @@ -61,4 +61,4 @@ public void Configure(string name, BlobStorageBindingOptions options)
options.Credential = _componentFactory.CreateTokenCredential(connectionSection);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@ internal class TablesBindingOptions

internal virtual TableServiceClient CreateClient()
{
if (ConnectionString is null && ServiceUri is null)
if (ConnectionString is null)
{
throw new ArgumentNullException(nameof(ConnectionString) + " " + nameof(ServiceUri));
throw new ArgumentNullException(nameof(ConnectionString));
}

if (ServiceUri is null)
{
throw new ArgumentNullException(nameof(ConnectionString));
}

if (TableServiceClient is not null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ public void Configure(string name, TablesBindingOptions options)
if (!connectionSection.Exists())
{
// Not found
throw new InvalidOperationException($"Tables connection configuration '{name}' does not exist. " +
"Make sure that it is a defined App Setting.");
throw new InvalidOperationException($"Tables connection configuration '{name}' does not exist. Make sure that it is a defined App Setting.");
}

if (!string.IsNullOrWhiteSpace(connectionSection.Value))
Expand Down
7 changes: 0 additions & 7 deletions extensions/Worker.Extensions.Tables/src/Nuget.config

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
using System.Runtime.CompilerServices;
using Microsoft.Azure.Functions.Worker.Extensions.Abstractions;

[assembly: ExtensionInformation("Microsoft.Azure.WebJobs.Extensions.Tables", "1.2.0-beta.1")]
[assembly: ExtensionInformation("Microsoft.Azure.WebJobs.Extensions.Tables", "1.2.0")]
[assembly: InternalsVisibleTo("Microsoft.Azure.Functions.Worker.Extensions.Tests, PublicKey=00240000048000009400000006020000002400005253413100040000010001005148be37ac1d9f58bd40a2e472c9d380d635b6048278f7d47480b08c928858f0f7fe17a6e4ce98da0e7a7f0b8c308aecd9e9b02d7e9680a5b5b75ac7773cec096fbbc64aebd429e77cb5f89a569a79b28e9c76426783f624b6b70327eb37341eb498a2c3918af97c4860db6cdca4732787150841e395a29cfacb959c1fd971c1")]
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")]
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ namespace Microsoft.Azure.Functions.Worker
/// <summary>
/// Attribute used to configure a parameter as the input target for the Azure Storage Tables binding.
/// </summary>
[AllowConverterFallback(true)]
[InputConverter(typeof(TableClientConverter))]
[InputConverter(typeof(TableEntityConverter))]
[InputConverter(typeof(TableEntityEnumerableConverter))]
[ConverterFallbackBehavior(ConverterFallbackBehavior.Default)]
public class TableInputAttribute : InputBindingAttribute
{
/// <summary>Initializes a new instance of the <see cref="TableInputAttribute"/> class.</summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Azure.Data.Tables;
using Microsoft.Azure.Functions.Worker.Converters;
Expand All @@ -15,10 +14,10 @@
namespace Microsoft.Azure.Functions.Worker.Extensions.Tables.TypeConverters
{
/// <summary>
/// Converter to bind Table client parameter.
/// Converter to bind <see cref="TableClient" /> type parameters.
/// </summary>
[SupportsDeferredBinding]
[SupportedConverterType(typeof(TableClient))]
[SupportedTargetType(typeof(TableClient))]
internal class TableClientConverter: TableConverterBase<TableClient>
{
public TableClientConverter(IOptionsSnapshot<TablesBindingOptions> tableOptions, ILogger<TableClientConverter> logger)
Expand All @@ -28,28 +27,23 @@ public TableClientConverter(IOptionsSnapshot<TablesBindingOptions> tableOptions,

public override ValueTask<ConversionResult> ConvertAsync(ConverterContext context)
{
if (!CanConvert(context))
{
return new(ConversionResult.Unhandled());
}
try
{
if (!CanConvert(context))
{
return new(ConversionResult.Unhandled());
}

var modelBindingData = context?.Source as ModelBindingData;
var tableData = GetBindingDataContent(modelBindingData);

var result = ConvertModelBindingData(tableData);

if (result is not null)
{
return new(ConversionResult.Success(result));
}
return new(ConversionResult.Success(result));
}
catch (Exception ex)
{
return new(ConversionResult.Failed(ex));
}

return new(ConversionResult.Unhandled());
}

private TableClient ConvertModelBindingData(TableData content)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Azure.Data.Tables;
using Microsoft.Azure.Functions.Worker.Converters;
Expand All @@ -23,7 +22,7 @@ public TableConverterBase(IOptionsSnapshot<TablesBindingOptions> tableOptions, I
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_tableOptions = tableOptions ?? throw new ArgumentNullException(nameof(tableOptions));
}

protected bool CanConvert(ConverterContext context)
{
if (context is null)
Expand All @@ -36,14 +35,14 @@ protected bool CanConvert(ConverterContext context)
return false;
}

if (!(context.Source is ModelBindingData bindingData))
if (context.Source is not ModelBindingData bindingData)
{
return false;
}

if (bindingData.Source is not Constants.TablesExtensionName)
{
return false;
throw new InvalidBindingSourceException(bindingData.Source, Constants.TablesExtensionName);
}

return true;
Expand All @@ -56,7 +55,7 @@ protected TableData GetBindingDataContent(ModelBindingData? bindingData)
return bindingData?.ContentType switch
{
Constants.JsonContentType => bindingData.Content.ToObjectFromJson<TableData>(),
_ => throw new NotSupportedException($"Unexpected content-type. Currently only '{Constants.JsonContentType}' is supported.")
_ => throw new InvalidContentTypeException(bindingData?.ContentType, Constants.JsonContentType)
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Azure.Data.Tables;
using Microsoft.Azure.Functions.Worker.Converters;
Expand All @@ -15,43 +14,36 @@
namespace Microsoft.Azure.Functions.Worker.Extensions.Tables.TypeConverters
{
/// <summary>
/// Converter to bind Table type parameters.
/// Converter to bind <see cref="TableEntity" /> type parameters.
/// </summary>
[SupportsDeferredBinding]
[SupportedConverterType(typeof(TableEntity))]
[SupportedTargetType(typeof(TableEntity))]
internal class TableEntityConverter : TableConverterBase<TableEntity>
{
public TableEntityConverter(IOptionsSnapshot<TablesBindingOptions> tableOptions, ILogger<TableEntityConverter> logger)
: base(tableOptions, logger)
{
}

public override async ValueTask<ConversionResult> ConvertAsync(ConverterContext context)
{
if (!CanConvert(context))
{
return ConversionResult.Unhandled();
}

try
{
if (!CanConvert(context))
{
return ConversionResult.Unhandled();
}

var modelBindingData = context?.Source as ModelBindingData;
var tableData = GetBindingDataContent(modelBindingData);

var result = await ConvertModelBindingData(tableData);

if (result is not null)
{
return ConversionResult.Success(result);
}

return ConversionResult.Success(result);
}
catch (Exception ex)
{
return ConversionResult.Failed(ex);
}

return ConversionResult.Unhandled();
}

private async Task<TableEntity> ConvertModelBindingData(TableData content)
Expand All @@ -73,7 +65,7 @@ private async Task<TableEntity> ConvertModelBindingData(TableData content)

return await GetTableEntity(content);
}

private async Task<TableEntity> GetTableEntity(TableData content)
{
var tableClient = GetTableClient(content.Connection, content.TableName!);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Azure.Data.Tables;
using Microsoft.Azure.Functions.Worker.Converters;
Expand All @@ -15,10 +16,10 @@
namespace Microsoft.Azure.Functions.Worker.Extensions.Tables.TypeConverters
{
/// <summary>
/// Converter to bind Table type parameters.
/// Converter to bind <see cref="IEnumerable{T}" /> of type <see cref="TableEntity"/> parameters.
/// </summary>
[SupportsDeferredBinding]
[SupportedConverterType(typeof(IEnumerable<TableEntity>))]
[SupportedTargetType(typeof(IEnumerable<TableEntity>))]
internal class TableEntityEnumerableConverter : TableConverterBase<IEnumerable<TableEntity>>
{
public TableEntityEnumerableConverter(IOptionsSnapshot<TablesBindingOptions> tableOptions, ILogger<TableEntityEnumerableConverter> logger)
Expand All @@ -28,30 +29,25 @@ public TableEntityEnumerableConverter(IOptionsSnapshot<TablesBindingOptions> tab

public override async ValueTask<ConversionResult> ConvertAsync(ConverterContext context)
{
if (!CanConvert(context))
{
return ConversionResult.Unhandled();
}
try
{
if (!CanConvert(context))
{
return ConversionResult.Unhandled();
}

var modelBindingData = context?.Source as ModelBindingData;
var tableData = GetBindingDataContent(modelBindingData);

var result = await ConvertModelBindingData(tableData);

if (result is not null)
{
return ConversionResult.Success(result);
}
return ConversionResult.Success(result);
}
catch (Exception ex)
{
return ConversionResult.Failed(ex);
}

return ConversionResult.Unhandled();
}

private async Task<IEnumerable<TableEntity>> ConvertModelBindingData(TableData content)
{
if (string.IsNullOrEmpty(content.TableName))
Expand Down Expand Up @@ -87,21 +83,22 @@ private async Task<IEnumerable<TableEntity>> GetEnumerableTableEntity(TableData
int countRemaining = content.Take;

var entities = tableClient.QueryAsync<TableEntity>(
filter: filter,
maxPerPage: maxPerPage).ConfigureAwait(false);
filter: filter,
maxPerPage: maxPerPage).ConfigureAwait(false);

List<TableEntity> bindingDataContent = new List<TableEntity>();
List<TableEntity> entityList = new();

await foreach (var entity in entities)
{
countRemaining--;
bindingDataContent.Add(entity);
entityList.Add(entity);
if (countRemaining == 0)
{
break;
}
}
return bindingDataContent;

return entityList;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@

<ItemGroup>
<PackageReference Include="Azure.Data.Tables" Version="12.8.0" />
<PackageReference Include="Azure.Identity" Version="1.8.0" />
<PackageReference Include="Microsoft.Extensions.Azure" Version="1.6.3" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="7.0.0" />
<PackageReference Include="System.Linq.Async" Version="6.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion extensions/Worker.Extensions.Timer/release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@

### Microsoft.Azure.Functions.Worker.Extensions.Timer <version>

- <entry>
- Add `BindingCapabilities` attribute to TimerTrigger to express function-level retry capabilities. (#1457)
4 changes: 2 additions & 2 deletions release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

- <entry>

### Microsoft.Azure.Functions.Worker.Core <version> (delete if not updated)
### Microsoft.Azure.Functions.Worker.Core <version>

- <entry>

### Microsoft.Azure.Functions.Worker.Grpc <version> (delete if not updated)
### Microsoft.Azure.Functions.Worker.Grpc <version>

- <entry>
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Azure.Messaging.EventHubs;
using Azure.Data.Tables;
using Azure.Messaging.EventHubs;
using Azure.Messaging.ServiceBus;
using Azure.Storage.Blobs;
using Azure.Storage.Queues.Models;
Expand Down Expand Up @@ -454,7 +454,7 @@ public void TableFunctions_SDKTypeBindings()

AssertDictionary(extensions, new Dictionary<string, string>
{
{ "Microsoft.Azure.WebJobs.Extensions.Tables", "1.2.0-beta.1" },
{ "Microsoft.Azure.WebJobs.Extensions.Tables", "1.2.0" },
});

var tableEntityFunction = functions.Single(p => p.Name == "TableEntityFunction");
Expand Down

0 comments on commit 5f6baa0

Please sign in to comment.