Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 13ee42c

Browse files
32943-Sugar for HierarchyId path generation (#33062)
* feat: add Parse method with parent parameter overload to HierarchyId abstraction * refactor: refactor and clean new parse overload * test: add test for parse overloads * test: separate and refactor parse tests * fix: #32943-resolve [review comment](#33062 (comment)) * fix: #32943 - resolve [review comment](#33062 (comment)) * fix: #32943 - resolve [review comment](#33062 (comment)) * refactor: #32943 - replace IReadOnlyList to params for easier use * fix: #32943 - resolve [review comment](#33062 (comment))
1 parent 0927dea commit 13ee42c

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

src/EFCore.SqlServer.Abstractions/HierarchyId.cs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Diagnostics.CodeAnalysis;
5+
using System.Text;
56
using System.Text.Json.Serialization;
67
using Microsoft.EntityFrameworkCore.Internal;
78
using Microsoft.SqlServer.Types;
@@ -25,7 +26,7 @@ public HierarchyId()
2526
}
2627

2728
/// <summary>
28-
/// Initializes a new instance of the<see cref="HierarchyId" /> class. Equivalent to <see cref="Parse" />.
29+
/// Initializes a new instance of the<see cref="HierarchyId" /> class. Equivalent to <see cref="Parse(string?)" />.
2930
/// </summary>
3031
/// <param name="value">The string representation of the node.</param>
3132
public HierarchyId(string value)
@@ -63,6 +64,35 @@ public static HierarchyId GetRoot()
6364
public static HierarchyId? Parse(string? input)
6465
=> (HierarchyId?)SqlHierarchyId.Parse(input);
6566

67+
/// <summary>
68+
/// Converts the <paramref name= "parentHierarchyId" /> and <paramref name= "parentId" /> of a node to a <see cref="HierarchyId" /> value.
69+
/// </summary>
70+
/// <param name="parentHierarchyId">The parent HierarchyId of node.</param>
71+
/// <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>
72+
/// <returns>A <see cref="HierarchyId" /> value.</returns>
73+
public static HierarchyId Parse(HierarchyId parentHierarchyId , params int[] parentId)
74+
=> GenerateHierarchyIdBasedOnParent(parentHierarchyId, parentId);
75+
76+
//This Method can move to "SqlHierarchyId in Microsoft.SqlServer.Types", if we don't want put it in this abstraction.
77+
private static HierarchyId GenerateHierarchyIdBasedOnParent(HierarchyId parent, params int[] parentId)
78+
{
79+
if (parent is null)
80+
{
81+
return HierarchyId.GetRoot();
82+
}
83+
84+
if (parentId.Length < 1)
85+
{
86+
return parent;
87+
}
88+
89+
var specificPath = new StringBuilder(parent.ToString());
90+
specificPath.Append(string.Join(".", parentId));
91+
specificPath.Append('/');
92+
93+
return HierarchyId.Parse(specificPath.ToString());
94+
}
95+
6696
/// <inheritdoc />
6797
public virtual int CompareTo(HierarchyId? other)
6898
=> _value.CompareTo((SqlHierarchyId)other);

test/EFCore.SqlServer.HierarchyId.Tests/WrapperTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,30 @@ public void GetReparentedValue_returns_null_when_newRoot_is_null()
3535
[ConditionalFact]
3636
public void IsDescendantOf_returns_false_when_parent_is_null()
3737
=> Assert.False(HierarchyId.Parse("/1/").IsDescendantOf(null));
38+
39+
[ConditionalFact]
40+
public void Parse_overloads_works_when_parentId_is_simpleId()
41+
=> Assert.Equal(HierarchyId.Parse(_parent, 2), HierarchyId.Parse("/1/2/"));
42+
43+
[ConditionalFact]
44+
public void Parse_overloads_works_when_parentId_is_dottedString()
45+
=> Assert.Equal(HierarchyId.Parse(_parent, 2,1), HierarchyId.Parse("/1/2.1/"));
46+
47+
[ConditionalFact]
48+
public void Parse_overloads_works_when_parentId_is_empty()
49+
=> Assert.Equal(HierarchyId.Parse(_parent), HierarchyId.Parse("/1/"));
50+
51+
[ConditionalFact]
52+
public void Parse_overloads_works_when_parentHierarchy_is_root_and_parentId_is_simple()
53+
=> Assert.Equal(HierarchyId.Parse(HierarchyId.GetRoot(),1), HierarchyId.Parse("/1/"));
54+
55+
[ConditionalFact]
56+
public void Parse_overloads_works_when_parentHierarchy_is_root_and_parentId_is_empty()
57+
=> Assert.Equal(HierarchyId.Parse(HierarchyId.GetRoot()), HierarchyId.Parse("/"));
58+
59+
[ConditionalFact]
60+
public void Parse_overloads_works_when_parentHierarchy_is_null_and_parentId_is_empty()
61+
=> Assert.Equal(HierarchyId.Parse(null,[]), HierarchyId.Parse("/"));
62+
63+
private readonly HierarchyId _parent = HierarchyId.Parse("/1/");
3864
}

0 commit comments

Comments
 (0)