Skip to content

Commit

Permalink
rfc
Browse files Browse the repository at this point in the history
  • Loading branch information
leonchen83 committed Feb 28, 2022
1 parent 87ab604 commit d93e59c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 23 deletions.
27 changes: 17 additions & 10 deletions src/main/java/com/redislabs/modules/rejson/Path.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
* Path is a ReJSON path, representing a valid path into an object
*/
public class Path {
public static final Path ROOT_PATH = new Path(".");
public static final Path ROOT_PATH = new Path(".");
private final String strPath;

public Path(final String strPath) {
Expand Down Expand Up @@ -77,20 +77,25 @@ public int hashCode() {
return strPath.hashCode();
}

private static String escape(String path) {
StringBuilder builder = new StringBuilder();
builder.append('"');
builder.append(path);
builder.append('"');
return builder.toString();
}

private static String parse(String path) {
if (path == null) {
throw new NullPointerException("Json Pointer Path cannot be null.");
}
if (path.isEmpty()) {
throw new IllegalArgumentException("Json Pointer Path cannot be empty.");
// ”“ means all document
return ROOT_PATH.toString();
}
if (path.charAt(0) != '/') {
throw new IllegalArgumentException("Json Pointer Path must start with '/'.");
}
if (path.length() == 1) {
// only contains '/'
return ROOT_PATH.toString();
}
char[] ary = path.toCharArray();
StringBuilder r = new StringBuilder(ary.length);
StringBuilder builder = new StringBuilder();
Expand All @@ -113,7 +118,7 @@ private static String parse(String path) {
if (builder.length() > 0 && num) {
r.append(".[" + builder.toString() + "]");
} else {
r.append("." + builder.toString());
r.append(".[" + escape(builder.toString()) + "]");
}
num = true;
builder.setLength(0);
Expand Down Expand Up @@ -155,8 +160,10 @@ private static String parse(String path) {
if (num) {
r.append(".[" + builder.toString() + "]");
} else {
r.append("." + builder.toString());
r.append(".[" + escape(builder.toString()) + "]");
}
} else if (prev == '/') {
r.append(".[" + escape(builder.toString()) + "]");
}
return r.toString();
}
Expand Down
42 changes: 29 additions & 13 deletions src/test/java/com/redislabs/modules/rejson/PathTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,35 @@ public void testPathHashCode() {

@Test
public void testJsonPointer() {
assertEquals(Path.ofJsonPointer("/"), Path.ROOT_PATH);
assertEquals(Path.ofJsonPointer("/a/b/c"), Path.of(".a.b.c"));
assertEquals(Path.ofJsonPointer("/a/0/c"), Path.of(".a.[0].c"));
assertEquals(Path.ofJsonPointer("/a/0b/c"), Path.of(".a.0b.c"));
assertEquals(Path.ofJsonPointer("/ab/cd/1010"), Path.of(".ab.cd.[1010]"));
assertEquals(Path.ofJsonPointer("/a/b/c").hashCode(), Path.of(".a.b.c").hashCode());
assertEquals(Path.ofJsonPointer(""), Path.ROOT_PATH);
assertEquals(Path.ofJsonPointer("/"), Path.of(".[\"\"]"));
assertEquals(Path.ofJsonPointer("//0"), Path.of(".[\"\"].[0]"));
assertEquals(Path.ofJsonPointer("//"), Path.of(".[\"\"].[\"\"]"));
assertEquals(Path.ofJsonPointer("// "), Path.of(".[\"\"].[\" \"]"));
assertEquals(Path.ofJsonPointer("/a/b/c"), Path.of(".[\"a\"].[\"b\"].[\"c\"]"));
assertEquals(Path.ofJsonPointer("/a/0/c"), Path.of(".[\"a\"].[0].[\"c\"]"));
assertEquals(Path.ofJsonPointer("/a/0b/c"), Path.of(".[\"a\"].[\"0b\"].[\"c\"]"));
assertEquals(Path.ofJsonPointer("/ab/cd/1010"), Path.of(".[\"ab\"].[\"cd\"].[1010]"));
assertEquals(Path.ofJsonPointer("/a/b/c").hashCode(), Path.of(".[\"a\"].[\"b\"].[\"c\"]").hashCode());

//escape test
assertEquals(Path.ofJsonPointer("/a/~0/c"), Path.of(".a.~.c"));
assertEquals(Path.ofJsonPointer("/a/~1/c"), Path.of(".a./.c"));
assertEquals(Path.ofJsonPointer("/a/~~/c"), Path.of(".a.~~.c"));
assertEquals(Path.ofJsonPointer("/~/~~~/~~"), Path.of(".~.~~~.~~"));
assertEquals(Path.ofJsonPointer("/~/~~~0/~~"), Path.of(".~.~~~.~~"));
assertEquals(Path.ofJsonPointer("/~/'.'/~~"), Path.of(".~.'.'.~~"));
// escape test
assertEquals(Path.ofJsonPointer("/a/~0"), Path.of(".[\"a\"].[\"~\"]"));
assertEquals(Path.ofJsonPointer("/a/~1"), Path.of(".[\"a\"].[\"/\"]"));
assertEquals(Path.ofJsonPointer("/a/~0/c"), Path.of(".[\"a\"].[\"~\"].[\"c\"]"));
assertEquals(Path.ofJsonPointer("/a/~1/c"), Path.of(".[\"a\"].[\"/\"].[\"c\"]"));
assertEquals(Path.ofJsonPointer("/a/~~/c"), Path.of(".[\"a\"].[\"~~\"].[\"c\"]"));
assertEquals(Path.ofJsonPointer("/~/~~~/~"), Path.of(".[\"~\"].[\"~~~\"].[\"~\"]"));
assertEquals(Path.ofJsonPointer("/~/~~~/~~"), Path.of(".[\"~\"].[\"~~~\"].[\"~~\"]"));
assertEquals(Path.ofJsonPointer("/~/~~~0/~~"), Path.of(".[\"~\"].[\"~~~\"].[\"~~\"]"));
assertEquals(Path.ofJsonPointer("/~/'.'/~~"), Path.of(".[\"~\"].[\"'.'\"].[\"~~\"]"));

// json path escape test
assertEquals(Path.ofJsonPointer("/\t"), Path.of(".[\"\t\"]"));
assertEquals(Path.ofJsonPointer("/\u0074"), Path.of(".[\"\u0074\"]"));
assertEquals(Path.ofJsonPointer("/'"), Path.of(".[\"'\"]"));
assertEquals(Path.ofJsonPointer("/\'"), Path.of(".[\"\'\"]"));
assertEquals(Path.ofJsonPointer("/\""), Path.of(".[\"\"\"]"));
assertEquals(Path.ofJsonPointer("/\n"), Path.of(".[\"\n\"]"));
assertEquals(Path.ofJsonPointer("/\\"), Path.of(".[\"\\\"]"));
}
}

0 comments on commit d93e59c

Please sign in to comment.