Skip to content

Commit

Permalink
Enhance SplitPropertyPath to conditionally handle parentheses in prop… (
Browse files Browse the repository at this point in the history
#16054)

* Enhance SplitPropertyPath to conditionally handle parentheses in property paths

* added test for SplitPropertyPath
  • Loading branch information
Abdesol authored Jul 11, 2024
1 parent 56108c1 commit ab02e25
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/Avalonia.Controls.DataGrid/Utils/ReflectionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ internal static class TypeHelper
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)
{
Expand Down Expand Up @@ -482,12 +484,35 @@ internal static List<string> SplitPropertyPath(string propertyPath)
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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
}

0 comments on commit ab02e25

Please sign in to comment.