-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(repeater): support stringified enum value serialization
closes #170
- Loading branch information
Showing
7 changed files
with
192 additions
and
53 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
src/SecTester.Repeater/Bus/Formatters/MessagePackStringEnumFormatter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Runtime.Serialization; | ||
using MessagePack; | ||
using MessagePack.Formatters; | ||
|
||
namespace SecTester.Repeater.Bus.Formatters; | ||
|
||
// ADHOC: MessagePack-CSharp prohibits declaration of IMessagePackFormatter<T> requesting to use System.Enum instead, refer to formatter interface argument type check | ||
// https://github.com/MessagePack-CSharp/MessagePack-CSharp/blob/db2320b3338735c9266110bbbfffe63f17dfdf46/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Resolvers/DynamicObjectResolver.cs#L623 | ||
|
||
public class MessagePackStringEnumFormatter<T> : IMessagePackFormatter<Enum> | ||
where T : Enum | ||
{ | ||
private static readonly Dictionary<T, string> EnumToString = typeof(T) | ||
.GetFields(BindingFlags.Public | BindingFlags.Static) | ||
.Select(field => new | ||
{ | ||
Value = (T)field.GetValue(null), | ||
StringValue = field.GetCustomAttribute<EnumMemberAttribute>()?.Value ?? field.Name | ||
}) | ||
.ToDictionary(x => x.Value, x => x.StringValue); | ||
|
||
private static readonly Dictionary<string, T> StringToEnum = EnumToString.ToDictionary(x => x.Value, x => x.Key); | ||
|
||
public void Serialize(ref MessagePackWriter writer, Enum value, MessagePackSerializerOptions options) | ||
{ | ||
if (!EnumToString.TryGetValue((T)value, out var stringValue)) | ||
{ | ||
throw new MessagePackSerializationException($"No string representation found for {value}"); | ||
} | ||
|
||
writer.Write(stringValue); | ||
} | ||
|
||
public Enum Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options) | ||
{ | ||
|
||
var stringValue = reader.ReadString(); | ||
|
||
if (!StringToEnum.TryGetValue(stringValue, out var enumValue)) | ||
{ | ||
throw new MessagePackSerializationException($"Unable to parse '{stringValue}' to {typeof(T).Name}."); | ||
} | ||
|
||
return enumValue; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
using System.Runtime.Serialization; | ||
|
||
namespace SecTester.Repeater; | ||
|
||
public enum Protocol | ||
{ | ||
[EnumMember(Value = "http")] | ||
Http | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
test/SecTester.Repeater.Tests/Bus/Formatters/MessagePackStringEnumFormatterTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using System.Runtime.Serialization; | ||
using MessagePack; | ||
using SecTester.Repeater.Bus.Formatters; | ||
|
||
namespace SecTester.Repeater.Tests.Bus.Formatters; | ||
|
||
public sealed class MessagePackStringEnumFormatterTests | ||
{ | ||
public enum Foo | ||
{ | ||
[EnumMember(Value = "bar")] | ||
Bar = 0, | ||
[EnumMember(Value = "baz_cux")] | ||
BazCux = 1 | ||
} | ||
|
||
[MessagePackObject] | ||
public record EnumDto | ||
{ | ||
[Key("foo")] | ||
[MessagePackFormatter(typeof(MessagePackStringEnumFormatter<Foo>))] | ||
public Enum Foo { get; set; } | ||
} | ||
|
||
private static readonly MessagePackSerializerOptions Options = MessagePackSerializerOptions.Standard; | ||
|
||
public static IEnumerable<object[]> | ||
Fixtures => | ||
new List<object[]> | ||
{ | ||
new object[] { "{\"foo\":\"bar\"}" }, | ||
new object[] { "{\"foo\":\"baz_cux\"}" } | ||
}; | ||
|
||
public static IEnumerable<object[]> | ||
WrongValueFixtures => | ||
new List<object[]> | ||
{ | ||
new object[] { "{\"foo\": null}" }, | ||
new object[] { "{\"foo\": 5}" }, | ||
new object[] { "{\"foo\":\"BazCux\"}" } | ||
}; | ||
|
||
|
||
[Theory] | ||
[MemberData(nameof(Fixtures))] | ||
public void MessagePackStringEnumFormatter_Serialize_ShouldCorrectlySerialize( | ||
string input) | ||
{ | ||
// arrange | ||
var binary = MessagePackSerializer.ConvertFromJson(input, Options); | ||
var obj = MessagePackSerializer.Deserialize<EnumDto>(binary, Options); | ||
|
||
|
||
// act | ||
var result = MessagePackSerializer.SerializeToJson(obj, Options); | ||
|
||
// assert | ||
result.Should().BeEquivalentTo(input); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(WrongValueFixtures))] | ||
public void MessagePackStringEnumFormatter_Deserialize_ShouldThrowWhenDataHasWrongValue(string input) | ||
{ | ||
// arrange | ||
var binary = MessagePackSerializer.ConvertFromJson(input, Options); | ||
|
||
// act | ||
var act = () => MessagePackSerializer.Deserialize<EnumDto>(binary, Options); | ||
|
||
// assert | ||
act.Should().Throw<MessagePackSerializationException>().WithMessage( | ||
"Failed to deserialize*"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters