Skip to content

Commit

Permalink
Add binding attribute to converter context (#1660)
Browse files Browse the repository at this point in the history
  • Loading branch information
liliankasem committed Jul 11, 2023
1 parent 50c7010 commit 4fcb8d8
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public async ValueTask<FunctionInputBindingResult> BindFunctionInputAsync(Functi
AddFunctionParameterPropertyIfPresent(properties, param, PropertyBagKeys.ConverterType);
AddFunctionParameterPropertyIfPresent(properties, param, PropertyBagKeys.ConverterFallbackBehavior);
AddFunctionParameterPropertyIfPresent(properties, param, PropertyBagKeys.BindingAttributeSupportedConverters);
AddFunctionParameterPropertyIfPresent(properties, param, PropertyBagKeys.BindingAttribute);

var converterContext = _converterContextFactory.Create(param.Type, source, context, properties.Count() != 0
? properties.ToImmutableDictionary()
Expand Down Expand Up @@ -117,7 +118,7 @@ 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))
if (param.Properties.TryGetValue(key, out object? val))
{
properties.Add(key, val);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;

namespace Microsoft.Azure.Functions.Worker.Converters
{
/// <summary>
/// Provides extension methods to work with an <see cref="ConverterContext"/> instance.
/// </summary>
public static class ConverterContextExtensions
{
/// <summary>
/// Tries to retrieve the binding attribute from the <see cref="ConverterContext"/>.
/// </summary>
/// <param name="context">The converter context.</param>
/// <param name="bindingAttribute">When this method returns, contains the binding attribute if found; otherwise, <c>null</c>.</param>
/// <returns><c>true</c> if the binding attribute is found in the converter context; otherwise, <c>false</c>.</returns>
public static bool TryGetBindingAttribute<T>(this ConverterContext context, out object? bindingAttribute) where T : Attribute
=> context.Properties.TryGetValue(PropertyBagKeys.BindingAttribute, out bindingAttribute);

/// <summary>
/// Tries to retrieve the binding attribute from the <see cref="ConverterContext"/>.
/// </summary>
/// <param name="context">The converter context.</param>
/// <param name="type">Binding attribute type.</param>
/// <param name="bindingAttribute">When this method returns, contains the binding attribute if found; otherwise, <c>null</c>.</param>
/// <returns><c>true</c> if the binding attribute is found in the converter context; otherwise, <c>false</c>.</returns>
public static bool TryGetBindingAttribute(this ConverterContext context, Type type, out object? bindingAttribute)
{
if (type is null)
{
throw new ArgumentNullException(nameof(type));
}

if (!typeof(Attribute).IsAssignableFrom(type))
{
throw new ArgumentException($"The type '{type}' must be an attribute.", nameof(type));
}

return context.Properties.TryGetValue(PropertyBagKeys.BindingAttribute, out bindingAttribute);
}
}
}
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 ConverterFallbackBehavior = "converterFallbackBehavior";
internal const string BindingAttributeSupportedConverters = "bindingAttributeSupportedConverters";
}
Expand Down
2 changes: 2 additions & 0 deletions src/DotNetWorker.Grpc/Definition/GrpcFunctionDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ public GrpcFunctionDefinition(FunctionLoadRequest loadRequest, IMethodInfoLocato
var output = new Dictionary<string, object>();
bool isInputConverterAttributeAdvertised = false;

output.Add(PropertyBagKeys.BindingAttribute, bindingAttribute);

// ConverterTypesDictionary will be "object" part of the return value - ImmutableDictionary<string, object>
// The dictionary has key of type IInputConverter and value as List of Types supported by the converter.
var converterTypesDictionary = new Dictionary<Type, List<Type>>();
Expand Down

0 comments on commit 4fcb8d8

Please sign in to comment.