Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SourceGen] Add support for overriding binding property name #1130

Merged
merged 5 commits into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sdk/Sdk.Generators/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ internal static class Constants
internal const string FunctionNameType = "Microsoft.Azure.Functions.Worker.FunctionAttribute";
internal const string HttpResponseType = "Microsoft.Azure.Functions.Worker.Http.HttpResponseData";
internal const string EventHubsTriggerType = "Microsoft.Azure.Functions.Worker.EventHubTriggerAttribute";
internal const string BindingPropertyNameAttributeType = "Microsoft.Azure.Functions.Worker.Extensions.Abstractions.BindingPropertyNameAttribute";

// System types
internal const string IEnumerableType = "System.Collections.IEnumerable";
Expand Down
22 changes: 21 additions & 1 deletion sdk/Sdk.Generators/FunctionMetadataProviderGenerator.Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,9 @@ private bool TryLoadConstructorArguments(AttributeData attributeData, IDictionar
// that the constructor names would match the property names
for (int i = 0; i < attributeData.ConstructorArguments.Length; i++)
{
var argumentName = attribMethodSymbol.Parameters[i].Name;
string argumentName = attribMethodSymbol.Parameters[i].Name;
OverrideBindingName(attributeData.AttributeClass!, ref argumentName); // either argumentName will remain unchanged OR be updated to the overridden name at the end of this.

var arg = attributeData.ConstructorArguments[i];

switch (arg.Kind)
Expand All @@ -562,6 +564,24 @@ private bool TryLoadConstructorArguments(AttributeData attributeData, IDictionar
return true;
}

private void OverrideBindingName(INamedTypeSymbol attributeClass, ref string argumentName)
{
var bindingPropertyNameSymbol = Compilation.GetTypeByMetadataName(Constants.BindingPropertyNameAttributeType);

foreach (var prop in attributeClass.GetMembers().Where(a => a is IPropertySymbol))
{
if (String.Equals(prop.Name, argumentName, StringComparison.OrdinalIgnoreCase)) // relies on convention where constructor parameter names match the property their value will be assigned to (JSON serialization is a precedence for this convention)
{
var bindingNameAttrList = prop.GetAttributes().Where(attr => SymbolEqualityComparer.Default.Equals(attr.AttributeClass, bindingPropertyNameSymbol));

if (bindingNameAttrList.SingleOrDefault() is { } bindingNameAttr) // there will only be one BindingAttributeName attribute b/c there can't be duplicate attributes on a piece of syntax
{
argumentName = bindingNameAttr.ConstructorArguments.First().Value!.ToString(); // there is only one constructor argument for this binding attribute (the binding name override)
}
}
}
}

/// <summary>
/// This method verifies that an EventHubsTrigger matches our expectations on cardinality (isBatched property). If isBatched is set to true, the parameter with the
/// attribute must be an enumerable type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string d
name = 'blob',
type = 'BlobTrigger',
direction = 'In',
blobPath = 'container2/%file%',
path = 'container2/%file%',
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test has been changed to reflect that blob triggers should have an arg name of "path", not "blobPath". In line 201 above for the Blob output binding, we still keep the arg name "blobPath".

Blob bindings classess for reference.

dataType = 'String',
};
var Function1binding1JSON = JsonSerializer.Serialize(Function1binding1);
Expand Down