Skip to content

[EventGrid] Generate switch expressions instead of dictionary lookups #49426

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

Merged
merged 3 commits into from
Apr 16, 2025
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 @@ -109,28 +109,21 @@ internal class SystemEventExtensions
{{
public static object AsSystemEventData(string eventType, JsonElement data)
{{
if (s_systemEventDeserializers.TryGetValue(eventType, out Func<JsonElement,{(_isSystemEventsLibrary ? " ModelReaderWriterOptions," : string.Empty)} object> systemDeserializationFunction))
{{
return systemDeserializationFunction(data{(_isSystemEventsLibrary ? ", null" : string.Empty)});
}}
else
{{
return null;
}}
}}

internal static readonly IReadOnlyDictionary<string, Func<JsonElement,{(_isSystemEventsLibrary ? " ModelReaderWriterOptions," : string.Empty)} object>> s_systemEventDeserializers = new Dictionary<string, Func<JsonElement,{(_isSystemEventsLibrary ? " ModelReaderWriterOptions," : string.Empty)} object>>(StringComparer.OrdinalIgnoreCase)
{{
var eventTypeSpan = eventType.AsSpan();
");
foreach (SystemEventNode sysEvent in _visitor.SystemEvents)
{
// Add each an entry for each system event to the dictionary containing a mapping from constant name to deserialization method.
sourceBuilder.AppendLine(
$"{Indent}{Indent}{Indent}{{ SystemEventNames.{sysEvent.EventConstantName}, {sysEvent.EventName}.{sysEvent.DeserializeMethod} }},");
$"{Indent}{Indent}{Indent}if (eventTypeSpan.Equals(SystemEventNames.{sysEvent.EventConstantName}.AsSpan(), StringComparison.OrdinalIgnoreCase))");
sourceBuilder.AppendLine(
$"{Indent}{Indent}{Indent}{Indent}return {sysEvent.EventName}.{sysEvent.DeserializeMethod}(data{(_isSystemEventsLibrary ? ", null" : string.Empty)});");
}
sourceBuilder.Append($@"{Indent}{Indent}}};
{Indent}}}
}}");
sourceBuilder.AppendLine($"{Indent}{Indent}{Indent}return null;");
sourceBuilder.AppendLine($"{Indent}{Indent}}}");
sourceBuilder.AppendLine($"{Indent}}}");
sourceBuilder.AppendLine("}");

return sourceBuilder.ToString();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text.Json;
using Azure.Messaging.EventGrid.SystemEvents;
using NUnit.Framework;

Expand Down Expand Up @@ -39,10 +40,9 @@ public void MappingContainsAllSystemEvents()
}

ValidateName(systemEvent.Name);
Assert.IsTrue(
SystemEventExtensions.s_systemEventDeserializers.Values.Any(
f => f.Method.ReturnType == systemEvent), systemEvent.Name);
Assert.IsNotNull(SystemEventExtensions.AsSystemEventData(systemEvent.Name, JsonDocument.Parse("{}").RootElement));
}
Assert.IsNull(SystemEventExtensions.AsSystemEventData("DoesNotExist", JsonDocument.Parse("{}").RootElement));
}

[Test]
Expand Down