Skip to content

Commit f837c2a

Browse files
committed
Generate switch expression
1 parent bfa6827 commit f837c2a

File tree

1 file changed

+8
-15
lines changed

1 file changed

+8
-15
lines changed

sdk/eventgrid/Azure.Messaging.EventGrid/EventGridSourceGenerator/src/EventGridSourceGenerator.cs

+8-15
Original file line numberDiff line numberDiff line change
@@ -109,28 +109,21 @@ internal class SystemEventExtensions
109109
{{
110110
public static object AsSystemEventData(string eventType, JsonElement data)
111111
{{
112-
if (s_systemEventDeserializers.TryGetValue(eventType, out Func<JsonElement,{(_isSystemEventsLibrary ? " ModelReaderWriterOptions," : string.Empty)} object> systemDeserializationFunction))
112+
return eventType switch
113113
{{
114-
return systemDeserializationFunction(data{(_isSystemEventsLibrary ? ", null" : string.Empty)});
115-
}}
116-
else
117-
{{
118-
return null;
119-
}}
120-
}}
121-
122-
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)
123-
{{
124114
");
125115
foreach (SystemEventNode sysEvent in _visitor.SystemEvents)
126116
{
127117
// Add each an entry for each system event to the dictionary containing a mapping from constant name to deserialization method.
128118
sourceBuilder.AppendLine(
129-
$"{Indent}{Indent}{Indent}{{ SystemEventNames.{sysEvent.EventConstantName}, {sysEvent.EventName}.{sysEvent.DeserializeMethod} }},");
119+
$"{Indent}{Indent}{Indent}{Indent}SystemEventNames.{sysEvent.EventConstantName} => {sysEvent.EventName}.{sysEvent.DeserializeMethod}(data{(_isSystemEventsLibrary ? ", null": string.Empty)}),");
130120
}
131-
sourceBuilder.Append($@"{Indent}{Indent}}};
132-
{Indent}}}
133-
}}");
121+
sourceBuilder.AppendLine($"{Indent}{Indent}{Indent}{Indent}_ => null");
122+
sourceBuilder.AppendLine($"{Indent}{Indent}{Indent}}};");
123+
sourceBuilder.AppendLine($"{Indent}{Indent}}}");
124+
sourceBuilder.AppendLine($"{Indent}}}");
125+
sourceBuilder.AppendLine("}");
126+
134127
return sourceBuilder.ToString();
135128
}
136129
}

0 commit comments

Comments
 (0)