using System; using System.Reflection; using Tapeti.Annotations; namespace Tapeti.Helpers { /// /// Helper class for compositing a routing key with support for the RoutingKey attribute. /// Should be used by all implementations of IRoutingKeyStrategy unless there is a good reason not to. /// public static class RoutingKeyHelper { /// /// Applies the RoutingKey attribute for the specified messageClass. /// /// /// Called when the strategy needs to be applied to the message class to generate a routing key. /// Will not be called if the Full property is specified on the RoutingKey attribute. public static string Decorate(Type messageType, Func applyStrategy) { var routingKeyAttribute = messageType.GetCustomAttribute(); if (routingKeyAttribute == null) return applyStrategy(); if (!string.IsNullOrEmpty(routingKeyAttribute.Full)) return routingKeyAttribute.Full; return (routingKeyAttribute.Prefix ?? "") + applyStrategy() + (routingKeyAttribute.Postfix ?? ""); } } }