Skip to content
Open
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
6 changes: 6 additions & 0 deletions Tomlet.Tests/DeliberatelyIncorrectTestResources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Tomlet.Tests/DeliberatelyIncorrectTestResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ apple = "red"

[fruit.apple]
texture = "smooth"</value>
</data>
<data name="EnumKeyRedefinitionViaTableTestInput" xml:space="preserve">
<value>[Subnames]
[Subnames.Value1]
a = 'A'
b = 'B'
[Subnames.Value1]
a = 'A'
b = 'B'</value>
</data>
<data name="ReDefiningSubTableAsSubTableArrayTestInput" xml:space="preserve">
<value># INVALID TOML DOC
Expand Down
11 changes: 10 additions & 1 deletion Tomlet.Tests/EnumTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,16 @@ public void CanDeserializeEnumDictionary()
var expected = new Dictionary<TestEnum, int> {{TestEnum.Value1, 1}, {TestEnum.Value2, 2}, {TestEnum.Value3, 3}};
Assert.Equal(expected, result);
}


[Fact]
public void CanIgnoreInvalidEnumDictionaryKeys()
{
var toml = "Value1 = 1\nValue2 = 2\nValue3 = 3\nValue4 = 4\nValue5 = 5\nValue6 = 6\n";
var result = TomletMain.To<Dictionary<TestEnum, int>>(toml, new TomlSerializerOptions { IgnoreInvalidEnumValues = true });
var expected = new Dictionary<TestEnum, int> {{TestEnum.Value1, 1}, {TestEnum.Value2, 2}, {TestEnum.Value3, 3}};
Assert.Equal(expected, result);
}

[Fact]
public void CanDeserializeEnumDictionaryWithFields()
{
Expand Down
4 changes: 4 additions & 0 deletions Tomlet.Tests/ExceptionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ public void ReDefiningASubTableAsASubTableArrayThrowsAnException() =>
public void RedefiningAKeyAsATableNameThrowsAnException() =>
AssertThrows<TomlKeyRedefinitionException>(() => GetDocument(DeliberatelyIncorrectTestResources.KeyRedefinitionViaTableTestInput));

[Fact]
public void RedefiningAnEnumKeyAsATableEntryThrowsAnException() =>
AssertThrows<TomlTableRedefinitionException>(() => GetDocument(DeliberatelyIncorrectTestResources.EnumKeyRedefinitionViaTableTestInput));

[Fact]
public void DefiningATableArrayWithTheSameNameAsATableThrowsAnException() =>
AssertThrows<TomlTableArrayAlreadyExistsAsNonArrayException>(() => GetDocument(DeliberatelyIncorrectTestResources.DefiningAsArrayWhenAlreadyTableTestInput));
Expand Down
30 changes: 17 additions & 13 deletions Tomlet/TomlSerializationMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -388,28 +388,32 @@ private static Deserialize<Dictionary<TKey, TValue>> PrimitiveKeyedDictionaryDes
if (value is not TomlTable table)
throw new TomlTypeMismatchException(typeof(TomlTable), value.GetType(), typeof(Dictionary<TKey, TValue>));

return table.Entries.ToDictionary(
entry =>
return table.Entries
.Where(entry =>
{
if (!type.IsEnum)
{
return (TKey)(entry.Key as IConvertible).ToType(typeof(TKey), CultureInfo.InvariantCulture);
}
if (!type.IsEnum || !options.IgnoreInvalidEnumValues)
return true;

try
{
return (TKey)Enum.Parse(type, entry.Key, true);
_ = Enum.Parse(type, entry.Key, true);
return true;
}
catch (ArgumentException)
{
if (options.IgnoreInvalidEnumValues)
return (TKey)Enum.GetValues(type).GetValue(0)!;

throw new TomlEnumParseException(entry.Key, typeof(TKey));
return false;
}
})
.ToDictionary(entry =>
{
if (!type.IsEnum)
{
return (TKey)(entry.Key as IConvertible).ToType(typeof(TKey), CultureInfo.InvariantCulture);
}

return (TKey)Enum.Parse(type, entry.Key, true);
},
entry => (TValue)valueDeserializer(entry.Value)
);
entry => (TValue)valueDeserializer(entry.Value));
};
}

Expand Down
3 changes: 3 additions & 0 deletions Tomlet/TomlSerializerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@ public class TomlSerializerOptions
/// <summary>
/// When set to true, the deserializer will ignore invalid enum values (and they will be implicitly left at their default value). When set to false, an exception will be thrown if the enum value is not found.
/// </summary>
/// <remarks>
/// When deserializing dictionaries with enums as keys, invalid enum values will be skipped.
/// </remarks>
public bool IgnoreInvalidEnumValues { get; set; } = false;
}
Loading