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

feat: Use *IgnoreCase() extension methods #2892

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace VirtoCommerce.Platform.Redis
{
public class RedisPlatformMemoryCache : PlatformMemoryCache
{
private static string _instanceId { get; } = $"{Environment.MachineName}_{Guid.NewGuid():N}";
private static readonly string _instanceId = $"{Environment.MachineName}_{Guid.NewGuid():N}";
private bool _isSubscribed;
private readonly ISubscriber _bus;
private readonly RedisCachingOptions _redisCachingOptions;
Expand Down Expand Up @@ -41,7 +41,7 @@ public RedisPlatformMemoryCache(IMemoryCache memoryCache

private void CacheCancellableTokensRegistry_OnTokenCancelled(TokenCancelledEventArgs e)
{
var message = new RedisCachingMessage { InstanceId = _instanceId, IsToken = true, CacheKeys = new[] { e.TokenKey } };
var message = new RedisCachingMessage { InstanceId = _instanceId, IsToken = true, CacheKeys = [e.TokenKey] };
Publish(message);
_log.LogTrace("Published token cancellation message {Message}", message.ToString());
}
Expand All @@ -65,7 +65,7 @@ protected virtual void OnMessage(RedisChannel channel, RedisValue redisValue)
{
var message = JsonConvert.DeserializeObject<RedisCachingMessage>(redisValue);

if (!string.IsNullOrEmpty(message.InstanceId) && !message.InstanceId.EqualsInvariant(_instanceId))
if (!string.IsNullOrEmpty(message.InstanceId) && !message.InstanceId.EqualsIgnoreCase(_instanceId))
{
_log.LogTrace("Received message {Message}", message.ToString());

Expand Down Expand Up @@ -95,7 +95,7 @@ public override bool TryGetValue(object key, out object value)

protected override void EvictionCallback(object key, object value, EvictionReason reason, object state)
{
var message = new RedisCachingMessage { InstanceId = _instanceId, CacheKeys = new[] { key } };
var message = new RedisCachingMessage { InstanceId = _instanceId, CacheKeys = [key] };
Publish(message);
_log.LogTrace("Published message {Message} to the Redis backplane", message);

Expand Down
5 changes: 3 additions & 2 deletions src/VirtoCommerce.Platform.Core/Common/EnumUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static T SafeParseFlags<T>(string value, T defaultValue, string separator

if (!string.IsNullOrEmpty(value))
{
var parts = value.Split(new[] { separator }, StringSplitOptions.RemoveEmptyEntries);
var parts = value.Split([separator], StringSplitOptions.RemoveEmptyEntries);

foreach (var part in parts)
{
Expand Down Expand Up @@ -78,7 +78,8 @@ public static string SafeRemoveFlagFromEnumString<T>(string value, T flag, char
else
{
var parts = value.Split(separator, StringSplitOptions.RemoveEmptyEntries);
result = string.Join(separator, parts.Where(x => !x.EqualsInvariant(flag.ToString())));
var flagString = flag.ToString();
result = string.Join(separator, parts.Where(x => !x.EqualsIgnoreCase(flagString)));
}
}
return result;
Expand Down
43 changes: 30 additions & 13 deletions src/VirtoCommerce.Platform.Core/Common/SemanticVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,16 @@ public class SemanticVersion : IComparable
{
private readonly Version _version;

public static readonly char[] Delimiters = { '.', '-' };
public static readonly char[] Delimiters = ['.', '-'];

public static readonly Regex SemanticVersionStrictRegex = new Regex(
public static readonly Regex SemanticVersionStrictRegex = new(
@"^(?<Version>([0-9]|[1-9][0-9]*)(\.([0-9]|[1-9][0-9]*)){2,3})" +
@"(?>\-(?<Prerelease>[0-9A-Za-z\-\.]+))?(?<Metadata>\+[0-9A-Za-z-]+)?$",
RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture | RegexOptions.Compiled);

public SemanticVersion(Version version)
{
if (version == null)
{
throw new ArgumentNullException(nameof(version));
}
ArgumentNullException.ThrowIfNull(version);

_version = NormalizeVersionValue(version);
}
Expand All @@ -46,8 +43,7 @@ public SemanticVersion(Version version)

public bool IsCompatibleWithBySemVer(SemanticVersion other)
{
if (other == null)
throw new ArgumentNullException(nameof(other));
ArgumentNullException.ThrowIfNull(other);

//MAJOR version when you make incompatible API changes,
var retVal = Major == other.Major;
Expand All @@ -61,8 +57,7 @@ public bool IsCompatibleWithBySemVer(SemanticVersion other)

public bool IsCompatibleWith(SemanticVersion other)
{
if (other == null)
throw new ArgumentNullException(nameof(other));
ArgumentNullException.ThrowIfNull(other);

var comparisonResult = CompareTo(other);
return comparisonResult <= 0;
Expand All @@ -81,7 +76,7 @@ public static SemanticVersion Parse(string value)
{
var normalizedVersion = NormalizeVersionValue(versionValue);
var result = new SemanticVersion(normalizedVersion);
if (((ICollection<Group>)match.Groups).Any(x => x.Name.EqualsInvariant("Prerelease")))
if (((ICollection<Group>)match.Groups).Any(x => x.Name.EqualsIgnoreCase("Prerelease")))
{
result.Prerelease = match.Groups["Prerelease"].Value;
}
Expand Down Expand Up @@ -180,22 +175,27 @@ public override int GetHashCode()

public int CompareTo(object obj)
{
if (obj == null)
throw new ArgumentNullException(nameof(obj));
ArgumentNullException.ThrowIfNull(obj);

var other = (SemanticVersion)obj;

var result = Major.CompareTo(other.Major);
if (result != 0)
{
return result;
}

result = Minor.CompareTo(other.Minor);
if (result != 0)
{
return result;
}

result = Patch.CompareTo(other.Patch);
if (result != 0)
{
return result;
}

return CompareComponent(Prerelease, other.Prerelease, true);
}
Expand All @@ -205,12 +205,19 @@ private static int CompareComponent(string a, string b, bool nonemptyIsLower = f
var aEmpty = string.IsNullOrEmpty(a);
var bEmpty = string.IsNullOrEmpty(b);
if (aEmpty && bEmpty)
{
return 0;
}

if (aEmpty)
{
return nonemptyIsLower ? 1 : -1;
}

if (bEmpty)
{
return nonemptyIsLower ? -1 : 1;
}

var aComps = a.Split(Delimiters);
var bComps = b.Split(Delimiters);
Expand All @@ -227,17 +234,27 @@ private static int CompareComponent(string a, string b, bool nonemptyIsLower = f
{
r = aNum.CompareTo(bNum);
if (r != 0)
{
return r;
}
}
else
{
if (aIsNum)
{
return -1;
}

if (bIsNum)
{
return 1;
}

r = string.CompareOrdinal(ac, bc);
if (r != 0)
{
return r;
}
}
}

Expand Down
16 changes: 11 additions & 5 deletions src/VirtoCommerce.Platform.Core/Common/SortInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ public enum SortDirection

public class SortInfo : IEquatable<SortInfo>
{
private static readonly char[] _columnSeparators = [';'];
private static readonly char[] _directionSeparators = [':', '-'];

public string SortColumn { get; set; }
public SortDirection SortDirection { get; set; }

public override string ToString()
{
return SortColumn + ":" + (SortDirection == SortDirection.Descending ? "desc" : "asc");
return SortColumn + (SortDirection == SortDirection.Descending ? ":desc" : string.Empty);
}

public static string ToString(IEnumerable<SortInfo> sortInfos)
Expand All @@ -37,13 +40,16 @@ public static string ToString(IEnumerable<SortInfo> sortInfos)
public static IEnumerable<SortInfo> Parse(string sortExpr)
{
var retVal = new List<SortInfo>();

if (string.IsNullOrEmpty(sortExpr))
{
return retVal;
}

var sortInfoStrings = sortExpr.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
var sortInfoStrings = sortExpr.Split(_columnSeparators, StringSplitOptions.RemoveEmptyEntries);
foreach (var sortInfoString in sortInfoStrings)
{
var parts = sortInfoString.Split(new[] { ':', '-' }, StringSplitOptions.RemoveEmptyEntries);
var parts = sortInfoString.Split(_directionSeparators, StringSplitOptions.RemoveEmptyEntries);
if (parts.Any())
{
var sortInfo = new SortInfo
Expand All @@ -53,7 +59,7 @@ public static IEnumerable<SortInfo> Parse(string sortExpr)
};
if (parts.Length > 1)
{
sortInfo.SortDirection = parts[1].StartsWith("desc", StringComparison.InvariantCultureIgnoreCase) ? SortDirection.Descending : SortDirection.Ascending;
sortInfo.SortDirection = parts[1].StartsWithIgnoreCase("desc") ? SortDirection.Descending : SortDirection.Ascending;
}
retVal.Add(sortInfo);
}
Expand All @@ -64,7 +70,7 @@ public static IEnumerable<SortInfo> Parse(string sortExpr)
public bool Equals(SortInfo other)
{
return other != null
&& string.Equals(SortColumn, other.SortColumn, StringComparison.OrdinalIgnoreCase)
&& SortColumn.EqualsIgnoreCase(other.SortColumn)
&& SortDirection == other.SortDirection;
}

Expand Down
11 changes: 4 additions & 7 deletions src/VirtoCommerce.Platform.Core/Domain/AbstractTypeFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,13 +254,10 @@ public static BaseType TryCreateInstance(string typeName, params object[] args)
/// <returns>The TypeInfo instance for the specified type name.</returns>
public static TypeInfo<BaseType> FindTypeInfoByName(string typeName)
{
//Try find first direct type match from registered types
var result = _typeInfos.FirstOrDefault(x => x.TypeName.EqualsInvariant(typeName));
//Try to find first direct type match from registered types
//Then need to find in inheritance chain from registered types
if (result == null)
{
result = _typeInfos.FirstOrDefault(x => x.IsAssignableTo(typeName));
}
var result = _typeInfos.FirstOrDefault(x => x.TypeName.EqualsIgnoreCase(typeName)) ??
_typeInfos.FirstOrDefault(x => x.IsAssignableTo(typeName));
return result;
}
}
Expand Down Expand Up @@ -378,7 +375,7 @@ public TypeInfo<BaseType> WithTypeName(string name)
/// <returns>true if the associated type is assignable to the specified type name; otherwise, false.</returns>
public bool IsAssignableTo(string typeName)
{
return Type.GetTypeInheritanceChainTo(typeof(BaseType)).Concat(new[] { typeof(BaseType) }).Any(t => typeName.EqualsInvariant(t.Name));
return Type.GetTypeInheritanceChainTo(typeof(BaseType)).Concat([typeof(BaseType)]).Any(t => typeName.EqualsIgnoreCase(t.Name));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public static bool IsModuleInstalled(this IModuleCatalog moduleCatalog, string m
{
return moduleCatalog.Modules
.OfType<ManifestModuleInfo>()
.Any(x => x.Id.EqualsInvariant(moduleId) && x.IsInstalled);
.Any(x => x.Id.EqualsIgnoreCase(moduleId) && x.IsInstalled);
}
}
}
Loading
Loading