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 3533b94
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 395 deletions.
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 InvalidContentTypeException(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
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 3533b94

Please sign in to comment.