feat: execute full-repo remaining parity closure plan
This commit is contained in:
30
src/NATS.Server/LeafNodes/LeafHubSpokeMapper.cs
Normal file
30
src/NATS.Server/LeafNodes/LeafHubSpokeMapper.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
namespace NATS.Server.LeafNodes;
|
||||
|
||||
public enum LeafMapDirection
|
||||
{
|
||||
Inbound,
|
||||
Outbound,
|
||||
}
|
||||
|
||||
public sealed record LeafMappingResult(string Account, string Subject);
|
||||
|
||||
public sealed class LeafHubSpokeMapper
|
||||
{
|
||||
private readonly IReadOnlyDictionary<string, string> _hubToSpoke;
|
||||
private readonly IReadOnlyDictionary<string, string> _spokeToHub;
|
||||
|
||||
public LeafHubSpokeMapper(IReadOnlyDictionary<string, string> hubToSpoke)
|
||||
{
|
||||
_hubToSpoke = hubToSpoke;
|
||||
_spokeToHub = hubToSpoke.ToDictionary(static p => p.Value, static p => p.Key, StringComparer.Ordinal);
|
||||
}
|
||||
|
||||
public LeafMappingResult Map(string account, string subject, LeafMapDirection direction)
|
||||
{
|
||||
if (direction == LeafMapDirection.Outbound && _hubToSpoke.TryGetValue(account, out var spoke))
|
||||
return new LeafMappingResult(spoke, subject);
|
||||
if (direction == LeafMapDirection.Inbound && _spokeToHub.TryGetValue(account, out var hub))
|
||||
return new LeafMappingResult(hub, subject);
|
||||
return new LeafMappingResult(account, subject);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user