Skip to content

Commit

Permalink
fix: improve parsing of AssemblyQualifiedName (#233)
Browse files Browse the repository at this point in the history
Co-authored-by: biedermannn <nicolas.biedermann@post.ch>
  • Loading branch information
2 people authored and BEagle1984 committed Jul 16, 2024
1 parent 839506e commit aece732
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
35 changes: 25 additions & 10 deletions src/Silverback.Core/Util/TypesCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,31 @@ internal static class TypesCache
return type;
}

internal static string CleanAssemblyQualifiedName(string typeAssemblyQualifiedName)
{
if (string.IsNullOrEmpty(typeAssemblyQualifiedName))
return typeAssemblyQualifiedName;

int endGenericType = typeAssemblyQualifiedName.LastIndexOf(']');
if (endGenericType == -1)
{
string[] split = typeAssemblyQualifiedName.Split(',', 3, StringSplitOptions.RemoveEmptyEntries);
return split.Length >= 2 ? $"{split[0].Trim()}, {split[1].Trim()}" : typeAssemblyQualifiedName;
}

int startGenericType = typeAssemblyQualifiedName.IndexOf('[', StringComparison.InvariantCulture);
if (startGenericType == -1)
return typeAssemblyQualifiedName;

string type = typeAssemblyQualifiedName[..startGenericType].Trim();
if (endGenericType + 1 >= typeAssemblyQualifiedName.Length)
return type;

string next = typeAssemblyQualifiedName[(endGenericType + 1)..];
string assemblyName = next.Split(",", 2, StringSplitOptions.RemoveEmptyEntries)[0].Trim();
return $"{type}, {assemblyName}";
}

[SuppressMessage("", "CA1031", Justification = "Can catch all, the operation is retried")]
private static Type? ResolveType(string typeName, bool throwOnError)
{
Expand All @@ -48,15 +73,5 @@ internal static class TypesCache

return type;
}

private static string CleanAssemblyQualifiedName(string typeAssemblyQualifiedName)
{
if (string.IsNullOrEmpty(typeAssemblyQualifiedName))
return typeAssemblyQualifiedName;

var split = typeAssemblyQualifiedName.Split(',');

return split.Length >= 2 ? $"{split[0]}, {split[1]}" : typeAssemblyQualifiedName;
}
}
}
13 changes: 13 additions & 0 deletions tests/Silverback.Core.Tests/Util/TypesCacheTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,18 @@ public void GetType_IncompleteTypeName_TypeReturned()

type.Should().Be(typeof(TestEventOne));
}

[Theory]
[InlineData("Silverback.Tests.Core.TestTypes.Messages.TestEventOne2", "Silverback.Tests.Core.TestTypes.Messages.TestEventOne2")]
[InlineData("Silverback.Tests.Core.TestTypes.Messages.TestEventOne2, Silverback.Core.Tests", "Silverback.Tests.Core.TestTypes.Messages.TestEventOne2, Silverback.Core.Tests")]
[InlineData("Silverback.Tests.Core.TestTypes.Messages.TestEventOne2, Silverback.Core.Tests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", "Silverback.Tests.Core.TestTypes.Messages.TestEventOne2, Silverback.Core.Tests")]
[InlineData("Silverback.Tests.Core.Util.TypesCacheTests+MyMessage`2[[System.Int32, System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Int64, System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], Silverback.Core.Tests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", "Silverback.Tests.Core.Util.TypesCacheTests+MyMessage`2, Silverback.Core.Tests")]
[InlineData("Silverback.Tests.Core.Util.TypesCacheTests+MyMessage`2[[System.Int32, System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Int64, System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]", "Silverback.Tests.Core.Util.TypesCacheTests+MyMessage`2")]
public void GetType_GenericType_TypeReturned(string typeAssemblyQualifiedName, string expected)
{
string cleanedName = TypesCache.CleanAssemblyQualifiedName(typeAssemblyQualifiedName);

cleanedName.Should().Be(expected);
}
}
}

0 comments on commit aece732

Please sign in to comment.