Skip to content

Commit

Permalink
Add attribute to converter context
Browse files Browse the repository at this point in the history
  • Loading branch information
liliankasem committed Jun 22, 2023
1 parent 80a2659 commit fd61026
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public async ValueTask<FunctionInputBindingResult> BindFunctionInputAsync(Functi
// Return the cached value if BindFunctionInputAsync is called a second time during invocation.
return _inputBindingResult!;
}

IFunctionBindingsFeature functionBindings = context.GetBindings();
var inputBindingCache = context.InstanceServices.GetService<IBindingCache<ConversionResult>>();
var inputConversionFeature = context.Features.Get<IInputConversionFeature>();
Expand Down Expand Up @@ -72,25 +72,12 @@ public async ValueTask<FunctionInputBindingResult> BindFunctionInputAsync(Functi
{
var properties = new Dictionary<string, object>();

// Pass info about specific input converter type defined for this parameter, if present.
if (param.Properties.TryGetValue(PropertyBagKeys.ConverterType, out var converterTypeAssemblyFullName))
{
properties.Add(PropertyBagKeys.ConverterType, converterTypeAssemblyFullName);
}

// Pass info about the flag to allow fallback to default converters defined for this parameter, if present.
if (param.Properties.TryGetValue(PropertyBagKeys.AllowConverterFallback, out var flag))
{
properties.Add(PropertyBagKeys.AllowConverterFallback, flag);
}

// Pass info about input converter types defined for this parameter, if present.
if (param.Properties.TryGetValue(PropertyBagKeys.BindingAttributeSupportedConverters, out var converters))
{
properties.Add(PropertyBagKeys.BindingAttributeSupportedConverters, converters);
}

var converterContext = _converterContextFactory.Create(param.Type, source, context, properties.Count() != 0
AddFunctionParameterPropertyIfPresent(properties, param, PropertyBagKeys.ConverterType);
AddFunctionParameterPropertyIfPresent(properties, param, PropertyBagKeys.AllowConverterFallback);
AddFunctionParameterPropertyIfPresent(properties, param, PropertyBagKeys.BindingAttributeSupportedConverters);
AddFunctionParameterPropertyIfPresent(properties, param, PropertyBagKeys.BindingAttribute);

var converterContext = _converterContextFactory.Create(param.Type, source, context, properties.Count() != 0
? properties.ToImmutableDictionary()
: ImmutableDictionary<string, object>.Empty);

Expand Down Expand Up @@ -129,6 +116,14 @@ public async ValueTask<FunctionInputBindingResult> BindFunctionInputAsync(Functi
}
}

private void AddFunctionParameterPropertyIfPresent(IDictionary<string, object> properties, FunctionParameter param, string key)
{
if (param.Properties.TryGetValue(key, out object val))
{
properties.Add(key, val);
}
}

public void Dispose()
{
if (_disposed)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace Microsoft.Azure.Functions.Worker.Converters
internal static class PropertyBagKeys
{
internal const string ConverterType = "converterType";
internal const string BindingAttribute = "bindingAttribute";
internal const string AllowConverterFallback = "allowConverterFallback";
internal const string BindingAttributeSupportedConverters = "bindingAttributeSupportedConverters";
}
Expand Down

0 comments on commit fd61026

Please sign in to comment.