Skip to content

Commit 830e540

Browse files
authored
add map parse (#405)
* add map parse for annotations * Fixes: #371 Signed-off-by: Gabriele Santomaggio <G.santomaggio@gmail.com> --------- Signed-off-by: Gabriele Santomaggio <G.santomaggio@gmail.com>
1 parent cb3fa11 commit 830e540

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

RabbitMQ.Stream.Client/AMQP/AmqpWireFormattingRead.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using System;
66
using System.Buffers;
7+
using System.Collections.Generic;
78
using System.Runtime.CompilerServices;
89
using System.Text;
910

@@ -18,6 +19,7 @@ private static void PeekType(ref SequenceReader<byte> reader, out byte value)
1819
{
1920
reader.TryPeek(out value);
2021
}
22+
2123
internal static int ReadType(ref SequenceReader<byte> reader, out byte value)
2224
{
2325
var read = WireFormatting.ReadByte(ref reader, out value);
@@ -154,12 +156,13 @@ internal static int ReadAny(ref SequenceReader<byte> reader, out object value)
154156
case FormatCode.List32:
155157
{
156158
offset = ReadListHeader(ref reader, out var fields);
159+
object list = null;
157160
for (long i = 0; i < fields; i++)
158161
{
159-
offset += ReadAny(ref reader, out _);
162+
offset += ReadAny(ref reader, out list);
160163
}
161164

162-
value = null;
165+
value = list;
163166
return offset;
164167
}
165168

@@ -168,13 +171,22 @@ internal static int ReadAny(ref SequenceReader<byte> reader, out object value)
168171
{
169172
offset = ReadMapHeader(ref reader, out var count);
170173
var values = count / 2;
174+
Dictionary<string, object> map = new();
171175
for (uint i = 0; i < values; i++)
172176
{
173-
offset += ReadAny(ref reader, out _);
174-
offset += ReadAny(ref reader, out _);
177+
offset += ReadAny(ref reader, out var v);
178+
if (v is string key)
179+
{
180+
offset += ReadAny(ref reader, out var v2);
181+
map[key] = v2;
182+
}
183+
else
184+
{
185+
offset += ReadAny(ref reader, out _);
186+
}
175187
}
176188

177-
value = null;
189+
value = map;
178190
return offset;
179191
}
180192
}

Tests/Amqp10Tests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using System;
66
using System.Buffers;
7+
using System.Collections.Generic;
78
using System.Linq;
89
using System.Text;
910
using RabbitMQ.Stream.Client;
@@ -444,6 +445,30 @@ public void ValidateUuidMessagesFromGo()
444445
Assert.Equal(uuid_value, uuid_message.Properties.CorrelationId);
445446
}
446447

448+
[Fact]
449+
public void ValidateAnnotationMap()
450+
{
451+
// shovel_annotations is a message with a map annotation
452+
// coming from the Go client and the following configuration:
453+
// source queue: "form"
454+
// destination exchange: "to"
455+
// queue bound to the exchange
456+
// shovel from the source queue to the destination exchange
457+
// the annotations will be added to the message
458+
459+
var buffer = SystemUtils.GetFileContent("shovel_annotations");
460+
var reader = new SequenceReader<byte>(new ReadOnlySequence<byte>(buffer));
461+
var shovelAnnotation = Message.From(ref reader, (uint)reader.Length);
462+
Assert.NotNull(shovelAnnotation);
463+
Assert.NotNull(shovelAnnotation.Annotations["x-shovelled"]);
464+
var xShovelled = shovelAnnotation.Annotations["x-shovelled"] as Dictionary<string, object>;
465+
Assert.NotNull(xShovelled);
466+
Assert.Equal("hello-key", xShovelled["dest-exchange-key"]);
467+
Assert.Equal("from", xShovelled["src-queue"]);
468+
Assert.Equal("to", xShovelled["dest-exchange"]);
469+
Assert.Equal("dynamic", xShovelled["shovel-type"]);
470+
}
471+
447472
[Fact]
448473
public void ValidateNilMessagesFromGo()
449474
{

Tests/Resources/shovel_annotations

312 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)