Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change the ObjectToTypeName value converter to give prettier C# names. (
#400)
- Loading branch information
Showing
5 changed files
with
162 additions
and
1 deletion.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
sources/presentation/Xenko.Core.Presentation.Tests/TestTypeExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright (c) Xenko contributors (https://xenko.com) | ||
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information. | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Xenko.Core.Presentation.Extensions; | ||
using Xunit; | ||
|
||
namespace Xenko.Core.Presentation.Tests | ||
{ | ||
public class TestTypeExtensions | ||
{ | ||
[Fact] | ||
public void TestTypeExtensionsToSimpleCSharpNameReturnsCorrectlyFormattedName() | ||
{ | ||
var tests = new List<(Type Type, string Name)>() | ||
{ | ||
(typeof(int), "int"), //Simple type | ||
(typeof(int?), "int?"), //Nullable simple type | ||
(typeof(TimeSpan), "TimeSpan"), //Type | ||
(typeof(TimeSpan?), "TimeSpan?"), //Nullable type | ||
(typeof(int[]), "int[]"), //Array of simple type | ||
(typeof(int?[]), "int?[]"), //Array of nullable simple type | ||
(typeof(TimeSpan[]), "TimeSpan[]"), //Array of type | ||
(typeof(TimeSpan?[]), "TimeSpan?[]"), //Array of nullable type | ||
(typeof(TimeSpan[,,]), "TimeSpan[,,]"), //Multi-dimesional array of type | ||
(typeof(Dictionary<string, FactAttribute>), "Dictionary<string, FactAttribute>"), //Generic type | ||
(typeof((string, FactAttribute)), "(string, FactAttribute)"), //Tuple types | ||
(typeof((string, FactAttribute)?), "(string, FactAttribute)?"), //Nullable tuple types | ||
(typeof(Dictionary<string, GenericStruct<int?>>), "Dictionary<string, GenericStruct<int?>>"), //Nested generic type | ||
(typeof((GenericStruct<int?>?, double)), "(GenericStruct<int?>?, double)"), //Crazy type | ||
}; | ||
|
||
foreach (var item in tests) | ||
{ | ||
Assert.Equal(item.Type.ToSimpleCSharpName(), item.Name); | ||
} | ||
} | ||
|
||
private struct GenericStruct<T> | ||
{ | ||
public T Field; | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
sources/presentation/Xenko.Core.Presentation.Tests/TestValueConverters.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright (c) Xenko contributors (https://xenko.com) | ||
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information. | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Xenko.Core.Presentation.ValueConverters; | ||
using Xunit; | ||
|
||
namespace Xenko.Core.Presentation.Tests | ||
{ | ||
public class TestValueConverters | ||
{ | ||
[Fact] | ||
public void TestObjectToTypeNameConvertsNullToNone() | ||
{ | ||
var converter = new ObjectToTypeName(); | ||
|
||
Assert.Equal(converter.Convert(null, typeof(string), null, CultureInfo.CurrentCulture), ObjectToTypeName.NullObjectType); | ||
} | ||
|
||
[Fact] | ||
public void TestObjectToTypeNameConverterValueToType() | ||
{ | ||
var converter = new ObjectToTypeName(); | ||
|
||
Assert.NotEqual(converter.Convert("hello", typeof(string), null, CultureInfo.CurrentCulture), ObjectToTypeName.NullObjectType); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
sources/presentation/Xenko.Core.Presentation/Extensions/TypeExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright (c) Xenko contributors (https://xenko.com) | ||
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information. | ||
using System; | ||
using System.CodeDom; | ||
using Microsoft.CSharp; | ||
using System.Linq; | ||
using System.Collections.Generic; | ||
|
||
namespace Xenko.Core.Presentation.Extensions | ||
{ | ||
/// <summary> | ||
/// Helper class to get formatted names from <see cref="Type"/>. | ||
/// </summary> | ||
internal static class TypeExtensions | ||
{ | ||
private static readonly CSharpCodeProvider codeProvider = new CSharpCodeProvider(); | ||
private static readonly HashSet<Type> valueTupleTypes = new HashSet<Type> | ||
{ | ||
typeof(ValueTuple<>), | ||
typeof(ValueTuple<,>), | ||
typeof(ValueTuple<,,>), | ||
typeof(ValueTuple<,,,>), | ||
typeof(ValueTuple<,,,,>), | ||
typeof(ValueTuple<,,,,,>), | ||
typeof(ValueTuple<,,,,,,>), | ||
typeof(ValueTuple<,,,,,,,>), | ||
}; | ||
|
||
private static bool IsValueTuple(Type type) => | ||
type.IsGenericType && | ||
valueTupleTypes.Contains(type.GetGenericTypeDefinition()); | ||
|
||
/// <summary> | ||
/// Gets C# syntax like type declaration from <see cref="Type"/> | ||
/// </summary> | ||
/// <param name="type">The type to get the name from.</param> | ||
/// <returns>C# syntax like type declaration from the type provided</returns> | ||
/// <exception cref="ArgumentNullException">If type parameter is null.</exception> | ||
public static string ToSimpleCSharpName(this Type type) | ||
{ | ||
if (type == null) | ||
{ | ||
throw new ArgumentNullException(nameof(type)); | ||
} | ||
|
||
if (type.IsArray) | ||
{ | ||
return ToSimpleCSharpName(type.GetElementType()) + "[" + new string(',', type.GetArrayRank() - 1) + "]"; | ||
} | ||
|
||
if (!type.IsGenericType) | ||
{ | ||
//Use a CSharpCodeProvider to handle conversions like 'Int32' to 'int' | ||
var fullTypeName = codeProvider.GetTypeOutput(new CodeTypeReference(type)); | ||
|
||
var simpleNameStart = fullTypeName.LastIndexOf('.') + 1; | ||
|
||
if (simpleNameStart >= fullTypeName.Length) | ||
return fullTypeName; | ||
|
||
return fullTypeName.Substring(simpleNameStart); | ||
} | ||
|
||
if (Nullable.GetUnderlyingType(type) is Type nullableType) | ||
{ | ||
return ToSimpleCSharpName(nullableType) + "?"; | ||
} | ||
|
||
var genericParameters = string.Join(", ", type.GetGenericArguments().Select(ToSimpleCSharpName)); | ||
|
||
if (IsValueTuple(type)) | ||
{ | ||
return "(" + genericParameters + ")"; | ||
} | ||
|
||
return type.Name.Substring(0, type.Name.LastIndexOf('`')) + "<" + genericParameters + ">"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters