Skip to content
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

GUID Pattern support in RegexMatcher #699

Merged
merged 4 commits into from
Dec 11, 2021
Merged

Conversation

brogdogg
Copy link
Contributor

@brogdogg brogdogg commented Dec 8, 2021

Adding a new RegexGuid class, extending the System.Text.RegularExpressions.Regex class, to support patterns for identifying a GUID with respect to the way the System.Guid.ToString() formats a new GUID.

Additionally, the RegexMatcher was updated to use this new RegexGuid class.

Potential use:

// Create a regex matcher with a \GUIDD pattern (i.e. only match on all caps GUID with format D
var matcher = new RegexMatcher("\GUIDD");
// Would not match, since there are lower case
double result = matcher.IsMatch("5ae7dbb5-ea2c-4c55-ac38-9634681f168d");
// Would match, as it is a valid GUID in all upper case and valid with respect o the
// format D specifer.
result = matcher.IsMatch("5AE7DBB5-EA2C-4C55-AC38-9634681F168D");

This is a suggested change to address feedback to #685

@brogdogg
Copy link
Contributor Author

brogdogg commented Dec 8, 2021

Status check shows a failed unit test. The test that failed I did not touch and when I run all tests locally I am unable to recreate the failure.

@StefH
Copy link
Collaborator

StefH commented Dec 8, 2021

Sometimes that test fails... I did update that test just now

I did rerun your CI build and it is ok

Maybe you can merge latest master into your PR branch?

Copy link
Collaborator

@StefH StefH left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot.

1] take a look at my comments

2] I did not check all regex, but is there also a simple GUID match which allows any casing? Like AAAAa1ff-2f1e-4f44-a053-e2c34a7ccBBB?

3] Maybe it's good to replace all existing Regex usages in WireMock with this new one.

4] I'm thinking if we need to make using this new Regex configurable via the wiremock settings maybe?

/// Token for a GUID formated with `B` format specifier with lower case
/// values.
/// </summary>
public const string GuidBLowerToken = @"\guidb";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An easier and (smaller code) approach on this would be to define a dictionary with the tokens and regex.
Like:

private static readonly IDictionary<string,string> _replacements = new Dictionary<string, string>
{
 { @"\guidb",  @"(\{[a-z0-9]{8}-([a-z0-9]{4}-){3}[a-z0-9]{12}\})" }, // Regular expression pattern associated with the expected format for `B` format specifier with lower case.
 // . . .
}

using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace WireMock.RegularExpressions
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

General: you can just write comments / code on 1 line, no need to break at 80 characters.

/// <param name="pattern">
/// Pattern to replace token for.
/// </param>
private static string ReplaceGuidPattern(string pattern)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When using the dictionary, this can be simpler.
And a null check should be added.

private static string ReplaceGuidPattern(string pattern)
{
    Check.NotNull(pattern, nameof(pattern));

     foreach (var replacement in _replacements)
     {
         pattern = pattern.Replace(replacement.Key, replacement.Value);
     }
}

@brogdogg
Copy link
Contributor Author

brogdogg commented Dec 9, 2021

2] I did not check all regex, but is there also a simple GUID match which allows any casing? Like AAAAa1ff-2f1e-4f44-a053-e2c34a7ccBBB?

I thought about this, but since it is a Regex the ignore case operator could be used, wouldn't you think?

3] Maybe it's good to replace all existing Regex usages in WireMock with this new one.

Are you requesting I change all references to the RegexGuid class?

4] I'm thinking if we need to make using this new Regex configurable via the wiremock settings maybe?

I'm not sure what you mean here, would you be able to point to a sample? (Sorry, I'm still new to Wiremock and have only used JSON files for configuring the dotnet tool).

@StefH
Copy link
Collaborator

StefH commented Dec 9, 2021

2]
You are correct

3 and 4]
Maybe better to rename RegexGuid to RegexExtended because in the future. more replacements can be added.

And maybe add an property like bool UseExtendedRegex to the settings:

And the default value is set to true, but if needed, a user can set this to false.

@StefH
Copy link
Collaborator

StefH commented Dec 10, 2021

@brogdogg
If you have time, can apply my comments?
Except for 3 and 4, I can also take that up once your branch is merged into master.

@brogdogg
Copy link
Contributor Author

@brogdogg If you have time, can apply my comments? Except for 3 and 4, I can also take that up once your branch is merged into master.

Hey @StefH yeah, I was planning to address this weekend. Current job is calling for heavy hours right now. Sorry, but will look into it.

@brogdogg brogdogg requested a review from StefH December 11, 2021 09:00
@StefH StefH merged commit 4a434b5 into WireMock-Net:master Dec 11, 2021
@brogdogg brogdogg deleted the feature/685 branch December 13, 2021 02:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants