Skip to content

Commit 2e8b66f

Browse files
committed
feat: protocol size reduction
1 parent ad9fb3c commit 2e8b66f

File tree

11 files changed

+207
-93
lines changed

11 files changed

+207
-93
lines changed

codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AwsEc2.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import software.amazon.smithy.model.traits.TimestampFormatTrait.Format;
2727
import software.amazon.smithy.typescript.codegen.TypeScriptWriter;
2828
import software.amazon.smithy.typescript.codegen.integration.HttpRpcProtocolGenerator;
29+
import software.amazon.smithy.typescript.codegen.util.StringStore;
2930
import software.amazon.smithy.utils.SmithyInternalApi;
3031

3132
/**
@@ -109,6 +110,10 @@ public void generateSharedComponents(GenerationContext context) {
109110
writer.openBlock("if (output.statusCode == 404) {", "}", () -> writer.write("return 'NotFound';"));
110111
});
111112
writer.write("");
113+
writer.write(
114+
context.getStringStore().getIncremental(AwsEc2.class.getSimpleName())
115+
);
116+
writer.write("");
112117
}
113118

114119
@Override
@@ -144,8 +149,15 @@ protected void serializeInputDocument(
144149
inputStructure.accept(new QueryMemberSerVisitor(context, "input", Format.DATE_TIME)));
145150
// Set the protocol required values.
146151
ServiceShape serviceShape = context.getService();
147-
writer.write("Action: $S,", operation.getId().getName(serviceShape));
148-
writer.write("Version: $S,", serviceShape.getVersion());
152+
StringStore stringStore = context.getStringStore();
153+
writer.write(
154+
"[" + stringStore.var("Action") + "]: $L,",
155+
stringStore.var(operation.getId().getName(serviceShape))
156+
);
157+
writer.write(
158+
"[" + stringStore.var("Version") + "]: $L,",
159+
stringStore.var(serviceShape.getVersion())
160+
);
149161
});
150162
}
151163

codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AwsProtocolUtils.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import software.amazon.smithy.typescript.codegen.integration.HttpProtocolGeneratorUtils;
4545
import software.amazon.smithy.typescript.codegen.integration.ProtocolGenerator;
4646
import software.amazon.smithy.typescript.codegen.integration.ProtocolGenerator.GenerationContext;
47+
import software.amazon.smithy.typescript.codegen.util.StringStore;
4748
import software.amazon.smithy.utils.IoUtils;
4849
import software.amazon.smithy.utils.SmithyInternalApi;
4950

@@ -247,8 +248,15 @@ static boolean generateUndefinedQueryInputBody(GenerationContext context, Operat
247248
writer.openBlock("const body = buildFormUrlencodedString({", "});", () -> {
248249
// Set the protocol required values.
249250
ServiceShape serviceShape = context.getService();
250-
writer.write("Action: $S,", operation.getId().getName(serviceShape));
251-
writer.write("Version: $S,", serviceShape.getVersion());
251+
StringStore stringStore = context.getStringStore();
252+
writer.write(
253+
"[" + stringStore.var("Action") + "]: $L,",
254+
stringStore.var(operation.getId().getName(serviceShape))
255+
);
256+
writer.write(
257+
"[" + stringStore.var("Version") + "]: $L,",
258+
stringStore.var(serviceShape.getVersion())
259+
);
252260
});
253261

254262
return true;
@@ -277,7 +285,7 @@ static boolean writeXmlNamespace(GenerationContext context, Shape shape, String
277285
if (prefix.isPresent()) {
278286
xmlns += ":" + prefix.get();
279287
}
280-
writer.write("$L.addAttribute($S, $S);", nodeName, xmlns, trait.getUri());
288+
writer.write("$L.a($S, $S);", nodeName, xmlns, trait.getUri());
281289
return true;
282290
}
283291

codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AwsQuery.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import software.amazon.smithy.model.traits.TimestampFormatTrait.Format;
3131
import software.amazon.smithy.typescript.codegen.TypeScriptWriter;
3232
import software.amazon.smithy.typescript.codegen.integration.HttpRpcProtocolGenerator;
33+
import software.amazon.smithy.typescript.codegen.util.StringStore;
3334
import software.amazon.smithy.utils.SmithyInternalApi;
3435

3536
/**
@@ -113,6 +114,10 @@ public void generateSharedComponents(GenerationContext context) {
113114
writer.openBlock("if (output.statusCode == 404) {", "}", () -> writer.write("return 'NotFound';"));
114115
});
115116
writer.write("");
117+
writer.write(
118+
context.getStringStore().getIncremental(AwsQuery.class.getSimpleName())
119+
);
120+
writer.write("");
116121
}
117122

118123
@Override
@@ -148,8 +153,15 @@ protected void serializeInputDocument(
148153
inputStructure.accept(new QueryMemberSerVisitor(context, "input", Format.DATE_TIME)));
149154
// Set the protocol required values.
150155
ServiceShape serviceShape = context.getService();
151-
writer.write("Action: $S,", operation.getId().getName(serviceShape));
152-
writer.write("Version: $S,", serviceShape.getVersion());
156+
StringStore stringStore = context.getStringStore();
157+
writer.write(
158+
"[" + stringStore.var("Action") + "]: $L,",
159+
stringStore.var(operation.getId().getName(serviceShape))
160+
);
161+
writer.write(
162+
"[" + stringStore.var("Version") + "]: $L,",
163+
stringStore.var(serviceShape.getVersion())
164+
);
153165
});
154166
}
155167

codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AwsRestXml.java

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import software.amazon.smithy.model.traits.XmlNameTrait;
3939
import software.amazon.smithy.typescript.codegen.TypeScriptWriter;
4040
import software.amazon.smithy.typescript.codegen.integration.HttpBindingProtocolGenerator;
41+
import software.amazon.smithy.typescript.codegen.util.StringStore;
4142
import software.amazon.smithy.utils.SmithyInternalApi;
4243

4344

@@ -65,9 +66,7 @@
6566
@SmithyInternalApi
6667
final class AwsRestXml extends HttpBindingProtocolGenerator {
6768

68-
AwsRestXml() {
69-
super(true);
70-
}
69+
AwsRestXml() { super(true); }
7170

7271
@Override
7372
protected String getDocumentContentType() {
@@ -96,7 +95,8 @@ protected void generateDocumentBodyShapeSerializers(GenerationContext context, S
9695

9796
@Override
9897
protected void generateDocumentBodyShapeDeserializers(GenerationContext context, Set<Shape> shapes) {
99-
AwsProtocolUtils.generateDocumentBodyShapeSerde(context, shapes, new XmlShapeDeserVisitor(context));
98+
AwsProtocolUtils.generateDocumentBodyShapeSerde(context, shapes,
99+
new XmlShapeDeserVisitor(context));
100100
}
101101

102102
@Override
@@ -127,6 +127,10 @@ public void generateSharedComponents(GenerationContext context) {
127127
writer.openBlock("if (output.statusCode == 404) {", "}", () -> writer.write("return 'NotFound';"));
128128
});
129129
writer.write("");
130+
writer.write(
131+
context.getStringStore().getIncremental(AwsRestXml.class.getSimpleName())
132+
);
133+
writer.write("");
130134
}
131135

132136
@Override
@@ -196,7 +200,8 @@ private void serializeDocumentBody(
196200
ShapeId inputShapeId = documentBindings.get(0).getMember().getContainer();
197201

198202
// Start with the XML declaration.
199-
writer.write("body = \"<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?>\";");
203+
String xmlDeclVar = context.getStringStore().var("<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?>");
204+
writer.write("body = $L;", xmlDeclVar);
200205

201206
writer.addImport("XmlNode", "__XmlNode", "@aws-sdk/xml-builder");
202207

@@ -205,13 +210,13 @@ private void serializeDocumentBody(
205210
String nodeName = inputShape.getTrait(XmlNameTrait.class)
206211
.map(XmlNameTrait::getValue)
207212
.orElse(inputShapeId.getName(serviceShape));
208-
writer.write("const bodyNode = new __XmlNode($S);", nodeName);
213+
writer.write("const bn = new __XmlNode($L);", context.getStringStore().var(nodeName));
209214

210215
// Add @xmlNamespace value of the service to the root node,
211216
// fall back to one from the input shape.
212-
boolean serviceXmlns = AwsProtocolUtils.writeXmlNamespace(context, serviceShape, "bodyNode");
217+
boolean serviceXmlns = AwsProtocolUtils.writeXmlNamespace(context, serviceShape, "bn");
213218
if (!serviceXmlns) {
214-
AwsProtocolUtils.writeXmlNamespace(context, inputShape, "bodyNode");
219+
AwsProtocolUtils.writeXmlNamespace(context, inputShape, "bn");
215220
}
216221

217222
XmlShapeSerVisitor shapeSerVisitor = new XmlShapeSerVisitor(context);
@@ -220,18 +225,16 @@ private void serializeDocumentBody(
220225
MemberShape memberShape = binding.getMember();
221226
// The name of the member to get from the input shape.
222227
String memberName = symbolProvider.toMemberName(memberShape);
223-
String inputLocation = "input." + memberName;
228+
String inputLocation = "input[" + context.getStringStore().var(memberName) + "]";
224229

225230
// Handle if the member is an idempotency token that should be auto-filled.
226231
AwsProtocolUtils.writeIdempotencyAutofill(context, memberShape, inputLocation);
227232

228-
writer.openBlock("if ($L !== undefined) {", "}", inputLocation, () -> {
229-
shapeSerVisitor.serializeNamedMember(context, memberName, memberShape, () -> inputLocation);
230-
});
233+
shapeSerVisitor.serializeNamedMember(context, memberName, memberShape, () -> inputLocation);
231234
}
232235

233236
// Append the generated XML to the body.
234-
writer.write("body += bodyNode.toString();");
237+
writer.write("body += bn.toString();");
235238
}
236239

237240
@Override
@@ -286,15 +289,16 @@ private void serializePayload(
286289
member.hasTrait(XmlNameTrait.class)
287290
&& !member.getTrait(XmlNameTrait.class).get().getValue().equals(targetName)
288291
) {
289-
writer.write("contents = contents.withName($S);", member.getTrait(XmlNameTrait.class).get().getValue());
292+
writer.write("contents = contents.n($S);", member.getTrait(XmlNameTrait.class).get().getValue());
290293
}
291294

292295
// XmlNode will serialize Structure and non-streaming Union payloads as XML documents.
293296
if (target instanceof StructureShape
294297
|| (target instanceof UnionShape && !target.hasTrait(StreamingTrait.class))
295298
) {
296299
// Start with the XML declaration.
297-
writer.write("body = \"<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?>\";");
300+
String xmlDeclVar = context.getStringStore().var("<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?>");
301+
writer.write("body = $L;", xmlDeclVar);
298302

299303
// Add @xmlNamespace value of the service to the root node,
300304
// fall back to one from the payload target.
@@ -363,7 +367,7 @@ private void deserializeDocumentBody(
363367

364368
shapeVisitor.deserializeNamedMember(context, memberName, memberShape, "data", (dataSource, visitor) -> {
365369
TypeScriptWriter writer = context.getWriter();
366-
writer.write("contents.$L = $L;", memberName, target.accept(visitor));
370+
writer.write("contents[$L] = $L;", context.getStringStore().var(memberName), target.accept(visitor));
367371
});
368372
}
369373
}

codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/JsonRpcProtocolGenerator.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ public void generateSharedComponents(GenerationContext context) {
112112
if (context.getService().hasTrait(AwsQueryCompatibleTrait.class)) {
113113
AwsProtocolUtils.generateJsonParseBodyWithQueryHeader(context);
114114
}
115+
writer.write(
116+
context.getStringStore().getIncremental(JsonRpcProtocolGenerator.class.getSimpleName())
117+
);
118+
writer.write("");
115119
}
116120

117121
@Override

codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/QueryShapeSerVisitor.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import software.amazon.smithy.typescript.codegen.integration.DocumentMemberSerVisitor;
3636
import software.amazon.smithy.typescript.codegen.integration.DocumentShapeSerVisitor;
3737
import software.amazon.smithy.typescript.codegen.integration.ProtocolGenerator.GenerationContext;
38+
import software.amazon.smithy.typescript.codegen.util.StringStore;
3839
import software.amazon.smithy.utils.SmithyInternalApi;
3940

4041
/**
@@ -49,6 +50,7 @@
4950
@SmithyInternalApi
5051
class QueryShapeSerVisitor extends DocumentShapeSerVisitor {
5152
private static final Format TIMESTAMP_FORMAT = Format.DATE_TIME;
53+
private StringStore stringStore = getContext().getStringStore();
5254

5355
QueryShapeSerVisitor(GenerationContext context) {
5456
super(context);
@@ -175,7 +177,7 @@ protected void serializeStructure(GenerationContext context, StructureShape shap
175177

176178
// Serialize every member of the structure if present.
177179
shape.getAllMembers().forEach((memberName, memberShape) -> {
178-
String inputLocation = "input." + memberName;
180+
String inputLocation = "input[" + stringStore.var(memberName) + "]";
179181

180182
// Handle if the member is an idempotency token that should be auto-filled.
181183
AwsProtocolUtils.writeIdempotencyAutofill(context, memberShape, inputLocation);
@@ -208,7 +210,10 @@ private void serializeNamedMember(
208210
if (inputVisitor.visitSuppliesEntryList(target)) {
209211
serializeNamedMemberEntryList(context, locationName, memberShape, target, inputVisitor, inputLocation);
210212
} else {
211-
serializeNamedMemberValue(context, locationName, "input." + memberName, memberShape, target);
213+
serializeNamedMemberValue(
214+
context, locationName,
215+
"input[" + stringStore.var(memberName) + "]", memberShape, target
216+
);
212217
}
213218
}
214219

@@ -227,7 +232,7 @@ private void serializeNamedMemberValue(
227232
TIMESTAMP_FORMAT, dataSource)
228233
: target.accept(getMemberVisitor(dataSource));
229234

230-
writer.write("entries[$S] = $L;", locationName, valueProvider);
235+
writer.write("entries[$L] = $L;", stringStore.var(locationName), valueProvider);
231236
}
232237

233238
/**

codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/RestJsonProtocolGenerator.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ public void generateSharedComponents(GenerationContext context) {
113113
writer.addUseImports(getApplicationProtocol().getResponseType());
114114
writer.addImport("take", null, TypeScriptDependency.AWS_SMITHY_CLIENT);
115115
writer.write(IoUtils.readUtf8Resource(getClass(), "load-json-error-code-stub.ts"));
116+
117+
writer.write(
118+
context.getStringStore().getIncremental(RestJsonProtocolGenerator.class.getSimpleName())
119+
);
120+
writer.write("");
116121
}
117122

118123
@Override

codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/XmlMemberSerVisitor.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import software.amazon.smithy.typescript.codegen.TypeScriptWriter;
3535
import software.amazon.smithy.typescript.codegen.integration.DocumentMemberSerVisitor;
3636
import software.amazon.smithy.typescript.codegen.integration.ProtocolGenerator.GenerationContext;
37+
import software.amazon.smithy.typescript.codegen.util.StringStore;
3738
import software.amazon.smithy.utils.SmithyInternalApi;
3839

3940
/**
@@ -52,10 +53,12 @@
5253
@SmithyInternalApi
5354
final class XmlMemberSerVisitor extends DocumentMemberSerVisitor {
5455
private final GenerationContext context;
56+
private final StringStore stringStore;
5557

5658
XmlMemberSerVisitor(GenerationContext context, String dataSource, Format defaultTimestampFormat) {
5759
super(context, dataSource, defaultTimestampFormat);
5860
this.context = context;
61+
this.stringStore = context.getStringStore();
5962
}
6063

6164
@Override
@@ -121,7 +124,7 @@ String getAsXmlText(Shape shape, String dataSource) {
121124
TypeScriptWriter writer = getContext().getWriter();
122125
writer.addImport("XmlNode", "__XmlNode", "@aws-sdk/xml-builder");
123126
writer.addImport("XmlText", "__XmlText", "@aws-sdk/xml-builder");
124-
return "__XmlNode.of(\"" + nodeName + "\", " + dataSource + ")";
127+
return "__XmlNode.of(" + stringStore.var(nodeName) + ", " + dataSource + ")";
125128
}
126129

127130
@Override

0 commit comments

Comments
 (0)