Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,14 @@ private bool TryGetAttributeProperties(AttributeData attributeData, Location? at
}
else
{
attrProperties[namedArgument.Key] = namedArgument.Value.Value;
if (TryParseValueByType(namedArgument.Value, out object? argValue))
{
attrProperties[namedArgument.Key] = argValue;
}
else
{
// TODO: Log diagnostic error
Comment thread
satvu marked this conversation as resolved.
}
}
}
}
Expand Down Expand Up @@ -657,14 +664,14 @@ private bool TryGetAttributeProperties(AttributeData attributeData, Location? at
return true;
}

private bool TryLoadConstructorArguments(AttributeData attributeData, IDictionary<string, object?> dict, Location? attribLocation)
private bool TryLoadConstructorArguments(AttributeData attributeData, IDictionary<string, object?> arguments, Location? attributeLocation)
{
IMethodSymbol? attribMethodSymbol = attributeData.AttributeConstructor;

// Check if the attribute constructor has any parameters
if (attribMethodSymbol is null)
{
_context.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.SymbolNotFound, attribLocation, nameof(attribMethodSymbol)));
_context.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.SymbolNotFound, attributeLocation, nameof(attribMethodSymbol)));
return false;
}

Expand All @@ -677,44 +684,51 @@ private bool TryLoadConstructorArguments(AttributeData attributeData, IDictionar

var arg = attributeData.ConstructorArguments[i];

switch (arg.Kind)
if (TryParseValueByType(arg, out object? argValue))
{
case TypedConstantKind.Error:
break;
arguments[argumentName] = argValue;
}
else
{
// TODO: Log diagnostic error
}
}

case TypedConstantKind.Primitive:
dict[argumentName] = arg.Value;
break;
return true;
}

case TypedConstantKind.Enum:
var enumValue = arg.Type!.GetMembers()
.FirstOrDefault(m => m is IFieldSymbol field
&& field.ConstantValue is object value
&& value.Equals(arg.Value));
private bool TryParseValueByType(TypedConstant attributeArg, out object? argValue)
{
argValue = null;

if (enumValue is null)
{
return false;
}
switch (attributeArg.Kind)
{
case TypedConstantKind.Primitive:
argValue = attributeArg.Value;
break;

// we want just the enumValue symbol's name (Admin, Anonymous, Function)
dict[argumentName] = enumValue.Name;
break;
case TypedConstantKind.Enum:
var enumValue = attributeArg.Type!.GetMembers()
.FirstOrDefault(m => m is IFieldSymbol field
&& field.ConstantValue is object value
&& value.Equals(attributeArg.Value));

case TypedConstantKind.Type:
break;
if (enumValue is null)
{
return false;
}

case TypedConstantKind.Array:
var arrayValues = arg.Values.Select(a => a.Value?.ToString()).ToArray();
dict[argumentName] = arrayValues;
break;
// we want just the enumValue symbol's name (ex: Admin, Anonymous, Function)
argValue = enumValue.Name;
break;

default:
break;
}
case TypedConstantKind.Array:
var arrayValues = attributeArg.Values.Select(a => a.Value?.ToString()).ToArray();
argValue = arrayValues;
break;
}

return true;
return argValue is not null;
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion sdk/release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@

### Microsoft.Azure.Functions.Worker.Sdk.Generators <version>

- <entry>
- Parse named arguments by type (#1877)
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public string QueueToBlob(
[Function("BlobToQueueFunction")]
[QueueOutput("queue2")]
public object BlobToQueue(
[BlobTrigger("container2/%file%")] string blob)
[BlobTrigger("container2/%file%", Source = BlobTriggerSource.EventGrid)] string blob)
{
throw new NotImplementedException();
}
Expand Down Expand Up @@ -206,7 +206,7 @@ public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string d
metadataList.Add(Function0);
var Function1RawBindings = new List<string>();
Function1RawBindings.Add(@"{""name"":""$return"",""type"":""Queue"",""direction"":""Out"",""queueName"":""queue2""}");
Function1RawBindings.Add(@"{""name"":""blob"",""type"":""BlobTrigger"",""direction"":""In"",""properties"":{""supportsDeferredBinding"":""True""},""path"":""container2/%file%"",""dataType"":""String""}");
Function1RawBindings.Add(@"{""name"":""blob"",""type"":""BlobTrigger"",""direction"":""In"",""properties"":{""supportsDeferredBinding"":""True""},""path"":""container2/%file%"",""source"":""EventGrid"",""dataType"":""String""}");

var Function1 = new DefaultFunctionMetadata
{
Expand Down