Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance SplitPropertyPath to conditionally handle parentheses in prop… #16054

Merged
merged 2 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}
Loading