Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(codegen): resolve obj and array JS literals from JMESPath types for waiters #1462

Merged
merged 4 commits into from
Dec 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
package software.amazon.smithy.typescript.codegen;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import software.amazon.smithy.codegen.core.CodegenException;
import software.amazon.smithy.jmespath.ExpressionVisitor;
import software.amazon.smithy.jmespath.JmespathExpression;
Expand Down Expand Up @@ -68,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 @@ -196,10 +161,11 @@ public Void visitLiteral(LiteralExpression expression) {
executionContext = "\"" + expression.getValue().toString() + "\"";
break;
case OBJECT:
executionContext = serializeObject(expression.expectObjectValue());
break;
case ARRAY:
// TODO: resolve JMESPATH OBJECTS and ARRAY types as literals
throw new CodegenException("TypeScriptJmesPath visitor has not implemented resolution of ARRAY and"
+ " OBJECT literials ");
executionContext = serializeArray(expression.expectArrayValue());
break;
default:
// All other options are already valid js literials.
// (BOOLEAN, ANY, NULL, NUMBER, EXPRESSION)
Expand Down Expand Up @@ -340,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());
}
}
Loading