Skip to content

Commit

Permalink
Implemented support of different types of dictionary keys via compari…
Browse files Browse the repository at this point in the history
…son of their string representations.
  • Loading branch information
hennadiilu committed May 3, 2024
1 parent a6e4574 commit c1b294e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 12 deletions.
10 changes: 9 additions & 1 deletion src/Heleonix.Reflection/Reflector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,15 @@ private static object GetElementAt(object container, object index)
{
if (container is IDictionary dictionary)
{
return dictionary.Contains(index) ? dictionary[index] : null;
foreach (var key in dictionary.Keys)
{
if (key.Equals(index) || Convert.ToString(key) == Convert.ToString(index))
{
return dictionary[key];
}
}

return null;
}

if (container is IList list)
Expand Down
9 changes: 7 additions & 2 deletions test/Heleonix.Reflection.Tests/Common/Dummies/SubItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,13 @@ public class SubItem
public IEnumerable<SubSubItem> SubSubItemsEnumerableProperty { get; set; } = new Queue<SubSubItem>();

/// <summary>
/// Gets or sets the sub sub items dictionary property.
/// Gets or sets the sub sub items string dictionary property.
/// </summary>
public Dictionary<string, SubSubItem> SubSubItemsDictionaryProperty { get; set; } = new Dictionary<string, SubSubItem>();
public Dictionary<string, SubSubItem> SubSubItemsStringDictionaryProperty { get; set; } = new Dictionary<string, SubSubItem>();

/// <summary>
/// Gets or sets the sub sub items int dictionary property.
/// </summary>
public Dictionary<int, SubSubItem> SubSubItemsIntDictionaryProperty { get; set; } = new Dictionary<int, SubSubItem>();
}
}
33 changes: 29 additions & 4 deletions test/Heleonix.Reflection.Tests/Reflector.Get.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,12 @@ public static void Get()

And("the indexer is in a middle of the memberPath", () =>
{
And("the collection is a dictionary", () =>
And("the collection is a string-keyed dictionary", () =>
{
instance.SubItemProperty.SubSubItemsDictionaryProperty.Add(
instance.SubItemProperty.SubSubItemsStringDictionaryProperty.Add(
"First Key",
new SubSubItem { TextProperty = "First Value" });
memberPath = "SubItemProperty.SubSubItemsDictionaryProperty[First Key].TextProperty";
memberPath = "SubItemProperty.SubSubItemsStringDictionaryProperty[First Key].TextProperty";

Should("provide the target value", () =>
{
Expand All @@ -209,7 +209,32 @@ public static void Get()

And("the key does not exist", () =>
{
memberPath = "SubItemProperty.SubSubItemsDictionaryProperty[NO KEY].TextProperty";
memberPath = "SubItemProperty.SubSubItemsStringDictionaryProperty[NO KEY].TextProperty";

Should("provide the default value and return false", () =>
{
Assert.That(result, Is.Null);
Assert.That(returnValue, Is.False);
});
});
});

And("the collection is an int-keyed dictionary", () =>
{
instance.SubItemProperty.SubSubItemsIntDictionaryProperty.Add(
12345,
new SubSubItem { TextProperty = "First Value" });
memberPath = "SubItemProperty.SubSubItemsIntDictionaryProperty[12345].TextProperty";

Should("provide the target value", () =>
{
Assert.That(result, Is.EqualTo("First Value"));
Assert.That(returnValue, Is.True);
});

And("the key does not exist", () =>
{
memberPath = "SubItemProperty.SubSubItemsStringDictionaryProperty[111].TextProperty";

Should("provide the default value and return false", () =>
{
Expand Down
10 changes: 5 additions & 5 deletions test/Heleonix.Reflection.Tests/Reflector.Set.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,25 +206,25 @@ public static void Set()

And("the indexer is in a middle of the memberPath", () =>
{
And("the collection is a dictionary", () =>
And("the collection is a string-keyed dictionary", () =>
{
instance.SubItemProperty.SubSubItemsDictionaryProperty.Add(
instance.SubItemProperty.SubSubItemsStringDictionaryProperty.Add(
"First Key",
new SubSubItem { TextProperty = "First Value" });
memberPath = "SubItemProperty.SubSubItemsDictionaryProperty[First Key].TextProperty";
memberPath = "SubItemProperty.SubSubItemsStringDictionaryProperty[First Key].TextProperty";
value = "New First Value";

Should("set the value and return true", () =>
{
Assert.That(
instance.SubItemProperty.SubSubItemsDictionaryProperty["First Key"].TextProperty,
instance.SubItemProperty.SubSubItemsStringDictionaryProperty["First Key"].TextProperty,
Is.EqualTo(value));
Assert.That(returnValue, Is.True);
});

And("the key does not exist", () =>
{
memberPath = "SubItemProperty.SubSubItemsDictionaryProperty[NO KEY].TextProperty";
memberPath = "SubItemProperty.SubSubItemsStringDictionaryProperty[NO KEY].TextProperty";

Should("return false", () =>
{
Expand Down

0 comments on commit c1b294e

Please sign in to comment.