Skip to content

Commit

Permalink
Make CultureDictionaryRecordKey record struct (#15413)
Browse files Browse the repository at this point in the history
  • Loading branch information
hishamco authored Mar 13, 2024
1 parent 77841f9 commit 2f84a12
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 52 deletions.
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

0 comments on commit 2f84a12

Please sign in to comment.