Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement nullable amq string encoding/decoding #41

Merged
merged 1 commit into from
Mar 24, 2024
Merged
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
25 changes: 19 additions & 6 deletions src/ArtemisNetCoreClient/ByteBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public void WriteString(string value)
}
else
{
WriteStringAsBytes(value);
WriteAmqString(value);
}
}

Expand All @@ -194,7 +194,7 @@ private void WriteAsShorts(string value)
}
}

public void WriteStringAsBytes(string value)
public void WriteAmqString(string value)
{
var chars = value.AsSpan();
WriteInt(chars.Length << 1);
Expand Down Expand Up @@ -254,11 +254,11 @@ public string ReadString()
}
else
{
return ReadStringAsBytes();
return ReadAmqString();
}
}

public string ReadStringAsBytes()
public string ReadAmqString()
{
var actualByteCount = ReadInt();

Expand Down Expand Up @@ -294,11 +294,24 @@ private string ReadAsShorts(int length)
var value = _memoryStream.ReadByte();
return value == DataConstants.NotNull ? ReadString() : null;
}

public void WriteNullableAmqString(string? value)
{
if (value != null)
{
WriteByte(DataConstants.NotNull);
WriteAmqString(value);
}
else
{
WriteByte(DataConstants.Null);
}
}

public string? ReadNullableStringAsBytes()
public string? ReadNullableAmqString()
{
var value = _memoryStream.ReadByte();
return value == DataConstants.NotNull ? ReadStringAsBytes() : null;
return value == DataConstants.NotNull ? ReadAmqString() : null;
}

public void WriteSize()
Expand Down
2 changes: 1 addition & 1 deletion src/ArtemisNetCoreClient/Framing/CreateAddressMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ internal class CreateAddressMessage : Packet

public override void Encode(ByteBuffer buffer)
{
buffer.WriteStringAsBytes(Address);
buffer.WriteAmqString(Address);
buffer.WriteInt(RoutingTypes.Length);
foreach (var routingType in RoutingTypes)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal class SessionBindingQueryMessage : Packet

public override void Encode(ByteBuffer buffer)
{
buffer.WriteStringAsBytes(Address);
buffer.WriteAmqString(Address);
}

public override void Decode(ByteBuffer buffer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
public override bool IsResponse => true;

public bool Exists { get; set; }
public IReadOnlyList<string> QueueNames { get; set; }

Check warning on line 10 in src/ArtemisNetCoreClient/Framing/SessionBindingQueryResponseMessageV5.cs

View workflow job for this annotation

GitHub Actions / linux

Non-nullable property 'QueueNames' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 10 in src/ArtemisNetCoreClient/Framing/SessionBindingQueryResponseMessageV5.cs

View workflow job for this annotation

GitHub Actions / linux

Non-nullable property 'QueueNames' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public bool AutoCreateQueues { get; set; }
public bool AutoCreateAddresses { get; set; }
public bool DefaultPurgeOnNoConsumers { get; set; }
Expand All @@ -33,7 +33,7 @@
var queueNames = new string[numQueues];
for (int i = 0; i < numQueues; i++)
{
queueNames[i] = buffer.ReadStringAsBytes();
queueNames[i] = buffer.ReadAmqString();
}

QueueNames = queueNames;
Expand All @@ -43,7 +43,7 @@
DefaultMaxConsumers = buffer.ReadInt();
DefaultExclusive = buffer.ReadNullableBool();
DefaultLastValue = buffer.ReadNullableBool();
DefaultLastValueKey = buffer.ReadNullableStringAsBytes();
DefaultLastValueKey = buffer.ReadNullableAmqString();
DefaultNonDestructive = buffer.ReadNullableBool();
DefaultConsumersBeforeDispatch = buffer.ReadNullableInt();
DefaultDelayBeforeDispatch = buffer.ReadNullableLong();
Expand Down
28 changes: 28 additions & 0 deletions test/ArtemisNetCoreClient.Tests/ByteBufferTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -358,4 +358,32 @@ public void should_decode_nullable_string(byte[] encoded, string? expected)
// Assert
Assert.That(value, Is.EqualTo(expected));
}

[TestCase("abcdefgh", new byte[] { 1, 0, 0, 0, 16, 97, 0, 98, 0, 99, 0, 100, 0, 101, 0, 102, 0, 103, 0, 104, 0 })]
[TestCase(null, new byte[] { 0 })]
public void should_encode_nullable_amq_string(string value, byte[] encoded)
{
// Arrange
var byteBuffer = new ByteBuffer();

// Act
byteBuffer.WriteNullableAmqString(value);

// Assert
CollectionAssert.AreEqual(encoded, byteBuffer.GetBuffer().ToArray());
}

[TestCase(new byte[] { 1, 0, 0, 0, 16, 97, 0, 98, 0, 99, 0, 100, 0, 101, 0, 102, 0, 103, 0, 104, 0 }, "abcdefgh")]
[TestCase(new byte[] { 0 }, null)]
public void should_decode_nullable_amq_string(byte[] encoded, string? expected)
{
// Arrange
var byteBuffer = new ByteBuffer(encoded);

// Act
var value = byteBuffer.ReadNullableAmqString();

// Assert
Assert.That(value, Is.EqualTo(expected));
}
}
Loading