Skip to content
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
14 changes: 5 additions & 9 deletions src/main/java/graphql/execution/ResultPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,27 @@ public static ResultPath rootPath() {
// hash is effective immutable but lazily initialized similar to the hash code of java.lang.String
private int hash;
private final String toStringValue;
private final int level;

private ResultPath() {
parent = null;
segment = null;
this.toStringValue = initString();
this.level = 0;
}

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

private ResultPath(ResultPath parent, int segment) {
this.parent = assertNotNull(parent, () -> "Must provide a parent path");
this.segment = segment;
this.toStringValue = initString();
this.level = parent.level;
}

private String initString() {
Expand All @@ -71,15 +75,7 @@ private String initString() {
}

public int getLevel() {
int counter = 0;
ResultPath currentPath = this;
while (currentPath != null) {
if (currentPath.segment instanceof String) {
counter++;
}
currentPath = currentPath.parent;
}
return counter;
return level;
}

public ResultPath getPathWithoutListEnd() {
Expand Down
19 changes: 17 additions & 2 deletions src/test/groovy/graphql/execution/ResultPathTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class ResultPathTest extends Specification {
actual.toString() == expected

where:
actual || expected
actual || expected
ResultPath.rootPath() || ""
ResultPath.rootPath().segment("A") || "/A"
ResultPath.rootPath().segment("A").segment(1).segment("B") || "/A[1]/B"
Expand All @@ -46,12 +46,27 @@ class ResultPathTest extends Specification {
actual.toList() == expected

where:
actual || expected
actual || expected
ResultPath.rootPath() || []
ResultPath.rootPath().segment("A").sibling("B") || ["B"]
ResultPath.rootPath().segment("A").segment(1).segment("B").sibling("C") || ["A", 1, "C"]
}

@Unroll
"unit test getLevel works as expected : #actual"() {

expect:
actual.getLevel() == expected

where:
actual || expected
ResultPath.rootPath() || 0
ResultPath.rootPath().segment("A") || 1
ResultPath.rootPath().segment("A").segment("B") || 2
ResultPath.rootPath().segment("A").segment(1).segment("B") || 2
ResultPath.rootPath().segment("A").segment("B").segment(1) || 2
}


def "full integration test of path support"() {
when:
Expand Down