Skip to content

Commit

Permalink
add raw json sqs envelope mapper for incoming messages
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian.weber authored and jeremydmiller committed Sep 18, 2023
1 parent 452d0ee commit 1eee4a5
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"profiles": {
"DiagnosticsApp": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:55166;http://localhost:55167"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"profiles": {
"TracingTests": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:55164;http://localhost:55165"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System.Text.Json;
using Amazon.SQS.Model;
using Wolverine.AmazonSqs.Internal;
using Wolverine.Util;

namespace Wolverine.AmazonSqs.Tests
{
public class RawJsonSqsEnvelopeMapperTests
{
[Fact]
public void deserializes_json_message_with_specified_type()
{
var sut = new RawJsonSqsEnvelopeMapper(typeof(TextDetected), new JsonSerializerOptions());

string body = @"{
""JobId"": ""fe0ffd41549670f4190cd4c07c94141aa9cb79519a23056721857e3157cd8bf1"",
""Status"": ""SUCCEEDED"",
""API"": ""StartDocumentTextDetection"",
""Timestamp"": 1692958398261,
""DocumentLocation"": {
""S3ObjectName"": ""my.pdf"",
""S3Bucket"": ""mybucket""
}
}";
Message nativeSqsMessage = new Message
{
Body = body,
};

AmazonSqsEnvelope envelope = new AmazonSqsEnvelope(nativeSqsMessage);

sut.ReadEnvelopeData(envelope, nativeSqsMessage.Body, nativeSqsMessage.MessageAttributes);

Assert.Equal(typeof(TextDetected).ToMessageTypeName(), envelope.MessageType);
}

public class TextDetected
{
public string JobId { get; set; } = string.Empty;

public string JobTag { get; set; } = string.Empty;

public string Status { get; set; } = string.Empty;

public DocumentLocation DocumentLocation { get; set; }

public long Timestamp { get; set; } = -1;

public string Api { get; set; } = string.Empty;
}

public class DocumentLocation
{
public string S3ObjectName { get; set; } = string.Empty;

public string S3Bucket { get; set; } = string.Empty;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Text.Json;
using Amazon.SQS.Model;
using Wolverine.AmazonSqs.Internal;
using Wolverine.Configuration;
Expand Down Expand Up @@ -74,4 +75,20 @@ public AmazonSqsListenerConfiguration ConfigureQueueCreation(Action<CreateQueueR
add(e => configure(e.Configuration));
return this;
}

public AmazonSqsListenerConfiguration ReceiveNativeJsonMessage(
Type messageType,
Action<JsonSerializerOptions>? configure = null)
{
add(e =>
{
JsonSerializerOptions serializerOptions = new JsonSerializerOptions();

configure?.Invoke(serializerOptions);

e.Mapper = new RawJsonSqsEnvelopeMapper(messageType, serializerOptions);
});

return this;
}
}
4 changes: 2 additions & 2 deletions src/Transports/AWS/Wolverine.AmazonSqs/ISqsEnvelopeMapper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Amazon.SQS.Model;
using Amazon.SQS.Model;
using Wolverine.Runtime.Serialization;
using Wolverine.Transports;

Expand Down Expand Up @@ -26,7 +26,7 @@ public string BuildMessageBody(Envelope envelope)
public IEnumerable<KeyValuePair<string, MessageAttributeValue>> ToAttributes(Envelope envelope)
{
yield return new KeyValuePair<string, MessageAttributeValue>(TransportConstants.ProtocolVersion,
new MessageAttributeValue { StringValue = "1.0", DataType = "String"});
new MessageAttributeValue { StringValue = "1.0", DataType = "String" });
}

public void ReadEnvelopeData(Envelope envelope, string messageBody,
Expand Down
42 changes: 42 additions & 0 deletions src/Transports/AWS/Wolverine.AmazonSqs/RawJsonSqsEnvelopeMapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Text.Json;
using Amazon.SQS.Model;
using Wolverine.Transports;
using Wolverine.Util;

namespace Wolverine.AmazonSqs;

internal class RawJsonSqsEnvelopeMapper : ISqsEnvelopeMapper
{
private readonly Type _defaultMessageType;
private readonly JsonSerializerOptions _serializerOptions;

public RawJsonSqsEnvelopeMapper(Type defaultMessageType, JsonSerializerOptions serializerOptions)
{
_defaultMessageType = defaultMessageType;
_serializerOptions = serializerOptions;
}

public string BuildMessageBody(Envelope envelope)
{
return JsonSerializer.Serialize(
envelope.Message,
_defaultMessageType,
_serializerOptions);
}

public IEnumerable<KeyValuePair<string, MessageAttributeValue>> ToAttributes(Envelope envelope)
{
yield return new KeyValuePair<string, MessageAttributeValue>(TransportConstants.ProtocolVersion,
new MessageAttributeValue { StringValue = "1.0", DataType = "String" });
}

public void ReadEnvelopeData(Envelope envelope, string messageBody, IDictionary<string, MessageAttributeValue> attributes)
{
// assuming json serialized message
envelope.MessageType = _defaultMessageType.ToMessageTypeName();
envelope.Message = JsonSerializer.Deserialize(
messageBody,
_defaultMessageType,
_serializerOptions);
}
}

0 comments on commit 1eee4a5

Please sign in to comment.