Skip to content

Commit

Permalink
Use source generators for RegEx in.NET 7 or later (#239)
Browse files Browse the repository at this point in the history
  • Loading branch information
mburumaxwell committed Jun 7, 2024
1 parent 68938d4 commit c84e7a5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
1 change: 1 addition & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
</PropertyGroup>

<PropertyGroup>
<WarningsAsErrors>$(WarningsAsErrors),SYSLIB1045</WarningsAsErrors>
<WarningsAsErrors>$(WarningsAsErrors),IL2026,IL2060,IL2091,IL2095,IL3050</WarningsAsErrors>
</PropertyGroup>

Expand Down
14 changes: 11 additions & 3 deletions src/FaluSdk/Extensions/CloudEventExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ namespace CloudNative.CloudEvents;
/// <summary>
/// Extensions for <see cref="CloudEvent"/> relating to <see cref="WebhookEvent"/> and <see cref="WebhookEvent{TObject}"/>
/// </summary>
public static class CloudEventExtensions
public static partial class CloudEventExtensions
{
private static readonly System.Text.RegularExpressions.Regex TypeFormat = new("^io.falu.(.*)$");
private const string TypeFormat = "^io.falu.(.*)$";
private static readonly System.Text.RegularExpressions.Regex typeFormat = GetTypeFormat();

/// <summary>
/// Convert a <see cref="CloudEvent"/> to a <see cref="WebhookEvent{TObject}"/> object.
Expand Down Expand Up @@ -40,7 +41,7 @@ public static class CloudEventExtensions
var type = @event.Type;
if (type is not null)
{
var match = TypeFormat.Match(type);
var match = typeFormat.Match(type);
type = match.Success
? match.Groups[1].Value
: throw new InvalidOperationException($"The '{nameof(@event)}.{nameof(@event.Type)}' value must start with 'io.falu.'");
Expand All @@ -66,4 +67,11 @@ public static class CloudEventExtensions
Live = @event.GetLiveMode() ?? false,
};
}

#if NET7_0_OR_GREATER
[System.Text.RegularExpressions.GeneratedRegex(TypeFormat)]
private static partial System.Text.RegularExpressions.Regex GetTypeFormat();
#else
private static System.Text.RegularExpressions.Regex GetTypeFormat() => new(TypeFormat);
#endif
}

0 comments on commit c84e7a5

Please sign in to comment.