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

Make use of the new partial properties #137

Merged
merged 5 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/Nexus.UI/Core/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ public static string EscapeDataString(string catalogId)
private static readonly long[] _nanoseconds = [(long)1e0, (long)1e3, (long)1e6, (long)1e9, (long)60e9, (long)3600e9, (long)86400e9];
private static readonly int[] _quotients = [1000, 1000, 1000, 60, 60, 24, 1];
private static readonly string[] _postFixes = ["ns", "us", "ms", "s", "min", "h", "d"];

// ... except this line
private static readonly Regex _unitStringEvaluator = UnitStringEvaluator();
[GeneratedRegex(@"^\s*([0-9]+)[\s_]*([a-zA-Z]+)\s*$")]
private static partial Regex _unitStringEvaluator { get; }

public static string ToUnitString(this TimeSpan samplePeriod, bool withUnderScore = false)
{
Expand Down Expand Up @@ -334,7 +336,4 @@ private static JsonElement GetJsonObjectFromPath(this JsonElement root, Span<str

return default;
}

[GeneratedRegex(@"^\s*([0-9]+)[\s_]*([a-zA-Z]+)\s*$", RegexOptions.Compiled)]
private static partial Regex UnitStringEvaluator();
}
12 changes: 5 additions & 7 deletions src/Nexus/PackageManagement/PackageController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -488,16 +488,14 @@ private async Task<string[]> DiscoverGithubReleasesAsync(CancellationToken cance
.First()
.Split(",")
.Where(current => current.Contains("rel=\"next\""))
.Select(current => GitHubRegex().Match(current).Groups[1].Value)
.Select(current => GitHubRegex.Match(current).Groups[1].Value)
.FirstOrDefault();

if (requestUrl == default)
break;

continue;
}

return [.. result];
return result.ToArray();
Conundraah marked this conversation as resolved.
Show resolved Hide resolved
}

private async Task<string> RestoreGitHubReleasesAsync(string restoreRoot, CancellationToken cancellationToken)
Expand Down Expand Up @@ -749,7 +747,7 @@ private static async IAsyncEnumerable<JsonElement> GetGitLabPackagesGenericAsync
.First()
.Split(",")
.Where(current => current.Contains("rel=\"next\""))
.Select(current => GitlabRegex().Match(current).Groups[1].Value)
.Select(current => GitlabRegex.Match(current).Groups[1].Value)
.FirstOrDefault();

if (requestUrl == default)
Expand All @@ -760,10 +758,10 @@ private static async IAsyncEnumerable<JsonElement> GetGitLabPackagesGenericAsync
}

[GeneratedRegex("\\<(https:.*)\\>; rel=\"next\"")]
private static partial Regex GitHubRegex();
private static partial Regex GitHubRegex { get; }

[GeneratedRegex("\\<(https:.*)\\>; rel=\"next\"")]
private static partial Regex GitlabRegex();
private static partial Regex GitlabRegex { get; }

#endregion

Expand Down
8 changes: 4 additions & 4 deletions src/Nexus/Utilities/NexusUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static string DefaultBaseUrl

if (aspnetcoreEnvVar is not null)
{
var match = AspNetCoreEnvVarRegex().Match(aspnetcoreEnvVar);
var match = AspNetCoreEnvVarRegex.Match(aspnetcoreEnvVar);

if (match.Success && int.TryParse(match.Groups[1].Value, out var parsedPort))
port = parsedPort;
Expand All @@ -37,6 +37,9 @@ public static string DefaultBaseUrl
}
}

[GeneratedRegex(":([0-9]+)")]
private static partial Regex AspNetCoreEnvVarRegex { get; }

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int Scale(TimeSpan value, TimeSpan samplePeriod) => (int)(value.Ticks / samplePeriod.Ticks);

Expand Down Expand Up @@ -151,7 +154,4 @@ public static IEnumerable<T> GetCustomAttributes<T>(this Type type) where T : At
{
return type.GetCustomAttributes(false).OfType<T>();
}

[GeneratedRegex(":([0-9]+)")]
private static partial Regex AspNetCoreEnvVarRegex();
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,9 @@ public static string ToPath(this Uri url)
private static readonly string[] _postFixes = ["ns", "us", "ms", "s", "min", "h", "d"];

// ... except these lines
private static readonly Regex _unitStringEvaluator = UnitStringEvaluator();

[GeneratedRegex(@"^([0-9]+)_([a-z]+)$")]
private static partial Regex _unitStringEvaluator { get; }

internal const string NEXUS_KEY = "nexus";

Expand Down Expand Up @@ -215,9 +217,6 @@ internal static TimeSpan ToSamplePeriod(string unitString)
return new TimeSpan(ticks);
}

[GeneratedRegex(@"^([0-9]+)_([a-z]+)$", RegexOptions.Compiled)]
private static partial Regex UnitStringEvaluator();

internal static ResourceCatalog EnsureAndSanitizeMandatoryProperties(
this ResourceCatalog catalog,
int pipelinePosition,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ internal static partial class DataModelUtilities
* /a/b/c/T1/600_s_mean(abc=456)#base=1s
*/
// keep in sync with Nexus.UI.Core.Utilities
private static readonly Regex _resourcePathEvaluator = ResourcePathEvaluator();

[GeneratedRegex(@"^(?'catalog'.*)\/(?'resource'.*)\/(?'sample_period'[0-9]+_[a-zA-Z]+)(?:_(?'kind'[^\(#\s]+))?(?:\((?'parameters'.*)\))?(?:#(?'fragment'.*))?$")]
private static partial Regex _resourcePathEvaluator { get; }

private static string ToPascalCase(string input)
{
Expand Down Expand Up @@ -288,7 +290,4 @@ private static void MergeArrays(JsonArray currentArray, JsonElement root1, JsonE
_ => JsonValue.Create(element)
};
}

[GeneratedRegex(@"^(?'catalog'.*)\/(?'resource'.*)\/(?'sample_period'[0-9]+_[a-zA-Z]+)(?:_(?'kind'[^\(#\s]+))?(?:\((?'parameters'.*)\))?(?:#(?'fragment'.*))?$", RegexOptions.Compiled)]
private static partial Regex ResourcePathEvaluator();
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
[DebuggerDisplay("{Id,nq}")]
public partial record Representation
{
private static readonly Regex _snakeCaseEvaluator = SnakeCaseEvaluator();
[GeneratedRegex("(?<=[a-z])([A-Z])")]
private static partial Regex _snakeCaseEvaluator { get; }

private static readonly HashSet<NexusDataType> _nexusDataTypeValues = new(Enum.GetValues<NexusDataType>());

private IReadOnlyDictionary<string, JsonElement>? _parameters;
Expand Down Expand Up @@ -111,7 +113,7 @@
/// The representation kind.
/// </summary>
[JsonIgnore]
#warning Remove this when https://github.com/RicoSuter/NSwag/issues/4681 is solved

Check warning on line 116 in src/extensibility/dotnet-extensibility/DataModel/Representation.cs

View workflow job for this annotation

GitHub Actions / Build

#warning: 'Remove this when https://github.com/RicoSuter/NSwag/issues/4681 is solved'
internal RepresentationKind Kind { get; }

/// <summary>
Expand Down Expand Up @@ -139,7 +141,4 @@
throw new Exception("The representation argument identifier is not valid.");
}
}

[GeneratedRegex("(?<=[a-z])([A-Z])", RegexOptions.Compiled)]
private static partial Regex SnakeCaseEvaluator();
}
6 changes: 2 additions & 4 deletions src/extensibility/dotnet-extensibility/DataModel/Resource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public Resource(
/// <summary>
/// Gets a regular expression to validate a resource identifier.
/// </summary>
public static Regex ValidIdExpression { get; } = ValidExpression();
[GeneratedRegex(@"^[a-zA-Z_][a-zA-Z_0-9]*$")]
public static partial Regex ValidIdExpression { get; }

/// <summary>
/// Gets a regular expression to find invalid characters in a resource identifier.
Expand Down Expand Up @@ -126,7 +127,4 @@ private static void ValidateRepresentations(IReadOnlyList<Representation> repres
if (uniqueIds.Count() != representations.Count)
throw new ArgumentException("There are multiple representations with the same identifier.");
}

[GeneratedRegex(@"^[a-zA-Z_][a-zA-Z_0-9]*$")]
private static partial Regex ValidExpression();
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public ResourceCatalog(
/// <summary>
/// Gets a regular expression to validate a resource catalog identifier.
/// </summary>
public static Regex ValidIdExpression { get; } = ValidIdExpressionRegex();
[GeneratedRegex(@"^(?:\/[a-zA-Z_][a-zA-Z_0-9]*)+$")]
public static partial Regex ValidIdExpression { get; }

private static Regex _matchSingleParametersExpression { get; } = new Regex(@"\s*(.+?)\s*=\s*([^,\)]+)\s*,?", RegexOptions.Compiled);

Expand Down Expand Up @@ -201,7 +202,4 @@ private static void ValidateResources(IReadOnlyList<Resource> resources)
if (uniqueIds.Count() != resources.Count)
throw new ArgumentException("There are multiple resources with the same identifier.");
}

[GeneratedRegex(@"^(?:\/[a-zA-Z_][a-zA-Z_0-9]*)+$", RegexOptions.Compiled)]
private static partial Regex ValidIdExpressionRegex();
}
Loading