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

Consider using source generators for regular expressions #2138

Closed
virzak opened this issue Aug 8, 2023 · 1 comment
Closed

Consider using source generators for regular expressions #2138

virzak opened this issue Aug 8, 2023 · 1 comment

Comments

@virzak
Copy link
Contributor

virzak commented Aug 8, 2023

Is your feature request related to a problem? Please describe.

The recommended way of using regular expressions is through source generators.

Describe the solution you'd like

This line (and others like it)

private static readonly Regex _keyRegex = new Regex("{([^{}:]+)(?::([^{}]+))?}", RegexOptions.Compiled);

can be rewritten as:

private static readonly Regex _keyRegex = _keyRegexMethod();

[GeneratedRegex("{([^{}:]+)(?::([^{}]+))?}")]
private static partial Regex _keyRegexMethod();

This feature is only available in .NET 7 and above and language version 11+, so NET7_0_OR_GREATER symbol should be used.

Describe alternatives you've considered

No response

Additional Context

Obviously this is very low priority issue, but came across when working on #2136, since this is where we were discussing source generators.

Visual Studio and Rider actually recommend this change when you drop all frameworks other than .NET 7 and upgrade LangVersion.

image

image

I can send a PR if you're interested.

@JeremySkinner
Copy link
Member

JeremySkinner commented Aug 9, 2023

Thanks for the suggestion, this was discussed already in #1907 and the decision was to leave it for now. We'll re-visit once we drop support for platforms older than net7. Using a compiled regex as we currently do allows for compilation on all platforms we support without a need for conditional compilation, which I want to avoid.

Visual Studio and Rider actually recommend this change when you drop all frameworks other than .NET 7 and upgrade LangVersion.

This isn't practical as FluentValidation is used in a huge number of codebases. At an absolute minimum we need to support the LTS release (net6) as well as the current release (net7).

We no longer need to support net5 (as this is out of support by MS), but can't be removed without a major version increase, so will continue to be supported in FluentValidation 11, and dropped from FluentValidation 12.

Netstandard support is more complex as there is no end of support date from Microsoft on these, but the plan is to drop netstandard2 in FluentValidation 12 as I don't want to support legacy .NET Framework anymore. Netstandard2.1 support will continue in FluentValidation 12, and we'll re-evaluate for FluentValidation 13. So v13 is the earliest I'd consider switching to regex source generators.

@JeremySkinner JeremySkinner closed this as not planned Won't fix, can't repro, duplicate, stale Aug 9, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 24, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants