Skip to content

Commit 7f39f56

Browse files
authored
Merge pull request #4135 from AlexandreCarlton/memoize-result-path-level
2 parents b0226a7 + 97b91e0 commit 7f39f56

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

src/main/java/graphql/execution/ResultPath.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,27 @@ public static ResultPath rootPath() {
3939
// hash is effective immutable but lazily initialized similar to the hash code of java.lang.String
4040
private int hash;
4141
private final String toStringValue;
42+
private final int level;
4243

4344
private ResultPath() {
4445
parent = null;
4546
segment = null;
4647
this.toStringValue = initString();
48+
this.level = 0;
4749
}
4850

4951
private ResultPath(ResultPath parent, String segment) {
5052
this.parent = assertNotNull(parent, () -> "Must provide a parent path");
5153
this.segment = assertNotNull(segment, () -> "Must provide a sub path");
5254
this.toStringValue = initString();
55+
this.level = parent.level + 1;
5356
}
5457

5558
private ResultPath(ResultPath parent, int segment) {
5659
this.parent = assertNotNull(parent, () -> "Must provide a parent path");
5760
this.segment = segment;
5861
this.toStringValue = initString();
62+
this.level = parent.level;
5963
}
6064

6165
private String initString() {
@@ -71,15 +75,7 @@ private String initString() {
7175
}
7276

7377
public int getLevel() {
74-
int counter = 0;
75-
ResultPath currentPath = this;
76-
while (currentPath != null) {
77-
if (currentPath.segment instanceof String) {
78-
counter++;
79-
}
80-
currentPath = currentPath.parent;
81-
}
82-
return counter;
78+
return level;
8379
}
8480

8581
public ResultPath getPathWithoutListEnd() {

src/test/groovy/graphql/execution/ResultPathTest.groovy

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class ResultPathTest extends Specification {
3232
actual.toString() == expected
3333

3434
where:
35-
actual || expected
35+
actual || expected
3636
ResultPath.rootPath() || ""
3737
ResultPath.rootPath().segment("A") || "/A"
3838
ResultPath.rootPath().segment("A").segment(1).segment("B") || "/A[1]/B"
@@ -46,12 +46,27 @@ class ResultPathTest extends Specification {
4646
actual.toList() == expected
4747

4848
where:
49-
actual || expected
49+
actual || expected
5050
ResultPath.rootPath() || []
5151
ResultPath.rootPath().segment("A").sibling("B") || ["B"]
5252
ResultPath.rootPath().segment("A").segment(1).segment("B").sibling("C") || ["A", 1, "C"]
5353
}
5454

55+
@Unroll
56+
"unit test getLevel works as expected : #actual"() {
57+
58+
expect:
59+
actual.getLevel() == expected
60+
61+
where:
62+
actual || expected
63+
ResultPath.rootPath() || 0
64+
ResultPath.rootPath().segment("A") || 1
65+
ResultPath.rootPath().segment("A").segment("B") || 2
66+
ResultPath.rootPath().segment("A").segment(1).segment("B") || 2
67+
ResultPath.rootPath().segment("A").segment("B").segment(1) || 2
68+
}
69+
5570

5671
def "full integration test of path support"() {
5772
when:

0 commit comments

Comments
 (0)