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

32943-Sugar for HierarchyId path generation #33062

Merged
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
32 changes: 31 additions & 1 deletion src/EFCore.SqlServer.Abstractions/HierarchyId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.CodeAnalysis;
using System.Text;
using System.Text.Json.Serialization;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.SqlServer.Types;
Expand All @@ -25,7 +26,7 @@ public HierarchyId()
}

/// <summary>
/// Initializes a new instance of the<see cref="HierarchyId" /> class. Equivalent to <see cref="Parse" />.
/// Initializes a new instance of the<see cref="HierarchyId" /> class. Equivalent to <see cref="Parse(string?)" />.
/// </summary>
/// <param name="value">The string representation of the node.</param>
public HierarchyId(string value)
Expand Down Expand Up @@ -63,6 +64,35 @@ public static HierarchyId GetRoot()
public static HierarchyId? Parse(string? input)
=> (HierarchyId?)SqlHierarchyId.Parse(input);

/// <summary>
/// Converts the <paramref name= "parentHierarchyId" /> and <paramref name= "parentId" /> of a node to a <see cref="HierarchyId" /> value.
/// </summary>
/// <param name="parentHierarchyId">The parent HierarchyId of node.</param>
/// <param name="parentId">The parent Id of current node. It can be more than one element if want have path like: "/1/2/3.1/", otherwise one element for have path like: "/1/2/3/".</param>
/// <returns>A <see cref="HierarchyId" /> value.</returns>
public static HierarchyId Parse(HierarchyId parentHierarchyId , params int[] parentId)
=> GenerateHierarchyIdBasedOnParent(parentHierarchyId, parentId);

//This Method can move to "SqlHierarchyId in Microsoft.SqlServer.Types", if we don't want put it in this abstraction.
private static HierarchyId GenerateHierarchyIdBasedOnParent(HierarchyId parent, params int[] parentId)
{
if (parent is null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add curlies here. In general, try to follow the formatting of other cs files in the project.

Copy link
Contributor Author

@Rezakazemi890 Rezakazemi890 Feb 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved in This Commit.

{
return HierarchyId.GetRoot();
}

if (parentId.Length < 1)
{
return parent;
}

var specificPath = new StringBuilder(parent.ToString());
specificPath.Append(string.Join(".", parentId));
specificPath.Append('/');

return HierarchyId.Parse(specificPath.ToString());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this can ever return null, right? In which case the return type here should be non-nullable.

Copy link
Contributor Author

@Rezakazemi890 Rezakazemi890 Feb 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you mean this method will never have a null output, it is correct. I Modified the new Parse overload output type in This commit

Also, the main overload of the Parse method that used, will not have a null output if it has a non-null input.

    [return: NotNullIfNotNull(nameof(input))]
    public static HierarchyId? Parse(string? input)
        => (HierarchyId?)SqlHierarchyId.Parse(input);

}

/// <inheritdoc />
public virtual int CompareTo(HierarchyId? other)
=> _value.CompareTo((SqlHierarchyId)other);
Expand Down
26 changes: 26 additions & 0 deletions test/EFCore.SqlServer.HierarchyId.Tests/WrapperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,30 @@ public void GetReparentedValue_returns_null_when_newRoot_is_null()
[ConditionalFact]
public void IsDescendantOf_returns_false_when_parent_is_null()
=> Assert.False(HierarchyId.Parse("/1/").IsDescendantOf(null));

[ConditionalFact]
public void Parse_overloads_works_when_parentId_is_simpleId()
=> Assert.Equal(HierarchyId.Parse(_parent, 2), HierarchyId.Parse("/1/2/"));

[ConditionalFact]
public void Parse_overloads_works_when_parentId_is_dottedString()
=> Assert.Equal(HierarchyId.Parse(_parent, 2,1), HierarchyId.Parse("/1/2.1/"));

[ConditionalFact]
public void Parse_overloads_works_when_parentId_is_empty()
=> Assert.Equal(HierarchyId.Parse(_parent), HierarchyId.Parse("/1/"));

[ConditionalFact]
public void Parse_overloads_works_when_parentHierarchy_is_root_and_parentId_is_simple()
=> Assert.Equal(HierarchyId.Parse(HierarchyId.GetRoot(),1), HierarchyId.Parse("/1/"));

[ConditionalFact]
public void Parse_overloads_works_when_parentHierarchy_is_root_and_parentId_is_empty()
=> Assert.Equal(HierarchyId.Parse(HierarchyId.GetRoot()), HierarchyId.Parse("/"));

[ConditionalFact]
public void Parse_overloads_works_when_parentHierarchy_is_null_and_parentId_is_empty()
=> Assert.Equal(HierarchyId.Parse(null,[]), HierarchyId.Parse("/"));

private readonly HierarchyId _parent = HierarchyId.Parse("/1/");
}
Loading