Skip to content

Commit

Permalink
Encode/decode Guid
Browse files Browse the repository at this point in the history
  • Loading branch information
Havret committed Apr 28, 2024
1 parent bf836e7 commit f687672
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/ArtemisNetCoreClient/ArtemisBinaryConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<byte> 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;
}

}
67 changes: 67 additions & 0 deletions test/ArtemisNetCoreClient.Tests/ArtemisBinaryConverterSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}

0 comments on commit f687672

Please sign in to comment.