diff --git a/src/ArtemisNetCoreClient/ArtemisBinaryConverter.cs b/src/ArtemisNetCoreClient/ArtemisBinaryConverter.cs index 4ae943b..8df39e4 100644 --- a/src/ArtemisNetCoreClient/ArtemisBinaryConverter.cs +++ b/src/ArtemisNetCoreClient/ArtemisBinaryConverter.cs @@ -411,4 +411,34 @@ public static int WriteNullableSimpleString(ref byte destination, string? value) offset += WriteSimpleString(ref destination.GetOffset(offset), value); return offset; } + + public const int GuidByteCount = 16; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static int WriteGuid(ref byte destination, Guid value) + { + var span = MemoryMarshal.CreateSpan(ref destination, GuidByteCount); + _ = value.TryWriteBytes(span, bigEndian: true, out var bytesWritten); + return bytesWritten; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static int ReadGuid(in ReadOnlySpan source, out Guid value) + { + value = new Guid(source, bigEndian: true); + return GuidByteCount; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static int GetNullableGuidByteCount(Guid? value) + { + var byteCount = sizeof(byte); + if (value.HasValue) + { + byteCount += GuidByteCount; + } + + return byteCount; + } + } \ No newline at end of file diff --git a/test/ArtemisNetCoreClient.Tests/ArtemisBinaryConverterSpec.cs b/test/ArtemisNetCoreClient.Tests/ArtemisBinaryConverterSpec.cs index ff614ec..a43b4d8 100644 --- a/test/ArtemisNetCoreClient.Tests/ArtemisBinaryConverterSpec.cs +++ b/test/ArtemisNetCoreClient.Tests/ArtemisBinaryConverterSpec.cs @@ -414,4 +414,71 @@ public void should_encode_nullable_simple_string(byte[] encoded, string? expecte Assert.Equal(encoded, byteBuffer); Assert.Equal(encoded.Length, writtenBytes); } + + [Fact] + public void Should_encode_guid() + { + // Arrange + var byteBuffer = new byte[ArtemisBinaryConverter.GuidByteCount]; + var guid = Guid.Parse("c69fdd4e-afa3-46bb-a6ef-f2d5fa4172fa"); + + // Act + var writtenBytes = ArtemisBinaryConverter.WriteGuid(ref byteBuffer.AsSpan().GetReference(), guid); + + // Assert + var expected = new byte[] + { + unchecked((byte) -58), + unchecked((byte) -97), + unchecked((byte) -35), + 78, + unchecked((byte) -81), + unchecked((byte) -93), + 70, + unchecked((byte) -69), + unchecked((byte) -90), + unchecked((byte) -17), + unchecked((byte) -14), + unchecked((byte) -43), + unchecked((byte) -6), + 65, + 114, + unchecked((byte) -6) + }; + Assert.Equal(expected, byteBuffer); + Assert.Equal(ArtemisBinaryConverter.GuidByteCount, writtenBytes); + } + + [Fact] + public void Should_decode_guid() + { + // Arrange + var byteBuffer = new byte[] + { + unchecked((byte) -58), + unchecked((byte) -97), + unchecked((byte) -35), + 78, + unchecked((byte) -81), + unchecked((byte) -93), + 70, + unchecked((byte) -69), + unchecked((byte) -90), + unchecked((byte) -17), + unchecked((byte) -14), + unchecked((byte) -43), + unchecked((byte) -6), + 65, + 114, + unchecked((byte) -6) + }; + + // Act + var readBytes = ArtemisBinaryConverter.ReadGuid(byteBuffer, out var value); + + // Assert + Assert.Equal(Guid.Parse("c69fdd4e-afa3-46bb-a6ef-f2d5fa4172fa"), value); + Assert.Equal(ArtemisBinaryConverter.GuidByteCount, readBytes); + } + } \ No newline at end of file