Skip to content
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
4 changes: 4 additions & 0 deletions docs/tutorials/interop.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ builder.UseWolverine(opts =>
mapper.MapProperty(x => x.ReplyUri!,
(e, msg) => e.ReplyUri = new Uri($"asb://queue/{msg.ReplyTo}"),
(e, msg) => msg.ReplyTo = "response");

// customize the incoming mapping
mapper.MapIncomingProperty(x => x.ReplyUri!,
(e, msg) => e.ReplyUri = new Uri($"asb://queue/{msg.ReplyTo}"));

});

Expand Down
62 changes: 62 additions & 0 deletions src/Testing/CoreTests/Transports/MapIncomingPropertyTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using Wolverine.Configuration;
using Wolverine.Runtime;
using Wolverine.Transports;
using Wolverine.Transports.Sending;
using Xunit;

namespace CoreTests.Transports;

public class MapIncomingPropertyTests
{
[Fact]
public void uses_the_custom_incoming_mapping()
{
var expectedReplyUri = new Uri("stub://test:123/");
var mapper = new StubEnvelopeMapper(new StubEndpoint());
mapper.MapIncomingProperty(x => x.ReplyUri!, (envelope, incoming) =>
{
envelope.ReplyUri = new Uri(incoming.SpecialReply!);
});

var envelope = new Envelope();
mapper.MapIncomingToEnvelope(envelope, new StubTransportMessage
{
SpecialReply = expectedReplyUri.ToString()
});

envelope.ReplyUri.ShouldBe(expectedReplyUri);
}
}

internal class StubTransportMessage
{
public Dictionary<string, string?> Headers { get; } = new();
public string? SpecialReply { get; set; }
}

internal class StubEnvelopeMapper(Endpoint endpoint)
: EnvelopeMapper<StubTransportMessage, StubTransportMessage>(endpoint)
{
protected override void writeOutgoingHeader(StubTransportMessage outgoing, string key, string value)
{
outgoing.Headers[key] = value;
}

protected override bool tryReadIncomingHeader(StubTransportMessage incoming, string key, out string? value)
{
return incoming.Headers.TryGetValue(key, out value);
}
}

internal class StubEndpoint() : Endpoint(new Uri("stub://mapper"), EndpointRole.Application)
{
public override ValueTask<IListener> BuildListenerAsync(IWolverineRuntime runtime, IReceiver receiver)
{
throw new NotImplementedException();
}

protected override ISender CreateSender(IWolverineRuntime runtime)
{
throw new NotImplementedException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,10 @@ public static async Task custom_mapping()
mapper.MapProperty(x => x.ReplyUri!,
(e, msg) => e.ReplyUri = new Uri($"asb://queue/{msg.ReplyTo}"),
(e, msg) => msg.ReplyTo = "response");

// customize the incoming mapping
mapper.MapIncomingProperty(x => x.ReplyUri!,
(e, msg) => e.ReplyUri = new Uri($"asb://queue/{msg.ReplyTo}"));

});

Expand Down Expand Up @@ -520,4 +524,4 @@ public static async Task nservicebus()
}
}

public interface IInterfaceMessage;
public interface IInterfaceMessage;
7 changes: 7 additions & 0 deletions src/Wolverine/Transports/EnvelopeMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,13 @@ public void MapProperty(Expression<Func<Envelope, object>> property, Action<Enve
_envelopeToOutgoing[prop] = writeToOutgoing;
}

public void MapIncomingProperty(Expression<Func<Envelope, object>> property,
Action<Envelope, TIncoming> readFromIncoming)
{
var prop = ReflectionHelper.GetProperty(property);
_incomingToEnvelope[prop] = readFromIncoming;
}

public void MapOutgoingProperty(Expression<Func<Envelope, object>> property,
Action<Envelope, TOutgoing> writeToOutgoing)
{
Expand Down
Loading