generated from Nexus-Mods/NexusMods.App.Template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
238 additions
and
72 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
src/NexusMods.EventSourcing/Serialization/AFixedSizeSerializer.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,31 @@ | ||
using System; | ||
using NexusMods.EventSourcing.Abstractions.Serialization; | ||
|
||
namespace NexusMods.EventSourcing.Serialization; | ||
|
||
/// <summary> | ||
/// A abstract fixed size serializer. | ||
/// </summary> | ||
/// <param name="size"></param> | ||
/// <typeparam name="TType"></typeparam> | ||
public abstract class AFixedSizeSerializer<TType>(int Size) : IFixedSizeSerializer<TType> | ||
{ | ||
/// <inheritdoc /> | ||
public bool CanSerialize(Type valueType) | ||
{ | ||
return valueType == typeof(TType); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public bool TryGetFixedSize(Type valueType, out int size) | ||
{ | ||
size = Size; | ||
return true; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public abstract void Serialize(TType value, Span<byte> output); | ||
|
||
/// <inheritdoc /> | ||
public abstract TType Deserialize(ReadOnlySpan<byte> from); | ||
} |
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
17 changes: 17 additions & 0 deletions
17
src/NexusMods.EventSourcing/Serialization/DoubleSerializer.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,17 @@ | ||
using System; | ||
using System.Buffers.Binary; | ||
|
||
namespace NexusMods.EventSourcing.Serialization; | ||
|
||
internal sealed class DoubleSerializer() : AFixedSizeSerializer<double>(sizeof(double)) | ||
{ | ||
public override void Serialize(double value, Span<byte> output) | ||
{ | ||
BinaryPrimitives.WriteDoubleBigEndian(output, value); | ||
} | ||
|
||
public override double Deserialize(ReadOnlySpan<byte> from) | ||
{ | ||
return BinaryPrimitives.ReadDoubleBigEndian(from); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/NexusMods.EventSourcing/Serialization/FloatSerializer.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,23 @@ | ||
using System; | ||
using System.Buffers.Binary; | ||
using NexusMods.EventSourcing.Abstractions.Serialization; | ||
|
||
namespace NexusMods.EventSourcing.Serialization; | ||
|
||
/// <summary> | ||
/// Serializer for floats. | ||
/// </summary> | ||
internal sealed class FloatSerializer() : AFixedSizeSerializer<float>(sizeof(float)) | ||
{ | ||
/// <inheritdoc /> | ||
public override void Serialize(float value, Span<byte> output) | ||
{ | ||
BinaryPrimitives.WriteSingleBigEndian(output, value); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override float Deserialize(ReadOnlySpan<byte> from) | ||
{ | ||
return BinaryPrimitives.ReadSingleBigEndian(from); | ||
} | ||
} |
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
17 changes: 17 additions & 0 deletions
17
src/NexusMods.EventSourcing/Serialization/Int16Serializer.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,17 @@ | ||
using System; | ||
using System.Buffers.Binary; | ||
|
||
namespace NexusMods.EventSourcing.Serialization; | ||
|
||
internal class Int16Serializer() : AFixedSizeSerializer<short>(sizeof(short)) | ||
{ | ||
public override void Serialize(short value, Span<byte> output) | ||
{ | ||
BinaryPrimitives.WriteInt16BigEndian(output, value); | ||
} | ||
|
||
public override short Deserialize(ReadOnlySpan<byte> from) | ||
{ | ||
return BinaryPrimitives.ReadInt16BigEndian(from); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/NexusMods.EventSourcing/Serialization/Int32Serializer.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,16 @@ | ||
using System; | ||
using System.Buffers.Binary; | ||
|
||
namespace NexusMods.EventSourcing.Serialization; | ||
|
||
internal sealed class Int32Serializer() : AFixedSizeSerializer<int>(sizeof(int)) { | ||
public override void Serialize(int value, Span<byte> output) | ||
{ | ||
BinaryPrimitives.WriteInt32BigEndian(output, value); | ||
} | ||
|
||
public override int Deserialize(ReadOnlySpan<byte> from) | ||
{ | ||
return BinaryPrimitives.ReadInt32BigEndian(from); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/NexusMods.EventSourcing/Serialization/Int64Serializer.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,18 @@ | ||
using System; | ||
using System.Buffers.Binary; | ||
using NexusMods.EventSourcing.Abstractions.Serialization; | ||
|
||
namespace NexusMods.EventSourcing.Serialization; | ||
|
||
internal sealed class Int64Serializer() : AFixedSizeSerializer<long>(sizeof(long)) | ||
{ | ||
public override void Serialize(long value, Span<byte> output) | ||
{ | ||
BinaryPrimitives.WriteInt64BigEndian(output, value); | ||
} | ||
|
||
public override long Deserialize(ReadOnlySpan<byte> from) | ||
{ | ||
return (long) BinaryPrimitives.ReadInt64BigEndian(from); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/NexusMods.EventSourcing/Serialization/UInt16Serializer.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,23 @@ | ||
using System; | ||
using System.Buffers.Binary; | ||
using NexusMods.EventSourcing.Abstractions.Serialization; | ||
|
||
namespace NexusMods.EventSourcing.Serialization; | ||
|
||
/// <summary> | ||
/// Serializer for unsigned 16 bit integers. | ||
/// </summary> | ||
internal sealed class UInt16Serializer() : AFixedSizeSerializer<ushort>(sizeof(ushort)) | ||
{ | ||
/// <inheritdoc /> | ||
public override void Serialize(ushort value, Span<byte> output) | ||
{ | ||
BinaryPrimitives.WriteUInt16BigEndian(output, value); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override ushort Deserialize(ReadOnlySpan<byte> from) | ||
{ | ||
return BinaryPrimitives.ReadUInt16BigEndian(from); | ||
} | ||
} |
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
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
70 changes: 70 additions & 0 deletions
70
tests/NexusMods.EventSourcing.Tests/ValueSerializerTests.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,70 @@ | ||
using System.Buffers; | ||
using NexusMods.EventSourcing.Abstractions.Serialization; | ||
|
||
namespace NexusMods.EventSourcing.Tests; | ||
|
||
public class ValueSerializerTests | ||
{ | ||
private readonly IEnumerable<ISerializer> _serializers; | ||
|
||
public ValueSerializerTests(IEnumerable<ISerializer> serializers) | ||
{ | ||
_serializers = serializers; | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(Data))] | ||
public void SerializeValue(string typeName, object value) | ||
{ | ||
var serializer = _serializers.FirstOrDefault(s => s.CanSerialize(value.GetType())); | ||
serializer.Should().NotBeNull(); | ||
if (serializer == default) return; | ||
|
||
serializer.CanSerialize(value.GetType()).Should().BeTrue(); | ||
|
||
var methodInfo = typeof(ValueSerializerTests).GetMethod(nameof(TestSerializer), System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); | ||
methodInfo = methodInfo!.MakeGenericMethod(value.GetType()); | ||
methodInfo.Invoke(this, [serializer, value]); | ||
} | ||
|
||
private void TestSerializer<T>(ISerializer serializer, object t) | ||
{ | ||
if(serializer is IFixedSizeSerializer<T> fixedSizeSerializer) | ||
{ | ||
fixedSizeSerializer.TryGetFixedSize(typeof(T), out var size).Should().BeTrue(); | ||
var span = new byte[size]; | ||
fixedSizeSerializer.Serialize((T)t, span); | ||
var deserialized = fixedSizeSerializer.Deserialize(span); | ||
deserialized.Should().Be(t); | ||
} | ||
else if (serializer is IVariableSizeSerializer<T> variableSizeSerializer) | ||
{ | ||
var writer = new ArrayBufferWriter<byte>(); | ||
variableSizeSerializer.Serialize((T)t, writer); | ||
var deserializedSize = variableSizeSerializer.Deserialize(writer.WrittenSpan, out var read); | ||
deserializedSize.Should().Be(writer.WrittenCount); | ||
read.Should().Be(t); | ||
} | ||
else | ||
{ | ||
throw new Exception("Unknown serializer type"); | ||
} | ||
} | ||
|
||
|
||
public static IEnumerable<object[]> Data = new object[] | ||
{ | ||
"Test", | ||
(byte)1, | ||
(short)1, | ||
(int)1, | ||
(long)1, | ||
(float)1, | ||
(double)1, | ||
(ushort)1, | ||
(uint)1, | ||
(ulong)1, | ||
Guid.Parse("154C9597-9E14-41A8-BFB9-2AEA27CA534B"), | ||
}.Select(v => new[] {v.GetType().Name, v }).ToList(); | ||
|
||
} |