Fixed #8: Account for all-capital abbreviations in routing key strategy
This commit is contained in:
parent
8126549841
commit
cc7b0cd042
23
Tapet.Tests/Properties/AssemblyInfo.cs
Normal file
23
Tapet.Tests/Properties/AssemblyInfo.cs
Normal file
@ -0,0 +1,23 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
// set of attributes. Change these attribute values to modify the information
|
||||
// associated with an assembly.
|
||||
[assembly: AssemblyTitle("Tapet.Tests")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("Tapet.Tests")]
|
||||
[assembly: AssemblyCopyright("")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// Setting ComVisible to false makes the types in this assembly not visible
|
||||
// to COM components. If you need to access a type in this assembly from
|
||||
// COM, set the ComVisible attribute to true on that type.
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||
[assembly: Guid("ba6bc046-5e60-410a-ae84-be1d91561a96")]
|
80
Tapet.Tests/Tapet.Tests.csproj
Normal file
80
Tapet.Tests/Tapet.Tests.csproj
Normal file
@ -0,0 +1,80 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{BA6BC046-5E60-410A-AE84-BE1D91561A96}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>Tapet.Tests</RootNamespace>
|
||||
<AssemblyName>Tapet.Tests</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<TargetFrameworkProfile />
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Net.Http" />
|
||||
<Reference Include="System.Xml" />
|
||||
<Reference Include="xunit.abstractions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\xunit.abstractions.2.0.0\lib\net35\xunit.abstractions.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="xunit.assert, Version=2.1.0.3179, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\xunit.assert.2.1.0\lib\dotnet\xunit.assert.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="xunit.core, Version=2.1.0.3179, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\xunit.extensibility.core.2.1.0\lib\dotnet\xunit.core.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="xunit.execution.desktop, Version=2.1.0.3179, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\xunit.extensibility.execution.2.1.0\lib\net45\xunit.execution.desktop.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="TypeNameRoutingKeyStrategyTests.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Tapeti\Tapeti.csproj">
|
||||
<Project>{8ab4fd33-4aaa-465c-8579-9db3f3b23813}</Project>
|
||||
<Name>Tapeti</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
<Target Name="BeforeBuild">
|
||||
</Target>
|
||||
<Target Name="AfterBuild">
|
||||
</Target>
|
||||
-->
|
||||
</Project>
|
72
Tapet.Tests/TypeNameRoutingKeyStrategyTests.cs
Normal file
72
Tapet.Tests/TypeNameRoutingKeyStrategyTests.cs
Normal file
@ -0,0 +1,72 @@
|
||||
using System;
|
||||
using Tapeti.Default;
|
||||
using Xunit;
|
||||
|
||||
namespace Tapet.Tests
|
||||
{
|
||||
// ReSharper disable InconsistentNaming
|
||||
public class TypeNameRoutingKeyStrategyTests
|
||||
{
|
||||
private class Example { }
|
||||
|
||||
[Fact]
|
||||
public void Singleword()
|
||||
{
|
||||
AssertRoutingKey("example", typeof(Example));
|
||||
}
|
||||
|
||||
|
||||
private class ExampleMessage { }
|
||||
|
||||
[Fact]
|
||||
public void SinglewordMessagePostfix()
|
||||
{
|
||||
AssertRoutingKey("example", typeof(ExampleMessage));
|
||||
}
|
||||
|
||||
|
||||
private class ExampleMultiWord { }
|
||||
|
||||
[Fact]
|
||||
public void Multiword()
|
||||
{
|
||||
AssertRoutingKey("example.multi.word", typeof(ExampleMultiWord));
|
||||
}
|
||||
|
||||
|
||||
private class ExampleMultiWordMessage { }
|
||||
|
||||
[Fact]
|
||||
public void MultiwordMessagePostfix()
|
||||
{
|
||||
AssertRoutingKey("example.multi.word", typeof(ExampleMultiWordMessage));
|
||||
}
|
||||
|
||||
|
||||
private class ACRTestMessage { }
|
||||
|
||||
[Fact]
|
||||
public void Acronym()
|
||||
{
|
||||
AssertRoutingKey("acr.test", typeof(ACRTestMessage));
|
||||
}
|
||||
|
||||
|
||||
private class ACRTestMIXEDCaseMESSAGE { }
|
||||
|
||||
[Fact]
|
||||
public void MixedCasing()
|
||||
{
|
||||
AssertRoutingKey("acr.test.mixed.case", typeof(ACRTestMIXEDCaseMESSAGE));
|
||||
}
|
||||
|
||||
private void AssertRoutingKey(string expected, Type messageType)
|
||||
{
|
||||
if (expected == null) throw new ArgumentNullException(nameof(expected));
|
||||
if (messageType == null) throw new ArgumentNullException(nameof(messageType));
|
||||
|
||||
Assert.Equal(expected, new TypeNameRoutingKeyStrategy().GetRoutingKey(messageType));
|
||||
}
|
||||
}
|
||||
// ReSharper restore InconsistentNaming
|
||||
}
|
9
Tapet.Tests/packages.config
Normal file
9
Tapet.Tests/packages.config
Normal file
@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="xunit" version="2.1.0" targetFramework="net452" />
|
||||
<package id="xunit.abstractions" version="2.0.0" targetFramework="net452" />
|
||||
<package id="xunit.assert" version="2.1.0" targetFramework="net452" />
|
||||
<package id="xunit.core" version="2.1.0" targetFramework="net452" />
|
||||
<package id="xunit.extensibility.core" version="2.1.0" targetFramework="net452" />
|
||||
<package id="xunit.extensibility.execution" version="2.1.0" targetFramework="net452" />
|
||||
</packages>
|
@ -15,6 +15,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tapeti.Flow.SQL", "Tapeti.F
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tapeti.Annotations", "Tapeti.Annotations\Tapeti.Annotations.csproj", "{C4897D64-D04E-4AE9-BD98-D64295D1D13A}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tapet.Tests", "Tapet.Tests\Tapet.Tests.csproj", "{BA6BC046-5E60-410A-AE84-BE1D91561A96}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@ -45,6 +47,10 @@ Global
|
||||
{C4897D64-D04E-4AE9-BD98-D64295D1D13A}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{C4897D64-D04E-4AE9-BD98-D64295D1D13A}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{C4897D64-D04E-4AE9-BD98-D64295D1D13A}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{BA6BC046-5E60-410A-AE84-BE1D91561A96}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{BA6BC046-5E60-410A-AE84-BE1D91561A96}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{BA6BC046-5E60-410A-AE84-BE1D91561A96}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{BA6BC046-5E60-410A-AE84-BE1D91561A96}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
@ -2,58 +2,59 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Tapeti.Default
|
||||
{
|
||||
public class TypeNameRoutingKeyStrategy : IRoutingKeyStrategy
|
||||
{
|
||||
private readonly ConcurrentDictionary<Type, string> routingKeyCache = new ConcurrentDictionary<Type, string>();
|
||||
private const string SeparatorPattern = @"
|
||||
(?<!^) # Not start
|
||||
(
|
||||
# Digit, not preceded by another digit
|
||||
(?<!\d)\d
|
||||
|
|
||||
# Upper-case letter, followed by lower-case letter if
|
||||
# preceded by another upper-case letter, e.g. 'G' in HTMLGuide
|
||||
(?(?<=[A-Z])[A-Z](?=[a-z])|[A-Z])
|
||||
)";
|
||||
|
||||
private static readonly Regex SeparatorRegex = new Regex(SeparatorPattern, RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled);
|
||||
|
||||
private static readonly ConcurrentDictionary<Type, string> RoutingKeyCache = new ConcurrentDictionary<Type, string>();
|
||||
|
||||
|
||||
public string GetRoutingKey(Type messageType)
|
||||
{
|
||||
return routingKeyCache.GetOrAdd(messageType, BuildRoutingKey);
|
||||
return RoutingKeyCache.GetOrAdd(messageType, BuildRoutingKey);
|
||||
}
|
||||
|
||||
|
||||
protected virtual string BuildRoutingKey(Type messageType)
|
||||
{
|
||||
// Split PascalCase into dot-separated parts. If the class name ends in "Message" leave that out.
|
||||
var words = SplitUpperCase(messageType.Name);
|
||||
var words = SplitPascalCase(messageType.Name);
|
||||
if (words == null)
|
||||
return "";
|
||||
|
||||
if (words.Count > 1 && words.Last().Equals("Message", StringComparison.InvariantCulture))
|
||||
if (words.Count > 1 && words.Last().Equals("Message", StringComparison.InvariantCultureIgnoreCase))
|
||||
words.RemoveAt(words.Count - 1);
|
||||
|
||||
return string.Join(".", words.Select(s => s.ToLower()));
|
||||
}
|
||||
|
||||
|
||||
protected static List<string> SplitUpperCase(string source)
|
||||
private static List<string> SplitPascalCase(string value)
|
||||
{
|
||||
var words = new List<string>();
|
||||
var split = SeparatorRegex.Split(value);
|
||||
if (split.Length == 0)
|
||||
return null;
|
||||
|
||||
if (string.IsNullOrEmpty(source))
|
||||
return words;
|
||||
var result = new List<string>(split.Length - 1 / 2) { split[0] };
|
||||
for (var i = 1; i < split.Length; i += 2)
|
||||
result.Add(split[i] + split[i + 1]);
|
||||
|
||||
var wordStartIndex = 0;
|
||||
|
||||
var letters = source.ToCharArray();
|
||||
var previousChar = char.MinValue;
|
||||
|
||||
// Intentionally skip the first character
|
||||
for (var charIndex = 1; charIndex < letters.Length; charIndex++)
|
||||
{
|
||||
if (char.IsUpper(letters[charIndex]) && !char.IsWhiteSpace(previousChar))
|
||||
{
|
||||
words.Add(new string(letters, wordStartIndex, charIndex - wordStartIndex));
|
||||
wordStartIndex = charIndex;
|
||||
}
|
||||
|
||||
previousChar = letters[charIndex];
|
||||
}
|
||||
|
||||
words.Add(new string(letters, wordStartIndex, letters.Length - wordStartIndex));
|
||||
return words;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user