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 Jul 21, 2023
1 parent 92c91e7 commit 206e18b
Show file tree
Hide file tree
Showing 16 changed files with 293 additions and 402 deletions.
2 changes: 1 addition & 1 deletion extensions/Worker.Extensions.Kafka/release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@

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

- <entry>
- Add `BindingCapabilities` attribute to KafkaTrigger to express function-level retry capabilities. (#1457)
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 @@ -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 @@ -15,10 +15,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 +28,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,8 +82,8 @@ 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>();

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>
5 changes: 2 additions & 3 deletions test/DotNetWorkerTests/GrpcFunctionDefinitionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,9 @@ public void GrpcFunctionDefinition_TableInput_Creates()
{
Assert.Equal("tableInput", q.Name);
Assert.Equal(typeof(TableClient), q.Type);
Assert.Contains(PropertyBagKeys.AllowConverterFallback, q.Properties.Keys);
Assert.Contains(PropertyBagKeys.ConverterFallbackBehavior, q.Properties.Keys);
Assert.Contains(PropertyBagKeys.BindingAttributeSupportedConverters, q.Properties.Keys);
Assert.True(true, q.Properties[PropertyBagKeys.AllowConverterFallback].ToString());
Assert.Equal("Default", q.Properties[PropertyBagKeys.ConverterFallbackBehavior].ToString());
Assert.Contains(new Dictionary<Type, List<Type>>().ToString(), q.Properties[PropertyBagKeys.BindingAttributeSupportedConverters].ToString());
});

Expand All @@ -214,7 +214,6 @@ public void GrpcFunctionDefinition_TableInput_Creates()
});
}


private class MyFunctionClass
{
public HttpResponseData Run(HttpRequestData req)
Expand Down

0 comments on commit 206e18b

Please sign in to comment.