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 CultureDictionaryRecordKey record struct #15413

Merged
merged 7 commits into from
Mar 13, 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
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ public static CultureDictionaryRecordKey GetKey(string messageId, string context
throw new ArgumentException("MessageId can't be empty.", nameof(messageId));
}

return new CultureDictionaryRecordKey(messageId, context);
return new CultureDictionaryRecordKey
{
MessageId = messageId, Context = context
};
}
}
}
Original file line number Diff line number Diff line change
@@ -1,70 +1,26 @@
using System;

namespace OrchardCore.Localization
{
/// <summary>
/// Represents a key for <see cref="CultureDictionaryRecord"/>.
/// </summary>
public readonly struct CultureDictionaryRecordKey : IEquatable<CultureDictionaryRecordKey>
public readonly record struct CultureDictionaryRecordKey
{
/// <summary>
/// Creates new instance of <see cref="CultureDictionaryRecordKey"/>.
/// </summary>
/// <param name="messageId">The message Id.</param>
public CultureDictionaryRecordKey(string messageId) : this(messageId, null)
{

}

/// <summary>
/// Creates new instance of <see cref="CultureDictionaryRecordKey"/>.
/// </summary>
/// <param name="messageId">The message Id.</param>
/// <param name="context">The message context.</param>
public CultureDictionaryRecordKey(string messageId, string context)
{
MessageId = messageId;
Context = context;
}

/// <summary>
/// Gets the message Id.
/// </summary>
public string MessageId { get; }
public required string MessageId { get; init; }

/// <summary>
/// Gets the message context.
/// </summary>
public string Context { get; }
public string Context { get; init; }

public static implicit operator string(CultureDictionaryRecordKey cultureDictionaryRecordKey)
=> cultureDictionaryRecordKey.ToString();

/// <inheritdoc />
public override bool Equals(object obj)
{
if (obj is CultureDictionaryRecordKey other)
{
return Equals(other);
}

return false;
}

/// <inheritdoc />
public bool Equals(CultureDictionaryRecordKey other)
=> string.Equals(MessageId, other.MessageId) && String.Equals(Context, other.Context);

/// <inheritdoc />
public override int GetHashCode() => HashCode.Combine(MessageId, Context);

public override string ToString()
=> string.IsNullOrEmpty(Context)
? MessageId
: Context.ToLowerInvariant() + "|" + MessageId;

public static bool operator ==(CultureDictionaryRecordKey left, CultureDictionaryRecordKey right) => left.Equals(right);

public static bool operator !=(CultureDictionaryRecordKey left, CultureDictionaryRecordKey right) => !(left == right);
: $"{Context.ToLowerInvariant()}|{MessageId}";
}
}
4 changes: 2 additions & 2 deletions test/OrchardCore.Tests/Localization/CultureDictionaryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void MergeOverwritesTranslationsForSameKeys()
public void IndexerReturnNullIfKeyDoesntExist()
{
var dictionary = new CultureDictionary("cs", PluralizationRule.Czech);
var key = new CultureDictionaryRecordKey("ball");
var key = new CultureDictionaryRecordKey { MessageId = "ball" };
var translation = dictionary[key];

Assert.Null(translation);
Expand All @@ -48,7 +48,7 @@ public void IndexerThrowsPluralFormNotFoundExceptionIfSpecifiedPluralFormDoesntE

Assert.Throws<PluralFormNotFoundException>(() =>
{
var key = new CultureDictionaryRecordKey("ball");
var key = new CultureDictionaryRecordKey { MessageId = "ball" };

return dictionary[key, 5];
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void GetDictionaryReturnsDictionaryWithTranslationsFromProvider()
var manager = new LocalizationManager(new[] { _pluralRuleProvider.Object }, new[] { _translationProvider.Object }, _memoryCache);

var dictionary = manager.GetDictionary(new CultureInfo("cs"));
var key = new CultureDictionaryRecordKey("ball");
var key = new CultureDictionaryRecordKey { MessageId = "ball" };

dictionary.Translations.TryGetValue(key, out var translations);

Expand Down
Loading