|
16 | 16 | package software.amazon.smithy.typescript.codegen; |
17 | 17 |
|
18 | 18 | import java.util.ArrayList; |
| 19 | +import java.util.List; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.stream.Collectors; |
19 | 22 | import software.amazon.smithy.codegen.core.CodegenException; |
20 | 23 | import software.amazon.smithy.jmespath.ExpressionVisitor; |
21 | 24 | import software.amazon.smithy.jmespath.JmespathExpression; |
@@ -68,44 +71,6 @@ public void run() { |
68 | 71 | executionContext = "returnComparator()"; |
69 | 72 | } |
70 | 73 |
|
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 | | - |
109 | 74 | @Override |
110 | 75 | public Void visitComparator(ComparatorExpression expression) { |
111 | 76 |
|
@@ -196,10 +161,11 @@ public Void visitLiteral(LiteralExpression expression) { |
196 | 161 | executionContext = "\"" + expression.getValue().toString() + "\""; |
197 | 162 | break; |
198 | 163 | case OBJECT: |
| 164 | + executionContext = serializeObject(expression.expectObjectValue()); |
| 165 | + break; |
199 | 166 | 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; |
203 | 169 | default: |
204 | 170 | // All other options are already valid js literials. |
205 | 171 | // (BOOLEAN, ANY, NULL, NUMBER, EXPRESSION) |
@@ -340,4 +306,72 @@ public Void visitSubexpression(Subexpression expression) { |
340 | 306 | expression.getRight().accept(this); |
341 | 307 | return null; |
342 | 308 | } |
| 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 | + } |
343 | 377 | } |
0 commit comments