-
Notifications
You must be signed in to change notification settings - Fork 257
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
122 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System; | ||
using System.Text.Json; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Paramore.Brighter.MessageMappers; | ||
|
||
public class JsonMessageMapper<TRequest>(RequestContext? context) : IAmAMessageMapper<TRequest>, IAmAMessageMapperAsync<TRequest> where TRequest : class, IRequest | ||
{ | ||
public IRequestContext? Context { get; set; } = context; | ||
|
||
public Task<Message> MapToMessageAsync(TRequest request, Publication publication, | ||
CancellationToken cancellationToken = default) | ||
=> Task.FromResult(MapToMessage(request, publication)); | ||
|
||
public Task<TRequest> MapToRequestAsync(Message message, CancellationToken cancellationToken = default) | ||
=> Task.FromResult(MapToRequest(message)); | ||
|
||
public Message MapToMessage(TRequest request, Publication publication) | ||
{ | ||
MessageType messageType = request switch | ||
{ | ||
ICommand => MessageType.MT_COMMAND, | ||
IEvent => MessageType.MT_EVENT, | ||
_ => throw new ArgumentException(@"This message mapper can only map Commands and Events", nameof(request)) | ||
}; | ||
|
||
if(publication.Topic is null) | ||
throw new ArgumentException($"No Topic Defined for {publication}"); | ||
|
||
var header = new MessageHeader(messageId: request.Id, topic: publication.Topic, messageType: messageType); | ||
|
||
var body = new MessageBody(JsonSerializer.Serialize(request, JsonSerialisationOptions.Options)); | ||
var message = new Message(header, body); | ||
return message; | ||
} | ||
|
||
public TRequest MapToRequest(Message message) | ||
{ | ||
var request = JsonSerializer.Deserialize<TRequest>(message.Body.Value, JsonSerialisationOptions.Options); | ||
|
||
if (request is null) | ||
throw new ArgumentException($"Unable to deseralise message body for {message.Header.Topic}"); | ||
|
||
return request; | ||
} | ||
} |