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
17 changes: 17 additions & 0 deletions Tomlet.Tests/EnumTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,23 @@ public void SerializingAnUndefinedEnumValueThrows()
Assert.Throws<ArgumentException>(() => TomletMain.TomlStringFrom(testObj));
}

[Fact]
public void CanSerializeAndDeserializeCombinedFlagsEnum()
{
var toml = TomletMain.TomlStringFrom(new {EnumValue = TestFlagsEnum.Flag1 | TestFlagsEnum.Flag3}).Trim();
Assert.Equal("EnumValue = \"Flag1, Flag3\"", toml);

var result = TomletMain.To<Dictionary<string, TestFlagsEnum>>(toml);
Assert.Equal(TestFlagsEnum.Flag1 | TestFlagsEnum.Flag3, result["EnumValue"]);
}

[Fact]
public void SerializingAFlagsEnumWithAnUndeclaredBitThrows()
{
var testObj = new {EnumValue = TestFlagsEnum.Flag1 | (TestFlagsEnum)8};
Assert.Throws<ArgumentException>(() => TomletMain.TomlStringFrom(testObj));
}

[Fact]
public void CanSerializeDictWithEnumValues()
{
Expand Down
12 changes: 12 additions & 0 deletions Tomlet.Tests/TestModelClasses/TestFlagsEnum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace Tomlet.Tests.TestModelClasses;

[Flags]
public enum TestFlagsEnum
{
None = 0,
Flag1 = 1,
Flag2 = 2,
Flag3 = 4
}
20 changes: 19 additions & 1 deletion Tomlet/TomlCompositeSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@ namespace Tomlet;

internal static class TomlCompositeSerializer
{
private static string GetEnumName(Type type, object value)
{
var name = Enum.GetName(type, value);
if (name != null)
return name;

//A [Flags] enum value can be a combination of declared values, which ToString writes as "A, B" and Enum.Parse reads back.
//If any bit has no declared name, ToString falls back to a number, which we still reject.
if (type.IsDefined(typeof(FlagsAttribute), false))
{
var combined = value.ToString();
if (!char.IsDigit(combined[0]) && combined[0] != '-')
return combined;
}

throw new ArgumentException($"Tomlet: Cannot serialize {value} as an enum of type {type} because the enum type does not declare a name for that value");
}

#if MODERN_DOTNET
[UnconditionalSuppressMessage("AOT", "IL2072", Justification = "Any field that is being serialized must have been used as a field in the consuming code in order for the code path that queries it to run, so the dynamic code requirement is already satisfied.")]
public static TomlSerializationMethods.Serialize<object> For([DynamicallyAccessedMembers(TomlSerializationMethods.MainDeserializerAccessedMemberTypes)] Type type, TomlSerializerOptions options)
Expand All @@ -23,7 +41,7 @@ public static TomlSerializationMethods.Serialize<object> For(Type type, TomlSeri
if (type.IsEnum)
{
var stringSerializer = TomlSerializationMethods.GetSerializer(typeof(string), options);
serializer = o => stringSerializer.Invoke(Enum.GetName(type, o!) ?? throw new ArgumentException($"Tomlet: Cannot serialize {o} as an enum of type {type} because the enum type does not declare a name for that value"));
serializer = o => stringSerializer.Invoke(GetEnumName(type, o!));
}
else
{
Expand Down