Skip to content

Commit

Permalink
chore(codegen): reorder methods
Browse files Browse the repository at this point in the history
  • Loading branch information
siddsriv committed Dec 10, 2024
1 parent e583e92 commit 7dc05bd
Showing 1 changed file with 68 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,36 +62,6 @@ class TypeScriptJmesPathVisitor implements ExpressionVisitor<Void> {
scopeCount = 0;
}

private String serializeObject(Map<String, Object> obj) {
return "{" + obj.entrySet().stream()
.map(entry -> "\"" + entry.getKey() + "\":" + serializeValue(entry.getValue()))
.collect(Collectors.joining(","))
+ "}";
}

private String serializeArray(List<Object> array) {
return "[" + array.stream()
.map(this::serializeValue)
.collect(Collectors.joining(","))
+ "]";
}

@SuppressWarnings("unchecked")
private String serializeValue(Object value) {
if (value == null) {
return "null";
} else if (value instanceof String) {
return "\"" + value + "\"";
} else if (value instanceof Number || value instanceof Boolean) {
return value.toString();
} else if (value instanceof Map) {
return serializeObject((Map<String, Object>) value);
} else if (value instanceof ArrayList) {
return serializeArray((List<Object>) value);
}
throw new CodegenException("Unsupported literal type: " + value.getClass());
}

public void run() {
writer.openBlock("let returnComparator = () => {", "}", () -> {
executionContext = accessor;
Expand All @@ -101,44 +71,6 @@ public void run() {
executionContext = "returnComparator()";
}

private String makeNewScope(String prefix) {
scopeCount += 1;
return prefix + scopeCount;
}

void writeBooleanExpectation(String expectedValue, String returnValue) {
writer.openBlock("if ($L == $L) {", "}", executionContext, expectedValue, () -> {
writer.write("return $L;", returnValue);
});
}

void writeAnyStringEqualsExpectation(String expectedValue, String returnValue) {
String element = makeNewScope("anyStringEq_");
writer.openBlock("for (let $L of $L) {", "}", element, executionContext, () -> {
writer.openBlock("if ($L == $S) {", "}", element, expectedValue, () -> {
writer.write("return $L;", returnValue);
});
});
}

void writeAllStringEqualsExpectation(String expectedValue, String returnValue) {
String element = makeNewScope("element_");
String result = makeNewScope("allStringEq_");
writer.write("let $L = ($L.length > 0);", result, executionContext);
writer.openBlock("for (let $L of $L) {", "}", element, executionContext, () -> {
writer.write("$L = $L && ($L == $S)", result, result, element, expectedValue);
});
writer.openBlock("if ($L) {", "}", result, () -> {
writer.write("return $L;", returnValue);
});
}

void writeStringExpectation(String expectedValue, String returnValue) {
writer.openBlock("if ($L === $S) {", "}", executionContext, expectedValue, () -> {
writer.write("return $L;", returnValue);
});
}

@Override
public Void visitComparator(ComparatorExpression expression) {

Expand Down Expand Up @@ -374,4 +306,72 @@ public Void visitSubexpression(Subexpression expression) {
expression.getRight().accept(this);
return null;
}

void writeBooleanExpectation(String expectedValue, String returnValue) {
writer.openBlock("if ($L == $L) {", "}", executionContext, expectedValue, () -> {
writer.write("return $L;", returnValue);
});
}

void writeAnyStringEqualsExpectation(String expectedValue, String returnValue) {
String element = makeNewScope("anyStringEq_");
writer.openBlock("for (let $L of $L) {", "}", element, executionContext, () -> {
writer.openBlock("if ($L == $S) {", "}", element, expectedValue, () -> {
writer.write("return $L;", returnValue);
});
});
}

void writeAllStringEqualsExpectation(String expectedValue, String returnValue) {
String element = makeNewScope("element_");
String result = makeNewScope("allStringEq_");
writer.write("let $L = ($L.length > 0);", result, executionContext);
writer.openBlock("for (let $L of $L) {", "}", element, executionContext, () -> {
writer.write("$L = $L && ($L == $S)", result, result, element, expectedValue);
});
writer.openBlock("if ($L) {", "}", result, () -> {
writer.write("return $L;", returnValue);
});
}

void writeStringExpectation(String expectedValue, String returnValue) {
writer.openBlock("if ($L === $S) {", "}", executionContext, expectedValue, () -> {
writer.write("return $L;", returnValue);
});
}

private String makeNewScope(String prefix) {
scopeCount += 1;
return prefix + scopeCount;
}

private String serializeObject(Map<String, Object> obj) {
return "{" + obj.entrySet().stream()
.map(entry -> "\"" + entry.getKey() + "\":" + serializeValue(entry.getValue()))
.collect(Collectors.joining(","))
+ "}";
}

private String serializeArray(List<Object> array) {
return "[" + array.stream()
.map(this::serializeValue)
.collect(Collectors.joining(","))
+ "]";
}

@SuppressWarnings("unchecked")
private String serializeValue(Object value) {
if (value == null) {
return "null";
} else if (value instanceof String) {
return "\"" + value + "\"";
} else if (value instanceof Number || value instanceof Boolean) {
return value.toString();
} else if (value instanceof Map) {
return serializeObject((Map<String, Object>) value);
} else if (value instanceof ArrayList) {
return serializeArray((List<Object>) value);
}
throw new CodegenException("Unsupported literal type: " + value.getClass());
}
}

0 comments on commit 7dc05bd

Please sign in to comment.