use of org.ojai.FieldPath in project drill by axbaretto.
the class FieldPathHelper method schemaPathToFieldPath.
/**
* Returns {@link FieldPath} equivalent of the specified {@link SchemaPath}.
*
* @param schemaPath {@link SchemaPath} instance that should be converted
* @return {@link FieldPath} equivalent of the specified {@link SchemaPath}.
*/
public static FieldPath schemaPathToFieldPath(SchemaPath schemaPath) {
Deque<PathSegment> pathSegments = Queues.newArrayDeque();
PathSegment pathSegment = schemaPath.getRootSegment();
while (pathSegment != null) {
pathSegments.push(pathSegment);
pathSegment = pathSegment.getChild();
}
FieldSegment child = null;
while (!pathSegments.isEmpty()) {
pathSegment = pathSegments.pop();
if (pathSegment.isNamed()) {
child = new FieldSegment.NameSegment(((PathSegment.NameSegment) pathSegment).getPath(), child, false);
} else {
child = new FieldSegment.IndexSegment(String.valueOf(((PathSegment.ArraySegment) pathSegment).getIndex()), child);
}
}
return new FieldPath((FieldSegment.NameSegment) child);
}
use of org.ojai.FieldPath in project drill by apache.
the class TestFieldPathHelper method simeTests.
@Test
public void simeTests() {
String[] pathStrs = { "a", "a.b", "a.b.c", "a[1].b[2].c", "a[0][1][2][3].b" };
FieldPath[] fieldPaths = new FieldPath[pathStrs.length];
SchemaPath[] schemaPaths = new SchemaPath[pathStrs.length];
// build
for (int i = 0; i < pathStrs.length; i++) {
String path = pathStrs[i];
fieldPaths[i] = FieldPath.parseFrom(path);
schemaPaths[i] = SchemaPath.parseFromString(path);
}
// verify
for (int i = 0; i < pathStrs.length; i++) {
FieldPath fp = FieldPathHelper.schemaPath2FieldPath(schemaPaths[i]);
assertEquals(fieldPaths[i], fp);
SchemaPath sp = FieldPathHelper.fieldPath2SchemaPath(fieldPaths[i]);
assertEquals(schemaPaths[i], sp);
}
}
Aggregations