Skip to content

Commit 242fd50

Browse files
committed
chore(codegen): resolve obj and array JS literals from JMESPath types
1 parent f4e1a45 commit 242fd50

File tree

1 file changed

+56
-3
lines changed

1 file changed

+56
-3
lines changed

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

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,54 @@ class TypeScriptJmesPathVisitor implements ExpressionVisitor<Void> {
5959
scopeCount = 0;
6060
}
6161

62+
private String serializeObject(Map<String, Object> obj) {
63+
StringBuilder builder = new StringBuilder();
64+
builder.append("{");
65+
boolean first = true; // first key-value pair
66+
for (Map.Entry<String, Object> entry: obj.entrySet()) {
67+
if (!first) {
68+
builder.append(",");
69+
}
70+
builder.append("\"").append(entry.getKey()).append("\":");
71+
// recursively serialize value (could be primitive, obj, array)
72+
builder.append(serializeValue(entry.getValue()));
73+
first = false;
74+
}
75+
builder.append("}");
76+
return builder.toString();
77+
}
78+
79+
private String serializeArray(ArrayList<Object> array) {
80+
StringBuilder builder = new StringBuilder();
81+
builder.append("[");
82+
boolean first = true;
83+
for (Object value: array) {
84+
if (!first) {
85+
builder.append(",");
86+
}
87+
builder.append(serializeValue(value));
88+
first = false;
89+
}
90+
builder.append("]");
91+
return builder.toString();
92+
}
93+
94+
@SuppressWarnings("unchecked")
95+
private String serializeValue(Object value) {
96+
if (value == null) {
97+
return "null";
98+
} else if (value instanceof String) {
99+
return "\"" + value + "\"";
100+
} else if (value instanceof Number || value instanceof Boolean) {
101+
return value.toString();
102+
} else if (value instanceof Map) {
103+
return serializeObject((Map<String, Object>) value);
104+
} else if (value instanceof ArrayList) {
105+
return serializeArray((ArrayList<Object>) value);
106+
}
107+
throw new CodegenException("Unsupported literal type: " + value.getClass());
108+
}
109+
62110
public void run() {
63111
writer.openBlock("let returnComparator = () => {", "}", () -> {
64112
executionContext = accessor;
@@ -196,10 +244,15 @@ public Void visitLiteral(LiteralExpression expression) {
196244
executionContext = "\"" + expression.getValue().toString() + "\"";
197245
break;
198246
case OBJECT:
247+
@SuppressWarnings("unchecked")
248+
Map<String, Object> objValue = (Map<String, Object>) expression.getValue();
249+
executionContext = serializeObject(objValue);
250+
break;
199251
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 ");
252+
@SuppressWarnings("unchecked")
253+
ArrayList<Object> arrayValue = (ArrayList<Object>) expression.getValue();
254+
executionContext = serializeArray(arrayValue);
255+
break;
203256
default:
204257
// All other options are already valid js literials.
205258
// (BOOLEAN, ANY, NULL, NUMBER, EXPRESSION)

0 commit comments

Comments
 (0)