Skip to content

Commit 93dfc75

Browse files
authored
shorten internal serde fn names (#730)
* shorten internal serde names * formatting * line length fixes * fix missing rename in error deser
1 parent 9a52172 commit 93dfc75

File tree

12 files changed

+172
-111
lines changed

12 files changed

+172
-111
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: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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, {", "});",
189193
eventsUnionSymbol, () -> {
190194
eventsUnion.getAllMembers().forEach((memberName, memberShape) -> {
191-
StructureShape target = model.expectShape(memberShape.getTarget(), StructureShape.class);
195+
StructureShape target = model.expectShape(memberShape.getTarget(), StructureShape.class);
192196
String eventSerMethodName = getEventSerFunctionName(context, target);
193197
writer.write("$L: value => $L(value, context),", memberName, eventSerMethodName);
194198
});
195199
writer.write("_: value => value as any");
196200
});
197-
writer.write("return context.eventStreamMarshaller.serialize(input, eventMarshallingVisitor);");
198-
});
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) {
@@ -347,7 +350,7 @@ 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();
@@ -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,10 +376,14 @@ 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"
@@ -401,8 +408,7 @@ private void generateEventStreamDeserializer(GenerationContext context, UnionSha
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) {
@@ -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
}

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

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ public void generateServiceHandlerFactory(GenerationContext context) {
397397
generateServiceMux(context);
398398
writer.addImport("ServiceException", "__ServiceException", "@aws-smithy/server-common");
399399
writer.openBlock("const serFn: (op: $1T) => __OperationSerializer<$2T<Context>, $1T, __ServiceException> = "
400-
+ "(op) => {", "};", operationsSymbol, serviceSymbol, () -> {
400+
+ "(op) => {", "};", operationsSymbol, serviceSymbol, () -> {
401401
writer.openBlock("switch (op) {", "}", () -> {
402402
operations.stream()
403403
.filter(o -> o.getTrait(HttpTrait.class).isPresent())
@@ -440,12 +440,12 @@ public void generateOperationHandlerFactory(GenerationContext context, Operation
440440

441441
if (context.getSettings().isDisableDefaultValidation()) {
442442
writer.write("export const get$L = <Context>(operation: __Operation<$T, $T, Context>, "
443-
+ "customizer: __ValidationCustomizer<$S>): "
444-
+ "__ServiceHandler<Context, __HttpRequest, __HttpResponse> => {",
443+
+ "customizer: __ValidationCustomizer<$S>): "
444+
+ "__ServiceHandler<Context, __HttpRequest, __HttpResponse> => {",
445445
operationHandlerSymbol.getName(), inputType, outputType, operationSymbol.getName());
446446
} else {
447447
writer.write("export const get$L = <Context>(operation: __Operation<$T, $T, Context>): "
448-
+ "__ServiceHandler<Context, __HttpRequest, __HttpResponse> => {",
448+
+ "__ServiceHandler<Context, __HttpRequest, __HttpResponse> => {",
449449
operationHandlerSymbol.getName(), inputType, outputType);
450450
}
451451
writer.indent();
@@ -642,19 +642,25 @@ private void generateOperationRequestSerializer(
642642
// Ensure that the request type is imported.
643643
writer.addUseImports(requestType);
644644
writer.addImport("Endpoint", "__Endpoint", "@aws-sdk/types");
645+
646+
// e.g., se_ES
647+
String methodName = ProtocolGenerator.getSerFunctionShortName(symbol);
645648
// e.g., serializeAws_restJson1_1ExecuteStatement
646-
String methodName = ProtocolGenerator.getSerFunctionName(symbol, getName());
649+
String methodLongName = ProtocolGenerator.getSerFunctionName(symbol, getName());
650+
647651
// Add the normalized input type.
648652
Symbol inputType = symbol.expectProperty("inputType", Symbol.class);
649653
String contextType = CodegenUtils.getOperationSerializerContextType(writer, context.getModel(), operation);
650654

655+
writer.writeDocs(methodLongName);
651656
writer.openBlock("export const $L = async(\n"
652657
+ " input: $T,\n"
653658
+ " context: $L\n"
654659
+ "): Promise<$T> => {", "}", methodName, inputType, contextType, requestType, () -> {
655660

656-
// Get the hostname, path, port, and scheme from client's resolved endpoint. Then construct the request from
657-
// them. The client's resolved endpoint can be default one or supplied by users.
661+
// Get the hostname, path, port, and scheme from client's resolved endpoint.
662+
// Then construct the request from them. The client's resolved endpoint can
663+
// be default one or supplied by users.
658664
writer.write("const {hostname, protocol = $S, port, path: basePath} = await context.endpoint();", "https");
659665

660666
writeRequestHeaders(context, operation, bindingIndex);
@@ -777,12 +783,12 @@ private void writeResolvedPath(
777783
Shape target = model.expectShape(binding.getMember().getTarget());
778784

779785
String labelValueProvider = "() => " + getInputValue(
780-
context,
781-
binding.getLocation(),
782-
"input." + memberName + "!",
783-
binding.getMember(),
784-
target
785-
);
786+
context,
787+
binding.getLocation(),
788+
"input." + memberName + "!",
789+
binding.getMember(),
790+
target
791+
);
786792

787793
// Get the correct label to use.
788794
Segment uriLabel = uriLabels.stream().filter(s -> s.getContent().equals(memberName)).findFirst().get();
@@ -1342,7 +1348,7 @@ private String getNamedMembersInputParam(
13421348
switch (bindingType) {
13431349
case PAYLOAD:
13441350
Symbol symbol = context.getSymbolProvider().toSymbol(target);
1345-
return ProtocolGenerator.getSerFunctionName(symbol, context.getProtocolName())
1351+
return ProtocolGenerator.getSerFunctionShortName(symbol)
13461352
+ "(" + dataSource + ", context)";
13471353
default:
13481354
throw new CodegenException("Unexpected named member shape binding location `" + bindingType + "`");
@@ -1887,17 +1893,18 @@ private void readDirectQueryBindings(GenerationContext context, List<HttpBinding
18871893
"@aws-smithy/server-common");
18881894
writer.write("let queryValue: string;");
18891895
writer.openBlock("if (Array.isArray(query[$S])) {", "}",
1890-
binding.getLocationName(),
1891-
() -> {
1892-
writer.openBlock("if (query[$S].length === 1) {", "}",
1893-
binding.getLocationName(),
1894-
() -> {
1895-
writer.write("queryValue = query[$S][0];", binding.getLocationName());
1896-
});
1897-
writer.openBlock("else {", "}", () -> {
1898-
writer.write("throw new __SerializationException();");
1899-
});
1896+
binding.getLocationName(),
1897+
() -> {
1898+
writer.openBlock("if (query[$S].length === 1) {", "}",
1899+
binding.getLocationName(),
1900+
() -> {
1901+
writer.write("queryValue = query[$S][0];", binding.getLocationName());
1902+
}
1903+
);
1904+
writer.openBlock("else {", "}", () -> {
1905+
writer.write("throw new __SerializationException();");
19001906
});
1907+
});
19011908
writer.openBlock("else {", "}", () -> {
19021909
writer.write("queryValue = query[$S] as string;", binding.getLocationName());
19031910
});
@@ -2052,18 +2059,21 @@ private void generateOperationResponseDeserializer(
20522059
// Ensure that the response type is imported.
20532060
writer.addUseImports(responseType);
20542061
// e.g., deserializeAws_restJson1_1ExecuteStatement
2055-
String methodName = ProtocolGenerator.getDeserFunctionName(symbol, getName());
2062+
String methodName = ProtocolGenerator.getDeserFunctionShortName(symbol);
2063+
String methodLongName = ProtocolGenerator.getDeserFunctionName(symbol, getName());
20562064
String errorMethodName = methodName + "Error";
20572065
// Add the normalized output type.
20582066
Symbol outputType = symbol.expectProperty("outputType", Symbol.class);
20592067
String contextType = CodegenUtils.getOperationDeserializerContextType(context.getSettings(), writer,
20602068
context.getModel(), operation);
20612069

20622070
// Handle the general response.
2071+
writer.writeDocs(methodLongName);
20632072
writer.openBlock("export const $L = async(\n"
20642073
+ " output: $T,\n"
20652074
+ " context: $L\n"
2066-
+ "): Promise<$T> => {", "}", methodName, responseType, contextType, outputType, () -> {
2075+
+ "): Promise<$T> => {", "}",
2076+
methodName, responseType, contextType, outputType, () -> {
20672077
// Redirect error deserialization to the dispatcher if we receive an error range
20682078
// status code that's not the modeled code (300 or higher). This allows for
20692079
// returning other 2XX codes that don't match the defined value.
@@ -2103,10 +2113,13 @@ private void generateErrorDeserializer(GenerationContext context, StructureShape
21032113
HttpBindingIndex bindingIndex = HttpBindingIndex.of(context.getModel());
21042114
Model model = context.getModel();
21052115
Symbol errorSymbol = symbolProvider.toSymbol(error);
2106-
String errorDeserMethodName = ProtocolGenerator.getDeserFunctionName(errorSymbol,
2107-
context.getProtocolName()) + "Response";
2116+
String errorDeserMethodName = ProtocolGenerator.getDeserFunctionShortName(errorSymbol) + "Res";
2117+
String errorDeserMethodLongName = ProtocolGenerator.getDeserFunctionName(errorSymbol, context.getProtocolName())
2118+
+ "Res";
2119+
21082120
String outputName = isErrorCodeInBody ? "parsedOutput" : "output";
21092121

2122+
writer.writeDocs(errorDeserMethodLongName);
21102123
writer.openBlock("const $L = async (\n"
21112124
+ " $L: any,\n"
21122125
+ " context: __SerdeContext\n"
@@ -2661,8 +2674,8 @@ private String getNamedMembersOutputParam(
26612674
case PAYLOAD:
26622675
// Redirect to a deserialization function.
26632676
Symbol symbol = context.getSymbolProvider().toSymbol(target);
2664-
return ProtocolGenerator.getDeserFunctionName(symbol, context.getProtocolName())
2665-
+ "(" + dataSource + ", context)";
2677+
return ProtocolGenerator.getDeserFunctionShortName(symbol)
2678+
+ "(" + dataSource + ", context)";
26662679
default:
26672680
throw new CodegenException("Unexpected named member shape binding location `" + bindingType + "`");
26682681
}

0 commit comments

Comments
 (0)