2017-01-31 11:01:08 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
|
|
|
|
namespace Tapeti.Default
|
|
|
|
|
{
|
2019-08-14 10:20:53 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// IExchangeStrategy implementation which uses the first identifier in the namespace in lower case,
|
|
|
|
|
/// skipping the first identifier if it is 'Messaging'.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <example>
|
|
|
|
|
/// Messaging.Service.Optional.Further.Parts will result in the exchange name 'service'.
|
|
|
|
|
/// </example>
|
2017-01-31 11:01:08 +00:00
|
|
|
|
public class NamespaceMatchExchangeStrategy : IExchangeStrategy
|
|
|
|
|
{
|
2022-11-17 15:47:07 +00:00
|
|
|
|
private static readonly Regex NamespaceRegex = new("^(Messaging\\.)?(?<exchange>[^\\.]+)", RegexOptions.Compiled | RegexOptions.Singleline);
|
2017-01-31 11:01:08 +00:00
|
|
|
|
|
|
|
|
|
|
2019-08-14 10:20:53 +00:00
|
|
|
|
/// <inheritdoc />
|
2017-01-31 11:01:08 +00:00
|
|
|
|
public string GetExchange(Type messageType)
|
|
|
|
|
{
|
|
|
|
|
if (messageType.Namespace == null)
|
|
|
|
|
throw new ArgumentException($"{messageType.FullName} does not have a namespace");
|
|
|
|
|
|
2017-02-07 15:13:33 +00:00
|
|
|
|
var match = NamespaceRegex.Match(messageType.Namespace);
|
2017-01-31 11:01:08 +00:00
|
|
|
|
if (!match.Success)
|
|
|
|
|
throw new ArgumentException($"Namespace for {messageType.FullName} does not match the specified format");
|
|
|
|
|
|
2017-02-07 15:13:33 +00:00
|
|
|
|
return match.Groups["exchange"].Value.ToLower();
|
2017-01-31 11:01:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|