Skip to content

Commit

Permalink
chore(codegen): use LiteralExpression methods and map, join for seria…
Browse files Browse the repository at this point in the history
…lizers
  • Loading branch information
siddsriv committed Dec 10, 2024
1 parent 607220d commit e583e92
Showing 1 changed file with 14 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +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 @@ -61,35 +63,17 @@ class TypeScriptJmesPathVisitor implements ExpressionVisitor<Void> {
}

private String serializeObject(Map<String, Object> obj) {
StringBuilder builder = new StringBuilder();
builder.append("{");
boolean first = true; // first key-value pair
for (Map.Entry<String, Object> entry: obj.entrySet()) {
if (!first) {
builder.append(",");
}
builder.append("\"").append(entry.getKey()).append("\":");
// recursively serialize value (could be primitive, obj, array)
builder.append(serializeValue(entry.getValue()));
first = false;
}
builder.append("}");
return builder.toString();
return "{" + obj.entrySet().stream()
.map(entry -> "\"" + entry.getKey() + "\":" + serializeValue(entry.getValue()))
.collect(Collectors.joining(","))
+ "}";
}

private String serializeArray(ArrayList<Object> array) {
StringBuilder builder = new StringBuilder();
builder.append("[");
boolean first = true;
for (Object value: array) {
if (!first) {
builder.append(",");
}
builder.append(serializeValue(value));
first = false;
}
builder.append("]");
return builder.toString();
private String serializeArray(List<Object> array) {
return "[" + array.stream()
.map(this::serializeValue)
.collect(Collectors.joining(","))
+ "]";
}

@SuppressWarnings("unchecked")
Expand All @@ -103,7 +87,7 @@ private String serializeValue(Object value) {
} else if (value instanceof Map) {
return serializeObject((Map<String, Object>) value);
} else if (value instanceof ArrayList) {
return serializeArray((ArrayList<Object>) value);
return serializeArray((List<Object>) value);
}
throw new CodegenException("Unsupported literal type: " + value.getClass());
}
Expand Down Expand Up @@ -245,14 +229,10 @@ public Void visitLiteral(LiteralExpression expression) {
executionContext = "\"" + expression.getValue().toString() + "\"";
break;
case OBJECT:
@SuppressWarnings("unchecked")
Map<String, Object> objValue = (Map<String, Object>) expression.getValue();
executionContext = serializeObject(objValue);
executionContext = serializeObject(expression.expectObjectValue());
break;
case ARRAY:
@SuppressWarnings("unchecked")
ArrayList<Object> arrayValue = (ArrayList<Object>) expression.getValue();
executionContext = serializeArray(arrayValue);
executionContext = serializeArray(expression.expectArrayValue());
break;
default:
// All other options are already valid js literials.
Expand Down

0 comments on commit e583e92

Please sign in to comment.