Skip to content

Commit

Permalink
refine the parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
evanchooly committed Jun 20, 2024
1 parent 54ef638 commit 3505721
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 48 deletions.
65 changes: 24 additions & 41 deletions core/src/test/java/dev/morphia/test/TemplatedTestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -287,74 +288,56 @@ protected List<Document> loadAction(String actionName) {
return extractDocuments(unwrapArray(loadResource(actionName)));
}

@NotNull
protected String loadResource(String pipelineName) {
protected List<String> loadResource(String pipelineName) {
InputStream stream = getClass().getResourceAsStream(pipelineName);
if (stream == null) {
fail(format("missing action file: src/test/resources/%s/%s", getClass().getPackageName().replace('.', '/'),
pipelineName));
}
var resource = new BufferedReader(new InputStreamReader(stream))
return new BufferedReader(new InputStreamReader(stream))
.lines()
.collect(joining("\n"));
return resource.trim();
.collect(toList());
}

private List<Document> extractDocuments(String resource) {
var line = resource;
private List<Document> extractDocuments(List<String> resource) {
var line = resource.get(0).trim();
if (line.startsWith("db.")) {
if (line.endsWith(")")) {
line = line.substring(line.indexOf("(") + 1, line.lastIndexOf(")")).trim();
} else {
throw new IllegalStateException("I don't know how to parse this line: \n\t" + line);
}
line = line.substring(line.indexOf("(") + 1).trim();
resource.set(0, line);
line = resource.get(resource.size() - 1);
line = line.substring(0, line.lastIndexOf(")"));
resource.set(resource.size() - 1, line);
}
List<Document> docs = new ArrayList<>();
Iterator<String> lines = resource.iterator();
var current = "";
while (!line.isEmpty()) {
char c = line.charAt(0);
line = line.substring(1);
while (lines.hasNext()) {
current += lines.next();
if (balanced(current)) {
try {
docs.add(Document.parse(current));
} catch (BsonInvalidOperationException e) {
throw new RuntimeException("Error parsing " + resource, e);
current = "";
} catch (JsonParseException | BsonInvalidOperationException e) {
throw new RuntimeException("Error parsing " + current, e);
}
current = "";
} else {
current += c;
}
}
docs.add(Document.parse(current));

return docs;
}

private void extractQueryFilters(List<String> list) {
String line = list.stream().collect(joining());
if (ACTION.matcher(line).matches()) {
if (line.endsWith(")")) {
line = line.substring(line.indexOf("(") + 1, line.lastIndexOf(")"));
list.clear();
list.add(line.trim());
} else {
throw new IllegalStateException("I don't know how to parse this line: \n\t" + line);
}

}

}

private static String unwrapArray(String resource) {
String line = resource.trim();
private static List<String> unwrapArray(List<String> resource) {
String line = resource.get(0).trim();
if (line.startsWith("[")) {
line = line.substring(1);
resource.set(0, line.trim().substring(1));
}
var last = resource.size() - 1;
line = resource.get(last).trim();
if (line.endsWith("]")) {
line = line.substring(0, line.length() - 1);
resource.set(last, line.substring(0, line.length() - 1));
}

return line.trim();
return resource;
}

private boolean balanced(String input) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
public class TestFunction extends AggregationTest {
@Test
public void testExample1() {
skipActionCheck = true;
testPipeline(ServerVersion.ANY, false, true, (aggregation) -> aggregation.pipeline(
addFields()
.field("isFound", function("""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class TestSplit extends AggregationTest {
public void testExample1() {
testPipeline(ServerVersion.ANY, false, true, (aggregation) -> {
RegexExpression regex = regexMatch("$city_state").pattern("[A-Z]{2}");
skipActionCheck = true;
return aggregation.pipeline(
project()
.include("city_state", split("$city", ", "))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
isFound:
{ $function:
{
body: "function(name) {\n
return hex_md5(name) == \"15b0a220baa16331e8d80e15367677ad\"\n
body: "function(name) {
return hex_md5(name) == \"15b0a220baa16331e8d80e15367677ad\"
}",
args: [ "$name" ],
lang: "js"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
$set: {
$set: {
totalHomework: { $sum: "$homework" },
totalQuiz: { $sum: "$quiz" }
}
},
{
},
{
$set: {
totalScore: { $add: [ "$totalHomework", "$totalQuiz", "$extraCredit" ] } }
}
totalScore: { $add: [ "$totalHomework", "$totalQuiz", "$extraCredit" ] }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
moved a trailing brace to a new line where it belongs

0 comments on commit 3505721

Please sign in to comment.