Skip to content

Commit

Permalink
Use RegEx source generators on .NET 7 or later
Browse files Browse the repository at this point in the history
  • Loading branch information
mburumaxwell committed Jun 5, 2024
1 parent 5965199 commit 85575cb
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 29 deletions.
1 change: 1 addition & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<PropertyGroup>
<WarningsAsErrors>$(WaringsAsErrors),IL2026,IL3050</WarningsAsErrors>
<WarningsAsErrors>$(WarningsAsErrors),SYSLIB1045</WarningsAsErrors>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Text.RegularExpressions;
using Tingle.Extensions.PhoneValidators.Airtel;
using Tingle.Extensions.PhoneValidators.Airtel;

namespace System.ComponentModel.DataAnnotations;

Expand All @@ -9,8 +8,6 @@ namespace System.ComponentModel.DataAnnotations;
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
public sealed class AirtelPhoneNumberAttribute : ValidationAttribute
{
private static readonly Regex regex = new(AirtelPhoneNumberValidator.RegExComplete);

/// <summary>
/// Initializes a new instance of the <see cref="SafaricomPhoneNumberAttribute"/> class.
/// </summary>
Expand All @@ -19,6 +16,8 @@ public AirtelPhoneNumberAttribute() : base("The field {0} must be a valid Airtel
/// <inheritdoc/>
public override bool IsValid(object? value)
{
static bool IsValidByRegEx(string value) => AirtelPhoneNumberValidator.Expression.IsMatch(value);

if (value is string s && !string.IsNullOrEmpty(s)) return IsValidByRegEx(s);

if (value is IEnumerable<string> values)
Expand All @@ -32,6 +31,4 @@ public override bool IsValid(object? value)

return true;
}

private bool IsValidByRegEx(string value) => regex.IsMatch(value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Tingle.Extensions.PhoneValidators.Airtel;
/// <summary>
/// Implementation of <see cref="IPhoneNumberValidator"/> specifically for Airtel phone numbers
/// </summary>
public class AirtelPhoneNumberValidator : AbstractPhoneNumberValidator
public partial class AirtelPhoneNumberValidator : AbstractPhoneNumberValidator
{
// This regular expression will match numbers with known formats.
// The intention is to ensure the line number (after country code or local code) are actually standard.
Expand All @@ -16,12 +16,19 @@ public class AirtelPhoneNumberValidator : AbstractPhoneNumberValidator
// The digits are 30-39, 50-56, 85-89 when prefixed with 7 and 00-02 when prefixed with 1
internal const string RegExComplete = @"^(?:254|\+254|0)?((?:(?:7(?:(?:3[0-9])|(?:5[0-6])|(8[5-9])))|(?:1(?:[0][0-2])))[0-9]{6})$";

private static readonly Regex regex = new(@RegExComplete);
internal static readonly Regex Expression = GetExpression();

/// <summary>
/// Creates an instance of <see cref="AirtelPhoneNumberValidator"/>
/// </summary>
public AirtelPhoneNumberValidator() : base() { }

internal override Regex RegularExpression => regex;
internal override Regex RegularExpression => Expression;

#if NET7_0_OR_GREATER
[GeneratedRegex(RegExComplete)]
private static partial Regex GetExpression();
#else
private static Regex GetExpression() => new(RegExComplete);
#endif
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Text.RegularExpressions;
using Tingle.Extensions.PhoneValidators.Safaricom;
using Tingle.Extensions.PhoneValidators.Safaricom;

namespace System.ComponentModel.DataAnnotations;

Expand All @@ -9,8 +8,6 @@ namespace System.ComponentModel.DataAnnotations;
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
public sealed class SafaricomPhoneNumberAttribute : ValidationAttribute
{
private static readonly Regex regex = new(SafaricomPhoneNumberValidator.RegExComplete);

/// <summary>
/// Initializes a new instance of the <see cref="SafaricomPhoneNumberAttribute"/> class.
/// </summary>
Expand All @@ -19,6 +16,8 @@ public SafaricomPhoneNumberAttribute() : base("The field {0} must be a valid Saf
/// <inheritdoc/>
public override bool IsValid(object? value)
{
static bool IsValidByRegEx(string value) => SafaricomPhoneNumberValidator.Expression.IsMatch(value);

if (value is string s && !string.IsNullOrEmpty(s)) return IsValidByRegEx(s);

if (value is IEnumerable<string> values)
Expand All @@ -32,6 +31,4 @@ public override bool IsValid(object? value)

return true;
}

private bool IsValidByRegEx(string value) => regex.IsMatch(value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Tingle.Extensions.PhoneValidators.Safaricom;
/// <summary>
/// Implementation of <see cref="IPhoneNumberValidator"/> specifically for Safaricom phone numbers
/// </summary>
public class SafaricomPhoneNumberValidator : AbstractPhoneNumberValidator
public partial class SafaricomPhoneNumberValidator : AbstractPhoneNumberValidator
{
// This regular expression will match numbers with known formats.
// The intention is to ensure the line number (after country code or local code) are actually standard.
Expand All @@ -16,12 +16,19 @@ public class SafaricomPhoneNumberValidator : AbstractPhoneNumberValidator
// The digits are 00-09, 10-19, 20-29, 40-49, 90-99, 57-59, 68-69 when prefixed with 7 and 10-15 when prefixed with 1
internal const string RegExComplete = @"^(?:254|\+254|0)?((?:(?:7(?:(?:[01249][0-9])|(?:5[789])|(?:6[89])))|(?:1(?:[1][0-5])))[0-9]{6})$";

private static readonly Regex regex = new(@RegExComplete);
internal static readonly Regex Expression = GetExpression();

/// <summary>
/// Creates a instance of <see cref="SafaricomPhoneNumberValidator"/>
/// </summary>
public SafaricomPhoneNumberValidator() : base() { }

internal override Regex RegularExpression => regex;
internal override Regex RegularExpression => Expression;

#if NET7_0_OR_GREATER
[GeneratedRegex(RegExComplete)]
private static partial Regex GetExpression();
#else
private static Regex GetExpression() => new(RegExComplete);
#endif
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Text.RegularExpressions;
using Tingle.Extensions.PhoneValidators.Telkom;
using Tingle.Extensions.PhoneValidators.Telkom;

namespace System.ComponentModel.DataAnnotations;

Expand All @@ -9,8 +8,6 @@ namespace System.ComponentModel.DataAnnotations;
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
public sealed class TelkomPhoneNumberAttribute : ValidationAttribute
{
private static readonly Regex regex = new(TelkomPhoneNumberValidator.RegExComplete);

/// <summary>
/// Initializes a new instance of the <see cref="SafaricomPhoneNumberAttribute"/> class.
/// </summary>
Expand All @@ -19,6 +16,8 @@ public TelkomPhoneNumberAttribute() : base("The field {0} must be a valid Telkom
/// <inheritdoc/>
public override bool IsValid(object? value)
{
static bool IsValidByRegEx(string value) => TelkomPhoneNumberValidator.Expression.IsMatch(value);

if (value is string s && !string.IsNullOrEmpty(s)) return IsValidByRegEx(s);

if (value is IEnumerable<string> values)
Expand All @@ -32,6 +31,4 @@ public override bool IsValid(object? value)

return true;
}

private bool IsValidByRegEx(string value) => regex.IsMatch(value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Tingle.Extensions.PhoneValidators.Telkom;
/// <summary>
/// Implementation of <see cref="IPhoneNumberValidator"/> specifically for Telkom phone numbers
/// </summary>
public class TelkomPhoneNumberValidator : AbstractPhoneNumberValidator
public partial class TelkomPhoneNumberValidator : AbstractPhoneNumberValidator
{
// This regular expression will match numbers with known formats.
// The intention is to ensure the line number (after country code or local code) are actually standard.
Expand All @@ -16,12 +16,19 @@ public class TelkomPhoneNumberValidator : AbstractPhoneNumberValidator
// The digits are 70-79 when prefixed with 7
internal const string RegExComplete = @"^(?:254|\+254|0)?(7(?:(?:7[0-9]))[0-9]{6})$";

private static readonly Regex regex = new(@RegExComplete);
internal static readonly Regex Expression = GetExpression();

/// <summary>
/// Creates an instance of <see cref="TelkomPhoneNumberValidator"/>
/// </summary>
public TelkomPhoneNumberValidator() : base() { }

internal override Regex RegularExpression => regex;
internal override Regex RegularExpression => Expression;

#if NET7_0_OR_GREATER
[GeneratedRegex(RegExComplete)]
private static partial Regex GetExpression();
#else
private static Regex GetExpression() => new(RegExComplete);
#endif
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using MongoDB.Driver;
using System.Text.RegularExpressions;

namespace Tingle.Extensions.Caching.MongoDB.Tests;

public sealed class MongoDbFixture : IDisposable
{
public MongoDbFixture()
{
var dbName = Regex.Replace(Guid.NewGuid().ToString(), "[^a-zA-Z0-9]", "");
var dbName = Guid.NewGuid().ToString("n");
var mub = new MongoUrlBuilder()
{
Server = MongoServerAddress.Parse("localhost:27017"),
Expand Down

0 comments on commit 85575cb

Please sign in to comment.