Skip to content

Commit

Permalink
(pardahlman#65) Serializes Message Type without assembly version etc
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcus Utter committed Mar 3, 2016
1 parent 5f54329 commit d7ab8fc
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/RawRabbit/Common/BasicPropertiesProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,31 @@ public IBasicProperties GetProperties<TMessage>(Action<IBasicProperties> custom
MessageId = Guid.NewGuid().ToString(),
Headers = new Dictionary<string, object>(),
Persistent = _config.PersistentDeliveryMode,
Type = typeof(TMessage).FullName
Type = GetTypeName(typeof(TMessage))
};
custom?.Invoke(properties);
properties.Headers.Add(PropertyHeaders.Sent, DateTime.UtcNow.ToString("u"));
return properties;
}

public string GetTypeName(Type type)
{
var name = $"{type.Namespace}.{type.Name}";
if (type.GenericTypeArguments.Length > 0)
{
var shouldInsertComma = false;
name += '[';
foreach (var genericType in type.GenericTypeArguments)
{
if (shouldInsertComma)
name += ",";
name += $"[{GetTypeName(genericType)}]";
shouldInsertComma = true;
}
name += ']';
}
name += $", {type.Assembly.GetName().Name}";
return name;
}
}
}
50 changes: 50 additions & 0 deletions test/RawRabbit.Tests/Common/BasicPropertiesProviderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using Xunit;
using RawRabbit.Common;
using RawRabbit.Configuration;

namespace RawRabbit.Tests.Common
{
public class BasicPropertiesProviderTests
{
[Fact]
public void Should_Be_Able_To_Get_Type_Property_For_Basic_Type()
{
/* Setup */
var provider = new BasicPropertiesProvider(new RawRabbitConfiguration());

/* Test */
var type = provider.GetProperties<First>().Type;

/* Assert */
Assert.Equal(expected: "RawRabbit.Tests.Common.First, RawRabbit.Tests", actual: type);
}

[Fact]
public void Should_Be_Able_To_Get_Type_Property_For_Type_With_Generic_Type_Arguments()
{
/* Setup */
var provider = new BasicPropertiesProvider(new RawRabbitConfiguration());

/* Test */
var type = provider.GetProperties<Generic<First, Second>>().Type;

/* Assert */
Assert.Equal(expected: "RawRabbit.Tests.Common.Generic`2[[RawRabbit.Tests.Common.First, RawRabbit.Tests],[RawRabbit.Tests.Common.Second, RawRabbit.Tests]], RawRabbit.Tests", actual: type);
}

private class Generic<TFirst, TSecond>
{
public TFirst First;
public TSecond Second;
}

private class First
{
}

private class Second
{
}

}
}

0 comments on commit d7ab8fc

Please sign in to comment.