Skip to content

Commit 941cc0e

Browse files
authored
chore(codegen): resolve obj and array JS literals from JMESPath types for waiters (#1462)
* chore(codegen): resolve obj and array JS literals from JMESPath types * chore(codegen): add missing import * chore(codegen): use LiteralExpression methods and map, join for serializers * chore(codegen): reorder methods
1 parent ce56edf commit 941cc0e

File tree

1 file changed

+75
-41
lines changed

1 file changed

+75
-41
lines changed

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptJmesPathVisitor.java

Lines changed: 75 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
package software.amazon.smithy.typescript.codegen;
1717

1818
import java.util.ArrayList;
19+
import java.util.List;
20+
import java.util.Map;
21+
import java.util.stream.Collectors;
1922
import software.amazon.smithy.codegen.core.CodegenException;
2023
import software.amazon.smithy.jmespath.ExpressionVisitor;
2124
import software.amazon.smithy.jmespath.JmespathExpression;
@@ -68,44 +71,6 @@ public void run() {
6871
executionContext = "returnComparator()";
6972
}
7073

71-
private String makeNewScope(String prefix) {
72-
scopeCount += 1;
73-
return prefix + scopeCount;
74-
}
75-
76-
void writeBooleanExpectation(String expectedValue, String returnValue) {
77-
writer.openBlock("if ($L == $L) {", "}", executionContext, expectedValue, () -> {
78-
writer.write("return $L;", returnValue);
79-
});
80-
}
81-
82-
void writeAnyStringEqualsExpectation(String expectedValue, String returnValue) {
83-
String element = makeNewScope("anyStringEq_");
84-
writer.openBlock("for (let $L of $L) {", "}", element, executionContext, () -> {
85-
writer.openBlock("if ($L == $S) {", "}", element, expectedValue, () -> {
86-
writer.write("return $L;", returnValue);
87-
});
88-
});
89-
}
90-
91-
void writeAllStringEqualsExpectation(String expectedValue, String returnValue) {
92-
String element = makeNewScope("element_");
93-
String result = makeNewScope("allStringEq_");
94-
writer.write("let $L = ($L.length > 0);", result, executionContext);
95-
writer.openBlock("for (let $L of $L) {", "}", element, executionContext, () -> {
96-
writer.write("$L = $L && ($L == $S)", result, result, element, expectedValue);
97-
});
98-
writer.openBlock("if ($L) {", "}", result, () -> {
99-
writer.write("return $L;", returnValue);
100-
});
101-
}
102-
103-
void writeStringExpectation(String expectedValue, String returnValue) {
104-
writer.openBlock("if ($L === $S) {", "}", executionContext, expectedValue, () -> {
105-
writer.write("return $L;", returnValue);
106-
});
107-
}
108-
10974
@Override
11075
public Void visitComparator(ComparatorExpression expression) {
11176

@@ -196,10 +161,11 @@ public Void visitLiteral(LiteralExpression expression) {
196161
executionContext = "\"" + expression.getValue().toString() + "\"";
197162
break;
198163
case OBJECT:
164+
executionContext = serializeObject(expression.expectObjectValue());
165+
break;
199166
case ARRAY:
200-
// TODO: resolve JMESPATH OBJECTS and ARRAY types as literals
201-
throw new CodegenException("TypeScriptJmesPath visitor has not implemented resolution of ARRAY and"
202-
+ " OBJECT literials ");
167+
executionContext = serializeArray(expression.expectArrayValue());
168+
break;
203169
default:
204170
// All other options are already valid js literials.
205171
// (BOOLEAN, ANY, NULL, NUMBER, EXPRESSION)
@@ -340,4 +306,72 @@ public Void visitSubexpression(Subexpression expression) {
340306
expression.getRight().accept(this);
341307
return null;
342308
}
309+
310+
void writeBooleanExpectation(String expectedValue, String returnValue) {
311+
writer.openBlock("if ($L == $L) {", "}", executionContext, expectedValue, () -> {
312+
writer.write("return $L;", returnValue);
313+
});
314+
}
315+
316+
void writeAnyStringEqualsExpectation(String expectedValue, String returnValue) {
317+
String element = makeNewScope("anyStringEq_");
318+
writer.openBlock("for (let $L of $L) {", "}", element, executionContext, () -> {
319+
writer.openBlock("if ($L == $S) {", "}", element, expectedValue, () -> {
320+
writer.write("return $L;", returnValue);
321+
});
322+
});
323+
}
324+
325+
void writeAllStringEqualsExpectation(String expectedValue, String returnValue) {
326+
String element = makeNewScope("element_");
327+
String result = makeNewScope("allStringEq_");
328+
writer.write("let $L = ($L.length > 0);", result, executionContext);
329+
writer.openBlock("for (let $L of $L) {", "}", element, executionContext, () -> {
330+
writer.write("$L = $L && ($L == $S)", result, result, element, expectedValue);
331+
});
332+
writer.openBlock("if ($L) {", "}", result, () -> {
333+
writer.write("return $L;", returnValue);
334+
});
335+
}
336+
337+
void writeStringExpectation(String expectedValue, String returnValue) {
338+
writer.openBlock("if ($L === $S) {", "}", executionContext, expectedValue, () -> {
339+
writer.write("return $L;", returnValue);
340+
});
341+
}
342+
343+
private String makeNewScope(String prefix) {
344+
scopeCount += 1;
345+
return prefix + scopeCount;
346+
}
347+
348+
private String serializeObject(Map<String, Object> obj) {
349+
return "{" + obj.entrySet().stream()
350+
.map(entry -> "\"" + entry.getKey() + "\":" + serializeValue(entry.getValue()))
351+
.collect(Collectors.joining(","))
352+
+ "}";
353+
}
354+
355+
private String serializeArray(List<Object> array) {
356+
return "[" + array.stream()
357+
.map(this::serializeValue)
358+
.collect(Collectors.joining(","))
359+
+ "]";
360+
}
361+
362+
@SuppressWarnings("unchecked")
363+
private String serializeValue(Object value) {
364+
if (value == null) {
365+
return "null";
366+
} else if (value instanceof String) {
367+
return "\"" + value + "\"";
368+
} else if (value instanceof Number || value instanceof Boolean) {
369+
return value.toString();
370+
} else if (value instanceof Map) {
371+
return serializeObject((Map<String, Object>) value);
372+
} else if (value instanceof ArrayList) {
373+
return serializeArray((List<Object>) value);
374+
}
375+
throw new CodegenException("Unsupported literal type: " + value.getClass());
376+
}
343377
}

0 commit comments

Comments
 (0)