Skip to content

add map parse #405

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

Merged
merged 4 commits into from
Mar 22, 2025
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
22 changes: 17 additions & 5 deletions RabbitMQ.Stream.Client/AMQP/AmqpWireFormattingRead.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Buffers;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;

Expand All @@ -18,6 +19,7 @@ private static void PeekType(ref SequenceReader<byte> reader, out byte value)
{
reader.TryPeek(out value);
}

internal static int ReadType(ref SequenceReader<byte> reader, out byte value)
{
var read = WireFormatting.ReadByte(ref reader, out value);
Expand Down Expand Up @@ -154,12 +156,13 @@ internal static int ReadAny(ref SequenceReader<byte> reader, out object value)
case FormatCode.List32:
{
offset = ReadListHeader(ref reader, out var fields);
object list = null;
for (long i = 0; i < fields; i++)
{
offset += ReadAny(ref reader, out _);
offset += ReadAny(ref reader, out list);
}

value = null;
value = list;
return offset;
}

Expand All @@ -168,13 +171,22 @@ internal static int ReadAny(ref SequenceReader<byte> reader, out object value)
{
offset = ReadMapHeader(ref reader, out var count);
var values = count / 2;
Dictionary<string, object> map = new();
for (uint i = 0; i < values; i++)
{
offset += ReadAny(ref reader, out _);
offset += ReadAny(ref reader, out _);
offset += ReadAny(ref reader, out var v);
if (v is string key)
{
offset += ReadAny(ref reader, out var v2);
map[key] = v2;
}
else
{
offset += ReadAny(ref reader, out _);
}
}

value = null;
value = map;
return offset;
}
}
Expand Down
25 changes: 25 additions & 0 deletions Tests/Amqp10Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Buffers;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RabbitMQ.Stream.Client;
Expand Down Expand Up @@ -444,6 +445,30 @@ public void ValidateUuidMessagesFromGo()
Assert.Equal(uuid_value, uuid_message.Properties.CorrelationId);
}

[Fact]
public void ValidateAnnotationMap()
{
// shovel_annotations is a message with a map annotation
// coming from the Go client and the following configuration:
// source queue: "form"
// destination exchange: "to"
// queue bound to the exchange
// shovel from the source queue to the destination exchange
// the annotations will be added to the message

var buffer = SystemUtils.GetFileContent("shovel_annotations");
var reader = new SequenceReader<byte>(new ReadOnlySequence<byte>(buffer));
var shovelAnnotation = Message.From(ref reader, (uint)reader.Length);
Assert.NotNull(shovelAnnotation);
Assert.NotNull(shovelAnnotation.Annotations["x-shovelled"]);
var xShovelled = shovelAnnotation.Annotations["x-shovelled"] as Dictionary<string, object>;
Assert.NotNull(xShovelled);
Assert.Equal("hello-key", xShovelled["dest-exchange-key"]);
Assert.Equal("from", xShovelled["src-queue"]);
Assert.Equal("to", xShovelled["dest-exchange"]);
Assert.Equal("dynamic", xShovelled["shovel-type"]);
}

[Fact]
public void ValidateNilMessagesFromGo()
{
Expand Down
Binary file added Tests/Resources/shovel_annotations
Binary file not shown.