Browse Source

Enhance SplitPropertyPath to conditionally handle parentheses in prop… (#16054)

* Enhance SplitPropertyPath to conditionally handle parentheses in property paths

* added test for SplitPropertyPath
pull/16296/head
Abdella Solomon 2 years ago
committed by GitHub
parent
commit
ab02e25866
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 29
      src/Avalonia.Controls.DataGrid/Utils/ReflectionHelper.cs
  2. 20
      tests/Avalonia.Controls.DataGrid.UnitTests/Utils/ReflectionHelperTests.cs

29
src/Avalonia.Controls.DataGrid/Utils/ReflectionHelper.cs

@ -19,6 +19,8 @@ namespace Avalonia.Controls.Utils
internal const char LeftIndexerToken = '[';
internal const char PropertyNameSeparator = '.';
internal const char RightIndexerToken = ']';
internal const char LeftParenthesisToken = '(';
internal const char RightParenthesisToken = ')';
private static Type FindGenericType(Type definition, Type type)
{
@ -482,12 +484,35 @@ namespace Avalonia.Controls.Utils
List<string> propertyPaths = new List<string>();
if (!string.IsNullOrEmpty(propertyPath))
{
bool parenthesisOn = false;
int startIndex = 0;
for (int index = 0; index < propertyPath.Length; index++)
{
if (propertyPath[index] == PropertyNameSeparator)
if (parenthesisOn)
{
propertyPaths.Add(propertyPath.Substring(startIndex, index - startIndex));
if (propertyPath[index] == RightParenthesisToken)
{
parenthesisOn = false;
startIndex = index + 1;
}
continue;
}
if (propertyPath[index] == LeftParenthesisToken)
{
parenthesisOn = true;
if (startIndex != index)
{
propertyPaths.Add(propertyPath.Substring(startIndex, index - startIndex));
startIndex = index + 1;
}
}
else if (propertyPath[index] == PropertyNameSeparator)
{
if (startIndex != index)
{
propertyPaths.Add(propertyPath.Substring(startIndex, index - startIndex));
}
startIndex = index + 1;
}
else if (startIndex != index && propertyPath[index] == LeftIndexerToken)

20
tests/Avalonia.Controls.DataGrid.UnitTests/Utils/ReflectionHelperTests.cs

@ -0,0 +1,20 @@
using Avalonia.Controls.Utils;
using Xunit;
namespace Avalonia.Controls.DataGrid.UnitTests.Utils
{
public class ReflectionHelperTests
{
[Fact]
public void SplitPropertyPath_Splits_PropertyPath_With_Cast()
{
var path = "(Type).Property";
var expected = new [] { "Property" };
var result = TypeHelper.SplitPropertyPath(path);
Assert.Equal(expected, result);
}
}
}
Loading…
Cancel
Save