Skip to content

Commit 3bfd63d

Browse files
committed
preserve shape names
1 parent c39d59d commit 3bfd63d

File tree

7 files changed

+153
-307
lines changed

7 files changed

+153
-307
lines changed

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

Lines changed: 93 additions & 121 deletions
Large diffs are not rendered by default.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ private void generateOperationResponseSerializer(
532532
TypeScriptWriter writer = context.getWriter();
533533

534534
writer.addUseImports(responseType);
535-
String methodName = ProtocolGenerator.getGenericSerFunctionName(symbol) + "Res";
535+
String methodName = ProtocolGenerator.getGenericSerFunctionName(symbol) + "Response";
536536
Symbol outputType = symbol.expectProperty("outputType", Symbol.class);
537537
writer.addImport("ServerSerdeContext", null, "@aws-smithy/server-common");
538538

@@ -1735,7 +1735,7 @@ private void generateOperationRequestDeserializer(
17351735
// Ensure that the request type is imported.
17361736
writer.addUseImports(requestType);
17371737
writer.addImport("Endpoint", "__Endpoint", "@aws-sdk/types");
1738-
String methodName = ProtocolGenerator.getGenericDeserFunctionName(symbol) + "Req";
1738+
String methodName = ProtocolGenerator.getGenericDeserFunctionName(symbol) + "Request";
17391739
// Add the normalized input type.
17401740
Symbol inputType = symbol.expectProperty("inputType", Symbol.class);
17411741
String contextType = CodegenUtils.getOperationSerializerContextType(writer, context.getModel(), operation);

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

Lines changed: 55 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
/*
22
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
*
4-
* Licensed under the Apache License, Version 2.0 (the "License").
5-
* You may not use this file except in compliance with the License.
6-
* A copy of the License is located at
4+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except
5+
* in compliance with the License. A copy of the License is located at
76
*
8-
* http://aws.amazon.com/apache2.0
7+
* http://aws.amazon.com/apache2.0
98
*
10-
* or in the "license" file accompanying this file. This file is distributed
11-
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12-
* express or implied. See the License for the specific language governing
13-
* permissions and limitations under the License.
9+
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
1412
*/
1513

1614
package software.amazon.smithy.typescript.codegen.integration;
@@ -30,7 +28,6 @@
3028
import software.amazon.smithy.typescript.codegen.TypeScriptDelegator;
3129
import software.amazon.smithy.typescript.codegen.TypeScriptSettings;
3230
import software.amazon.smithy.typescript.codegen.TypeScriptWriter;
33-
import software.amazon.smithy.typescript.codegen.protocol.SerdeFunctionNameShortener;
3431
import software.amazon.smithy.utils.CaseUtils;
3532
import software.amazon.smithy.utils.SmithyUnstableApi;
3633

@@ -40,16 +37,13 @@
4037
@SmithyUnstableApi
4138
public interface ProtocolGenerator {
4239
String PROTOCOLS_FOLDER = "protocols";
43-
SerdeFunctionNameShortener NAME_SHORTENER = new SerdeFunctionNameShortener();
4440

4541
/**
46-
* Sanitizes the name of the protocol so it can be used as a symbol
47-
* in TypeScript.
42+
* Sanitizes the name of the protocol so it can be used as a symbol in TypeScript.
4843
*
4944
* <p>
50-
* For example, the default implementation converts "." to "_",
51-
* and converts "-" to become camelCase separated words. This means
52-
* that "aws.rest-json-1.1" becomes "Aws_RestJson1_1".
45+
* For example, the default implementation converts "." to "_", and converts "-" to become
46+
* camelCase separated words. This means that "aws.rest-json-1.1" becomes "Aws_RestJson1_1".
5347
*
5448
* @param name Name of the protocol to sanitize.
5549
* @return Returns the sanitized name.
@@ -70,8 +64,8 @@ static String getSanitizedName(String name) {
7064
* Gets the name of the protocol.
7165
*
7266
* <p>
73-
* The default implementation is the ShapeId name of the protocol trait in
74-
* Smithy models (e.g., "aws.protocols#restJson1" would return "restJson1").
67+
* The default implementation is the ShapeId name of the protocol trait in Smithy models (e.g.,
68+
* "aws.protocols#restJson1" would return "restJson1").
7569
*
7670
* @return Returns the protocol name.
7771
*/
@@ -87,36 +81,30 @@ default String getName() {
8781
ApplicationProtocol getApplicationProtocol();
8882

8983
/**
90-
* Determines if two protocol generators are compatible at the
91-
* application protocol level, meaning they both use HTTP, or MQTT
92-
* for example.
84+
* Determines if two protocol generators are compatible at the application protocol level,
85+
* meaning they both use HTTP, or MQTT for example.
9386
*
9487
* <p>
9588
* Two protocol implementations are considered compatible if the
96-
* {@link ApplicationProtocol#equals} method of {@link #getApplicationProtocol}
97-
* returns true when called with {@code other}. The default implementation
98-
* should work for most interfaces, but may be overridden for more in-depth
99-
* handling of things like minor version incompatibilities.
89+
* {@link ApplicationProtocol#equals} method of {@link #getApplicationProtocol} returns true
90+
* when called with {@code other}. The default implementation should work for most interfaces,
91+
* but may be overridden for more in-depth handling of things like minor version
92+
* incompatibilities.
10093
*
10194
* <p>
102-
* By default, if the application protocols are considered equal, then
103-
* {@code other} is returned.
95+
* By default, if the application protocols are considered equal, then {@code other} is
96+
* returned.
10497
*
105-
* @param service Service being generated.
98+
* @param service Service being generated.
10699
* @param protocolGenerators Other protocol generators that are being generated.
107-
* @param other Protocol generator to resolve against.
100+
* @param other Protocol generator to resolve against.
108101
* @return Returns the resolved application protocol object.
109102
*/
110-
default ApplicationProtocol resolveApplicationProtocol(
111-
ServiceShape service,
112-
Collection<ProtocolGenerator> protocolGenerators,
113-
ApplicationProtocol other) {
103+
default ApplicationProtocol resolveApplicationProtocol(ServiceShape service,
104+
Collection<ProtocolGenerator> protocolGenerators, ApplicationProtocol other) {
114105
if (!getApplicationProtocol().equals(other)) {
115-
String protocolNames = protocolGenerators.stream()
116-
.map(ProtocolGenerator::getProtocol)
117-
.map(ShapeId::getName)
118-
.sorted()
119-
.collect(Collectors.joining(", "));
106+
String protocolNames = protocolGenerators.stream().map(ProtocolGenerator::getProtocol)
107+
.map(ShapeId::getName).sorted().collect(Collectors.joining(", "));
120108
throw new CodegenException(String.format(
121109
"All of the protocols generated for a service must be runtime compatible, but "
122110
+ "protocol `%s` is incompatible with other application protocols: [%s]. Please pick a "
@@ -133,27 +121,25 @@ default ApplicationProtocol resolveApplicationProtocol(
133121
* @param context Serde context.
134122
*/
135123
default void generateSharedComponents(GenerationContext context) {
124+
//
136125
}
137126

138127
/**
139-
* Generates the code used to serialize the shapes of a service
140-
* for requests.
128+
* Generates the code used to serialize the shapes of a service for requests.
141129
*
142130
* @param context Serialization context.
143131
*/
144132
void generateRequestSerializers(GenerationContext context);
145133

146134
/**
147-
* Generates the code used to deserialize the shapes of a service
148-
* for requests.
135+
* Generates the code used to deserialize the shapes of a service for requests.
149136
*
150137
* @param context Serialization context.
151138
*/
152139
void generateRequestDeserializers(GenerationContext context);
153140

154141
/**
155-
* Generates the code used to serialize the shapes of a service
156-
* for responses.
142+
* Generates the code used to serialize the shapes of a service for responses.
157143
*
158144
* @param context Serialization context.
159145
*/
@@ -174,20 +160,17 @@ default void generateSharedComponents(GenerationContext context) {
174160
void generateServiceHandlerFactory(GenerationContext context);
175161

176162
/**
177-
* Generates the code used to handle a request for a specific operation in the
178-
* given service. This allows the
179-
* business logic for a service to be split among multiple deployment targets,
180-
* for example, one Lambda function
181-
* per operation.
163+
* Generates the code used to handle a request for a specific operation in the given service.
164+
* This allows the business logic for a service to be split among multiple deployment targets,
165+
* for example, one Lambda function per operation.
182166
*
183-
* @param context Generation context.
167+
* @param context Generation context.
184168
* @param operation The operation to generate a handler factory for.
185169
*/
186170
void generateOperationHandlerFactory(GenerationContext context, OperationShape operation);
187171

188172
/**
189-
* Generates the code used to deserialize the shapes of a service
190-
* for responses.
173+
* Generates the code used to deserialize the shapes of a service for responses.
191174
*
192175
* @param context Deserialization context.
193176
*/
@@ -203,7 +186,7 @@ default void generateSharedComponents(GenerationContext context) {
203186
/**
204187
* Generates the name of a serializer function for shapes of a service.
205188
*
206-
* @param symbol The symbol the serializer function is being generated for.
189+
* @param symbol The symbol the serializer function is being generated for.
207190
* @param protocol Name of the protocol being generated.
208191
* @return Returns the generated function name.
209192
*/
@@ -212,7 +195,8 @@ static String getSerFunctionName(Symbol symbol, String protocol) {
212195
String functionName = "serialize" + ProtocolGenerator.getSanitizedName(protocol);
213196

214197
// Update the function to have a component based on the symbol.
215-
functionName += getSerdeFunctionSymbolComponent(symbol, symbol.expectProperty("shape", Shape.class));
198+
functionName += getSerdeFunctionSymbolComponent(symbol,
199+
symbol.expectProperty("shape", Shape.class));
216200

217201
return functionName;
218202
}
@@ -225,27 +209,28 @@ static String getSerFunctionShortName(Symbol symbol) {
225209
// e.g., se_ES for ExecuteStatement
226210
String functionName = "se_";
227211
// Update the function to have a component based on the symbol.
228-
functionName += NAME_SHORTENER
229-
.getAbbreviatedName(symbol, symbol.expectProperty("shape", Shape.class));
212+
functionName += ProtocolGenerator.getSerdeFunctionSymbolComponent(symbol,
213+
symbol.expectProperty("shape", Shape.class));
230214
return functionName;
231215
}
232216

233217
/**
234-
* Generates the name of a serializer function for shapes of a service that is
235-
* not protocol-specific.
218+
* Generates the name of a serializer function for shapes of a service that is not
219+
* protocol-specific.
236220
*
237221
* @param symbol The symbol the serializer function is being generated for.
238222
* @return Returns the generated function name.
239223
*/
240224
static String getGenericSerFunctionName(Symbol symbol) {
241225
// e.g., serializeExecuteStatement
242-
return "serialize" + getSerdeFunctionSymbolComponent(symbol, symbol.expectProperty("shape", Shape.class));
226+
return "serialize" + getSerdeFunctionSymbolComponent(symbol,
227+
symbol.expectProperty("shape", Shape.class));
243228
}
244229

245230
/**
246231
* Generates the name of a deserializer function for shapes of a service.
247232
*
248-
* @param symbol The symbol the deserializer function is being generated for.
233+
* @param symbol The symbol the deserializer function is being generated for.
249234
* @param protocol Name of the protocol being generated.
250235
* @return Returns the generated function name.
251236
*/
@@ -254,7 +239,8 @@ static String getDeserFunctionName(Symbol symbol, String protocol) {
254239
String functionName = "deserialize" + ProtocolGenerator.getSanitizedName(protocol);
255240

256241
// Update the function to have a component based on the symbol.
257-
functionName += getSerdeFunctionSymbolComponent(symbol, symbol.expectProperty("shape", Shape.class));
242+
functionName += getSerdeFunctionSymbolComponent(symbol,
243+
symbol.expectProperty("shape", Shape.class));
258244

259245
return functionName;
260246
}
@@ -267,22 +253,22 @@ static String getDeserFunctionShortName(Symbol symbol) {
267253
// e.g., de_ES for ExecuteStatement
268254
String functionName = "de_";
269255
// Update the function to have a component based on the symbol.
270-
functionName += NAME_SHORTENER
271-
.getAbbreviatedName(symbol, symbol.expectProperty("shape", Shape.class));
256+
functionName += ProtocolGenerator.getSerdeFunctionSymbolComponent(symbol,
257+
symbol.expectProperty("shape", Shape.class));
272258
return functionName;
273259
}
274260

275261
/**
276-
* Generates the name of a deserializer function for shapes of a service that is
277-
* not protocol-specific.
262+
* Generates the name of a deserializer function for shapes of a service that is not
263+
* protocol-specific.
278264
*
279265
* @param symbol The symbol the deserializer function is being generated for.
280266
* @return Returns the generated function name.
281267
*/
282268
static String getGenericDeserFunctionName(Symbol symbol) {
283269
// e.g., deserializeExecuteStatement
284-
return "deserialize"
285-
+ getSerdeFunctionSymbolComponent(symbol, symbol.expectProperty("shape", Shape.class));
270+
return "deserialize" + getSerdeFunctionSymbolComponent(symbol,
271+
symbol.expectProperty("shape", Shape.class));
286272
}
287273

288274
static String getSerdeFunctionSymbolComponent(Symbol symbol, Shape shape) {
@@ -302,11 +288,12 @@ static String getSerdeFunctionSymbolComponent(Symbol symbol, Shape shape) {
302288
/**
303289
* Returns a map of error names to their {@link ShapeId}.
304290
*
305-
* @param context the generation context
291+
* @param context the generation context
306292
* @param operation the operation shape to retrieve errors for
307293
* @return map of error names to {@link ShapeId}
308294
*/
309-
default Map<String, ShapeId> getOperationErrors(GenerationContext context, OperationShape operation) {
295+
default Map<String, ShapeId> getOperationErrors(GenerationContext context,
296+
OperationShape operation) {
310297
return HttpProtocolGeneratorUtils.getOperationErrors(context, operation);
311298
}
312299

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/protocol/SerdeFunctionNameShortener.java

Lines changed: 0 additions & 63 deletions
This file was deleted.

smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/integration/DocumentMemberDeserVisitorTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public static Collection<Object[]> validMemberTargetTypes() {
8181
MemberShape member = MemberShape.builder().id(id + "$member").target(targetId).build();
8282
MemberShape key = MemberShape.builder().id(id + "$key").target(targetId).build();
8383
MemberShape value = MemberShape.builder().id(id + "$value").target(targetId).build();
84-
String delegate = "de_F"
84+
String delegate = "de_Foo"
8585
+ "(" + DATA_SOURCE + ", context)";
8686

8787
return ListUtils.of(new Object[][] {
@@ -121,7 +121,7 @@ public static Collection<Object[]> validMemberTargetTypes() {
121121
},
122122
{
123123
UnionShape.builder().id(id).addMember(member).build(),
124-
"de_F(__expectUnion(" + DATA_SOURCE + "), context)",
124+
"de_Foo(__expectUnion(" + DATA_SOURCE + "), context)",
125125
source
126126
},
127127
});

smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/integration/DocumentMemberSerVisitorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public static Collection<Object[]> validMemberTargetTypes() {
6666
MemberShape member = MemberShape.builder().id(id + "$member").target(targetId).build();
6767
MemberShape key = MemberShape.builder().id(id + "$key").target(targetId).build();
6868
MemberShape value = MemberShape.builder().id(id + "$value").target(targetId).build();
69-
String delegate = "se_F(" + DATA_SOURCE + ", context)";
69+
String delegate = "se_Foo(" + DATA_SOURCE + ", context)";
7070

7171
return ListUtils.of(new Object[][] {
7272
{ BooleanShape.builder().id(id).build(), DATA_SOURCE },

0 commit comments

Comments
 (0)