Skip to content
Permalink
Browse files
This commit reverts changes that altered the ZPath parse method based…
… on incorrect assumptions about what a "resolved" ZPath actually is.

I've added a few notes to the JavaDoc and unit tests to hopefully clear up confusion in this area in the future.
  • Loading branch information
Ryan0751 authored and randgalt committed Aug 18, 2021
1 parent 0128b96 commit 137159bb919cb7915eb1ddb3a3dfe369bd2253b9
Showing 3 changed files with 21 additions and 22 deletions.
@@ -31,9 +31,6 @@
*/
public interface ZPath extends Resolvable
{
String PARAMETER_OPENING_DELIMITER = "{";
String PARAMETER_CLOSING_DELIMITER = "}";

/**
* The root path: "/"
*/
@@ -55,11 +52,14 @@ static String parameter()
*/
static String parameter(String name)
{
return PATH_SEPARATOR + PARAMETER_OPENING_DELIMITER + name + PARAMETER_CLOSING_DELIMITER;
return PATH_SEPARATOR + "{" + name + "}";
}

/**
* Take a string path and return a ZPath
* Take a string path and return a ZPath.
* <p>
* Note: This method always produces a fully resolved path despite the presence of any parameter-like elements (i.e, {@code {one}}).
* For substituting parameter elements and for proper parameter resolution status checks, use {@code parseWithIds()} instead.
*
* @param fullPath the path to parse
* @return ZPath
@@ -82,7 +82,7 @@ static ZPath parse(String fullPath)
*/
static ZPath parseWithIds(String fullPath)
{
return ZPathImpl.parse(fullPath, s -> isId(s) ? (PATH_SEPARATOR + s) : s); // TODO
return ZPathImpl.parse(fullPath, s -> isId(s) ? (PATH_SEPARATOR + s) : s);
}

/**
@@ -244,7 +244,13 @@ default ZPath resolved(Object... parameters)
boolean isRoot();

/**
* Return true if this path is fully resolved (i.e. has no unresolved parameters)
* Return true if this path is fully resolved (i.e. has no unresolved parameters).
* <p>
* Note: ZPath's returned by the {@code parse()} method are always considered fully resolved, despite if there are
* remaining elements in the path which appear to be parameters (but are not, i.e. {@code {one}}).
* <p>
* When working with parameters, use the {@code parseWithIds()} method, which returns a ZPath with a
* resolved state based on the presence of unresolved parameter elements in the ZPath.
*
* @return true/false
*/
@@ -210,9 +210,7 @@ public ZPath resolved(List<Object> parameters)
.map(name -> {
if ( isParameter(name) && iterator.hasNext() )
{
// Eliminate any leading path separator from substituted parameters, as ZPathImpl() will
// add in all required path separators as part of it's build operation.
return NodeName.nameFrom(iterator.next()).replaceAll(String.format("^%s+", PATH_SEPARATOR), "");
return NodeName.nameFrom(iterator.next());
}
return name;
})
@@ -228,8 +226,7 @@ public boolean isResolved()

private static boolean isParameter(String name)
{
return name.matches(String.format(".*\\%s.*\\%s", PARAMETER_OPENING_DELIMITER,
PARAMETER_CLOSING_DELIMITER));
return (name.length() > 1) && name.startsWith(PATH_SEPARATOR);
}

private ZPathImpl(List<String> nodes, String child)
@@ -56,12 +56,15 @@ public void testBasic()
assertTrue(path.startsWith(ZPath.root.child("one")));
assertFalse(path.startsWith(ZPath.root.child("two")));

// Despite these paths containing elements which appear to be parameters, ZPath.parse() always returns
// a ZPath which is considered fully resolved. This allows users to include parameter-like elements in their
// ZPath's that aren't treated as parameters.
ZPath checkIdLike = ZPath.parse("/one/{two}/three");
assertFalse(checkIdLike.isResolved());
assertTrue(checkIdLike.isResolved(), "parse method always returns a fully resolved ZPath");
checkIdLike = ZPath.parse("/one/" + ZPath.parameter() + "/three");
assertFalse(checkIdLike.isResolved());
assertTrue(checkIdLike.isResolved(), "parse method always returns a fully resolved ZPath");
checkIdLike = ZPath.parse("/one/" + ZPath.parameter("others") + "/three");
assertFalse(checkIdLike.isResolved());
assertTrue(checkIdLike.isResolved(), "parse method always returns a fully resolved ZPath");
}

@Test
@@ -71,13 +74,6 @@ public void testParsing()
assertEquals(ZPath.parse("/one/two/three"), ZPath.root.child("one").child("two").child("three"));
assertEquals(ZPath.parse("/one/two/three"), ZPath.from("one", "two", "three"));
assertEquals(ZPath.parseWithIds("/one/{id}/two/{id}"), ZPath.from("one", parameter(), "two", parameter()));

final ZPath rootPath = ZPath.parse("/root");
ZPath path = ZPath.parseWithIds("{root}/one/{id}/two/{id}");
assertFalse(path.isResolved());
path = path.resolved(rootPath.toString(), "foo", "bar");
assertEquals(ZPath.from("root", "one", "foo", "two", "bar").toString(), path.toString());
assertTrue(path.isResolved());
}

@Test

0 comments on commit 137159b

Please sign in to comment.