Skip to content

Commit ebbe397

Browse files
committed
shorten internal serde names
1 parent 9a52172 commit ebbe397

File tree

12 files changed

+572
-513
lines changed

12 files changed

+572
-513
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,8 +448,8 @@ private void writeSerdeDispatcher(boolean isInput) {
448448
writer.write("throw new Error(\"No supported protocol was found\");");
449449
} else {
450450
String serdeFunctionName = isInput
451-
? ProtocolGenerator.getSerFunctionName(symbol, protocolGenerator.getName())
452-
: ProtocolGenerator.getDeserFunctionName(symbol, protocolGenerator.getName());
451+
? ProtocolGenerator.getSerFunctionShortName(symbol)
452+
: ProtocolGenerator.getDeserFunctionShortName(symbol);
453453
writer.addImport(serdeFunctionName, serdeFunctionName,
454454
Paths.get(".", CodegenUtils.SOURCE_FOLDER, ProtocolGenerator.PROTOCOLS_FOLDER,
455455
ProtocolGenerator.getSanitizedName(protocolGenerator.getName())).toString());

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/DocumentMemberDeserVisitor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ private String getDelegateDeserializer(Shape shape) {
283283
private String getDelegateDeserializer(Shape shape, String customDataSource) {
284284
// Use the shape for the function name.
285285
Symbol symbol = context.getSymbolProvider().toSymbol(shape);
286-
return ProtocolGenerator.getDeserFunctionName(symbol, context.getProtocolName())
286+
return ProtocolGenerator.getDeserFunctionShortName(symbol)
287287
+ "(" + customDataSource + ", context)";
288288
}
289289
}

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/DocumentMemberSerVisitor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ public final String unionShape(UnionShape shape) {
252252
private String getDelegateSerializer(Shape shape) {
253253
// Use the shape for the function name.
254254
Symbol symbol = context.getSymbolProvider().toSymbol(shape);
255-
return ProtocolGenerator.getSerFunctionName(symbol, context.getProtocolName())
255+
return ProtocolGenerator.getSerFunctionShortName(symbol)
256256
+ "(" + dataSource + ", context)";
257257
}
258258
}

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/DocumentShapeDeserVisitor.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,12 @@ protected final void generateDeserFunction(
301301

302302
Symbol symbol = symbolProvider.toSymbol(shape);
303303
// Use the shape name for the function name.
304-
String methodName = ProtocolGenerator.getDeserFunctionName(symbol, context.getProtocolName());
304+
String methodName = ProtocolGenerator.getDeserFunctionShortName(symbol);
305+
String methodLongName =
306+
ProtocolGenerator.getDeserFunctionName(symbol, context.getProtocolName());
305307

306308
writer.addImport(symbol, symbol.getName());
309+
writer.writeDocs(methodLongName);
307310
writer.openBlock("const $L = (\n"
308311
+ " output: any,\n"
309312
+ " context: __SerdeContext\n"

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/DocumentShapeSerVisitor.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,9 +297,12 @@ private void generateSerFunction(
297297

298298
Symbol symbol = symbolProvider.toSymbol(shape);
299299
// Use the shape name for the function name.
300-
String methodName = ProtocolGenerator.getSerFunctionName(symbol, context.getProtocolName());
300+
String methodName = ProtocolGenerator.getSerFunctionShortName(symbol);
301+
String methodLongName = ProtocolGenerator.getSerFunctionName(symbol, context.getProtocolName());
301302

302303
writer.addImport(symbol, symbol.getName());
304+
305+
writer.writeDocs(methodLongName);
303306
writer.openBlock("const $L = (\n"
304307
+ " input: $T,\n"
305308
+ " context: __SerdeContext\n"

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/EventStreamGenerator.java

Lines changed: 74 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,10 @@ public static UnionShape getEventStreamOutputShape(GenerationContext context, Op
9090
* Shapes that referred by event will be added.
9191
*/
9292
public void generateEventStreamSerializers(
93-
GenerationContext context,
94-
ServiceShape service,
95-
String documentContentType,
96-
Runnable serializeInputEventDocumentPayload,
93+
GenerationContext context,
94+
ServiceShape service,
95+
String documentContentType,
96+
Runnable serializeInputEventDocumentPayload,
9797
Set<Shape> documentShapesToSerialize
9898
) {
9999
Model model = context.getModel();
@@ -118,11 +118,11 @@ public void generateEventStreamSerializers(
118118
});
119119
eventShapesToMarshall.forEach(event -> {
120120
generateEventMarshaller(
121-
context,
122-
event,
123-
documentContentType,
124-
serializeInputEventDocumentPayload,
125-
documentShapesToSerialize);
121+
context,
122+
event,
123+
documentContentType,
124+
serializeInputEventDocumentPayload,
125+
documentShapesToSerialize);
126126
});
127127
}
128128

@@ -177,31 +177,34 @@ public void generateEventStreamDeserializers(
177177

178178
private void generateEventStreamSerializer(GenerationContext context, UnionShape eventsUnion) {
179179
String methodName = getSerFunctionName(context, eventsUnion);
180+
String methodLongName = ProtocolGenerator.getSerFunctionName(getSymbol(context, eventsUnion),
181+
context.getProtocolName());
180182
Symbol eventsUnionSymbol = getSymbol(context, eventsUnion);
181183
TypeScriptWriter writer = context.getWriter();
182184
Model model = context.getModel();
183185
writer.addImport("Message", "__Message", TypeScriptDependency.AWS_SDK_TYPES.packageName);
186+
187+
writer.writeDocs(methodLongName);
184188
writer.openBlock("const $L = (\n"
185189
+ " input: any,\n"
186190
+ " context: $L\n"
187191
+ "): any => {", "}", methodName, getEventStreamSerdeContextType(context, eventsUnion), () -> {
188192
writer.openBlock("const eventMarshallingVisitor = (event: any): __Message => $T.visit(event, {", "});",
189-
eventsUnionSymbol, () -> {
190-
eventsUnion.getAllMembers().forEach((memberName, memberShape) -> {
193+
eventsUnionSymbol, () -> {
194+
eventsUnion.getAllMembers().forEach((memberName, memberShape) -> {
191195
StructureShape target = model.expectShape(memberShape.getTarget(), StructureShape.class);
192-
String eventSerMethodName = getEventSerFunctionName(context, target);
193-
writer.write("$L: value => $L(value, context),", memberName, eventSerMethodName);
194-
});
195-
writer.write("_: value => value as any");
196-
});
197-
writer.write("return context.eventStreamMarshaller.serialize(input, eventMarshallingVisitor);");
198-
});
196+
String eventSerMethodName = getEventSerFunctionName(context, target);
197+
writer.write("$L: value => $L(value, context),", memberName, eventSerMethodName);
198+
});
199+
writer.write("_: value => value as any");
200+
});
201+
writer.write("return context.eventStreamMarshaller.serialize(input, eventMarshallingVisitor);");
202+
});
199203
}
200204

201205
private String getSerFunctionName(GenerationContext context, Shape shape) {
202206
Symbol symbol = getSymbol(context, shape);
203-
String protocolName = context.getProtocolName();
204-
return ProtocolGenerator.getSerFunctionName(symbol, protocolName);
207+
return ProtocolGenerator.getSerFunctionShortName(symbol);
205208
}
206209

207210
public String getEventSerFunctionName(GenerationContext context, Shape shape) {
@@ -226,10 +229,10 @@ private Symbol getSymbol(GenerationContext context, Shape shape) {
226229
}
227230

228231
public void generateEventMarshaller(
229-
GenerationContext context,
230-
StructureShape event,
231-
String documentContentType,
232-
Runnable serializeInputEventDocumentPayload,
232+
GenerationContext context,
233+
StructureShape event,
234+
String documentContentType,
235+
Runnable serializeInputEventDocumentPayload,
233236
Set<Shape> documentShapesToSerialize
234237
) {
235238
String methodName = getEventSerFunctionName(context, event);
@@ -240,17 +243,17 @@ public void generateEventMarshaller(
240243
+ " input: $T,\n"
241244
+ " context: __SerdeContext\n"
242245
+ "): __Message => {", "}", methodName, symbol, () -> {
243-
writer.openBlock("const headers: __MessageHeaders = {", "}", () -> {
244-
//fix headers required by event stream
245-
writer.write("\":event-type\": { type: \"string\", value: $S },", symbol.getName());
246-
writer.write("\":message-type\": { type: \"string\", value: \"event\" },");
247-
writeEventContentTypeHeader(context, event, documentContentType);
248-
});
249-
writeEventHeaders(context, event);
250-
writeEventBody(context, event, serializeInputEventDocumentPayload,
251-
documentShapesToSerialize);
252-
writer.openBlock("return { headers, body };");
253-
});
246+
writer.openBlock("const headers: __MessageHeaders = {", "}", () -> {
247+
//fix headers required by event stream
248+
writer.write("\":event-type\": { type: \"string\", value: $S },", symbol.getName());
249+
writer.write("\":message-type\": { type: \"string\", value: \"event\" },");
250+
writeEventContentTypeHeader(context, event, documentContentType);
251+
});
252+
writeEventHeaders(context, event);
253+
writeEventBody(context, event, serializeInputEventDocumentPayload,
254+
documentShapesToSerialize);
255+
writer.openBlock("return { headers, body };");
256+
});
254257
}
255258

256259
private void writeEventContentTypeHeader(
@@ -280,8 +283,8 @@ private Optional<MemberShape> getEventPayloadMember(StructureShape event) {
280283
.filter(member -> member.hasTrait(EventPayloadTrait.class))
281284
.collect(Collectors.toList());
282285
return payloadMembers.isEmpty()
283-
? Optional.empty() // implicit payload
284-
: Optional.of(payloadMembers.get(0));
286+
? Optional.empty() // implicit payload
287+
: Optional.of(payloadMembers.get(0));
285288
}
286289

287290
private void writeEventHeaders(GenerationContext context, StructureShape event) {
@@ -324,8 +327,8 @@ private String getEventHeaderType(Shape shape) {
324327
*/
325328
private MemberShape getExplicitEventPayloadMember(StructureShape event) {
326329
return event.getAllMembers().values().stream()
327-
.filter(member -> member.hasTrait(EventPayloadTrait.class))
328-
.collect(Collectors.toList()).get(0);
330+
.filter(member -> member.hasTrait(EventPayloadTrait.class))
331+
.collect(Collectors.toList()).get(0);
329332
}
330333

331334
private void writeEventBody(
@@ -347,13 +350,13 @@ private void writeEventBody(
347350
writer.write("body = context.utf8Decoder(input.$L);", payloadMemberName);
348351
} else if (payloadShape instanceof BlobShape || payloadShape instanceof StringShape) {
349352
Symbol symbol = getSymbol(context, payloadShape);
350-
String serFunctionName = ProtocolGenerator.getSerFunctionName(symbol, context.getProtocolName());
353+
String serFunctionName = ProtocolGenerator.getSerFunctionShortName(symbol);
351354
documentShapesToSerialize.add(payloadShape);
352355
writer.write("body = $L(input.$L, context);", payloadMemberName, serFunctionName);
353356
serializeInputEventDocumentPayload.run();
354357
} else {
355358
throw new CodegenException(String.format("Unexpected shape type bound to event payload: `%s`",
356-
payloadShape.getType()));
359+
payloadShape.getType()));
357360
}
358361
});
359362
} else {
@@ -364,7 +367,7 @@ private void writeEventBody(
364367
}
365368
}
366369
Symbol symbol = getSymbol(context, event);
367-
String serFunctionName = ProtocolGenerator.getSerFunctionName(symbol, context.getProtocolName());
370+
String serFunctionName = ProtocolGenerator.getSerFunctionShortName(symbol);
368371
documentShapesToSerialize.add(event);
369372
writer.write("body = $L(input, context);", serFunctionName);
370373
serializeInputEventDocumentPayload.run();
@@ -373,36 +376,39 @@ private void writeEventBody(
373376

374377
private void generateEventStreamDeserializer(GenerationContext context, UnionShape eventsUnion) {
375378
String methodName = getDeserFunctionName(context, eventsUnion);
379+
String methodLongName = ProtocolGenerator.getDeserFunctionName(getSymbol(context, eventsUnion),
380+
context.getProtocolName());
376381
Symbol eventsUnionSymbol = getSymbol(context, eventsUnion);
377382
TypeScriptWriter writer = context.getWriter();
378383
Model model = context.getModel();
379384
String contextType = getEventStreamSerdeContextType(context, eventsUnion);
385+
386+
writer.writeDocs(methodLongName);
380387
writer.openBlock("const $L = (\n"
381388
+ " output: any,\n"
382389
+ " context: $L\n"
383390
+ "): AsyncIterable<$T> => {", "}", methodName, contextType, eventsUnionSymbol, () -> {
384-
writer.openBlock("return context.eventStreamMarshaller.deserialize(", ");", () -> {
385-
writer.write("output,");
386-
writer.openBlock("async event => {", "}", () -> {
387-
eventsUnion.getAllMembers().forEach((name, member) -> {
388-
StructureShape event = model.expectShape(member.getTarget(), StructureShape.class);
389-
writer.openBlock("if (event[$S] != null) {", "}", name, () -> {
390-
writer.openBlock("return {", "};", () -> {
391-
String eventDeserMethodName = getEventDeserFunctionName(context, event);
392-
writer.write("$1L: await $2L(event[$1S], context),", name, eventDeserMethodName);
391+
writer.openBlock("return context.eventStreamMarshaller.deserialize(", ");", () -> {
392+
writer.write("output,");
393+
writer.openBlock("async event => {", "}", () -> {
394+
eventsUnion.getAllMembers().forEach((name, member) -> {
395+
StructureShape event = model.expectShape(member.getTarget(), StructureShape.class);
396+
writer.openBlock("if (event[$S] != null) {", "}", name, () -> {
397+
writer.openBlock("return {", "};", () -> {
398+
String eventDeserMethodName = getEventDeserFunctionName(context, event);
399+
writer.write("$1L: await $2L(event[$1S], context),", name, eventDeserMethodName);
400+
});
401+
});
393402
});
403+
writer.write("return {$$unknown: output};");
394404
});
395405
});
396-
writer.write("return {$$unknown: output};");
397406
});
398-
});
399-
});
400407
}
401408

402409
private String getDeserFunctionName(GenerationContext context, Shape shape) {
403410
Symbol symbol = getSymbol(context, shape);
404-
String protocolName = context.getProtocolName();
405-
return ProtocolGenerator.getDeserFunctionName(symbol, protocolName);
411+
return ProtocolGenerator.getDeserFunctionShortName(symbol);
406412
}
407413

408414
public String getEventDeserFunctionName(GenerationContext context, Shape shape) {
@@ -423,15 +429,15 @@ public void generateEventUnmarshaller(
423429
+ " output: any,\n"
424430
+ " context: __SerdeContext\n"
425431
+ "): Promise<$T> => {", "}", methodName, symbol, () -> {
426-
if (event.hasTrait(ErrorTrait.class)) {
427-
generateErrorEventUnmarshaller(context, event, errorShapesToDeserialize, isErrorCodeInBody);
428-
} else {
429-
writer.write("const contents: $L = {} as any;", symbol.getName());
430-
readEventHeaders(context, event);
431-
readEventBody(context, event, eventShapesToDeserialize);
432-
writer.write("return contents;");
433-
}
434-
});
432+
if (event.hasTrait(ErrorTrait.class)) {
433+
generateErrorEventUnmarshaller(context, event, errorShapesToDeserialize, isErrorCodeInBody);
434+
} else {
435+
writer.write("const contents: $L = {} as any;", symbol.getName());
436+
readEventHeaders(context, event);
437+
readEventBody(context, event, eventShapesToDeserialize);
438+
writer.write("return contents;");
439+
}
440+
});
435441
}
436442

437443
// Writes function content that unmarshall error event with error deserializer
@@ -444,7 +450,7 @@ private void generateErrorEventUnmarshaller(
444450
TypeScriptWriter writer = context.getWriter();
445451
// If this is an error event, we need to generate the error deserializer.
446452
errorShapesToDeserialize.add(event);
447-
String errorDeserMethodName = getDeserFunctionName(context, event) + "Response";
453+
String errorDeserMethodName = getDeserFunctionName(context, event) + "Res";
448454
if (isErrorCodeInBody) {
449455
// If error code is in body, parseBody() won't be called inside error deser. So we parse body here.
450456
// It's ok to parse body here because body won't be streaming if 'isErrorCodeInBody' is set.
@@ -489,14 +495,14 @@ private void readEventBody(
489495
} else if (payloadShape instanceof StructureShape || payloadShape instanceof UnionShape) {
490496
writer.write("const data: any = await parseBody(output.body, context);");
491497
Symbol symbol = getSymbol(context, payloadShape);
492-
String deserFunctionName = ProtocolGenerator.getDeserFunctionName(symbol, context.getProtocolName());
498+
String deserFunctionName = ProtocolGenerator.getDeserFunctionShortName(symbol);
493499
writer.write("contents.$L = $L(data, context);", payloadMemberName, deserFunctionName);
494500
eventShapesToDeserialize.add(payloadShape);
495501
}
496502
} else {
497503
writer.write("const data: any = await parseBody(output.body, context);");
498504
Symbol symbol = getSymbol(context, event);
499-
String deserFunctionName = ProtocolGenerator.getDeserFunctionName(symbol, context.getProtocolName());
505+
String deserFunctionName = ProtocolGenerator.getDeserFunctionShortName(symbol);
500506
writer.write("Object.assign(contents, $L(data, context));", deserFunctionName);
501507
eventShapesToDeserialize.add(event);
502508
}

0 commit comments

Comments
 (0)