1
1
/*
2
2
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
3
*
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
7
6
*
8
- * http://aws.amazon.com/apache2.0
7
+ * http://aws.amazon.com/apache2.0
9
8
*
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.
14
12
*/
15
13
16
14
package software .amazon .smithy .typescript .codegen .integration ;
30
28
import software .amazon .smithy .typescript .codegen .TypeScriptDelegator ;
31
29
import software .amazon .smithy .typescript .codegen .TypeScriptSettings ;
32
30
import software .amazon .smithy .typescript .codegen .TypeScriptWriter ;
33
- import software .amazon .smithy .typescript .codegen .protocol .SerdeFunctionNameShortener ;
34
31
import software .amazon .smithy .utils .CaseUtils ;
35
32
import software .amazon .smithy .utils .SmithyUnstableApi ;
36
33
40
37
@ SmithyUnstableApi
41
38
public interface ProtocolGenerator {
42
39
String PROTOCOLS_FOLDER = "protocols" ;
43
- SerdeFunctionNameShortener NAME_SHORTENER = new SerdeFunctionNameShortener ();
44
40
45
41
/**
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.
48
43
*
49
44
* <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".
53
47
*
54
48
* @param name Name of the protocol to sanitize.
55
49
* @return Returns the sanitized name.
@@ -70,8 +64,8 @@ static String getSanitizedName(String name) {
70
64
* Gets the name of the protocol.
71
65
*
72
66
* <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").
75
69
*
76
70
* @return Returns the protocol name.
77
71
*/
@@ -87,36 +81,30 @@ default String getName() {
87
81
ApplicationProtocol getApplicationProtocol ();
88
82
89
83
/**
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.
93
86
*
94
87
* <p>
95
88
* 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.
100
93
*
101
94
* <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.
104
97
*
105
- * @param service Service being generated.
98
+ * @param service Service being generated.
106
99
* @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.
108
101
* @return Returns the resolved application protocol object.
109
102
*/
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 ) {
114
105
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 (", " ));
120
108
throw new CodegenException (String .format (
121
109
"All of the protocols generated for a service must be runtime compatible, but "
122
110
+ "protocol `%s` is incompatible with other application protocols: [%s]. Please pick a "
@@ -133,27 +121,25 @@ default ApplicationProtocol resolveApplicationProtocol(
133
121
* @param context Serde context.
134
122
*/
135
123
default void generateSharedComponents (GenerationContext context ) {
124
+ //
136
125
}
137
126
138
127
/**
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.
141
129
*
142
130
* @param context Serialization context.
143
131
*/
144
132
void generateRequestSerializers (GenerationContext context );
145
133
146
134
/**
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.
149
136
*
150
137
* @param context Serialization context.
151
138
*/
152
139
void generateRequestDeserializers (GenerationContext context );
153
140
154
141
/**
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.
157
143
*
158
144
* @param context Serialization context.
159
145
*/
@@ -174,20 +160,17 @@ default void generateSharedComponents(GenerationContext context) {
174
160
void generateServiceHandlerFactory (GenerationContext context );
175
161
176
162
/**
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.
182
166
*
183
- * @param context Generation context.
167
+ * @param context Generation context.
184
168
* @param operation The operation to generate a handler factory for.
185
169
*/
186
170
void generateOperationHandlerFactory (GenerationContext context , OperationShape operation );
187
171
188
172
/**
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.
191
174
*
192
175
* @param context Deserialization context.
193
176
*/
@@ -203,7 +186,7 @@ default void generateSharedComponents(GenerationContext context) {
203
186
/**
204
187
* Generates the name of a serializer function for shapes of a service.
205
188
*
206
- * @param symbol The symbol the serializer function is being generated for.
189
+ * @param symbol The symbol the serializer function is being generated for.
207
190
* @param protocol Name of the protocol being generated.
208
191
* @return Returns the generated function name.
209
192
*/
@@ -212,7 +195,8 @@ static String getSerFunctionName(Symbol symbol, String protocol) {
212
195
String functionName = "serialize" + ProtocolGenerator .getSanitizedName (protocol );
213
196
214
197
// 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 ));
216
200
217
201
return functionName ;
218
202
}
@@ -225,27 +209,28 @@ static String getSerFunctionShortName(Symbol symbol) {
225
209
// e.g., se_ES for ExecuteStatement
226
210
String functionName = "se_" ;
227
211
// 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 ));
230
214
return functionName ;
231
215
}
232
216
233
217
/**
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.
236
220
*
237
221
* @param symbol The symbol the serializer function is being generated for.
238
222
* @return Returns the generated function name.
239
223
*/
240
224
static String getGenericSerFunctionName (Symbol symbol ) {
241
225
// e.g., serializeExecuteStatement
242
- return "serialize" + getSerdeFunctionSymbolComponent (symbol , symbol .expectProperty ("shape" , Shape .class ));
226
+ return "serialize" + getSerdeFunctionSymbolComponent (symbol ,
227
+ symbol .expectProperty ("shape" , Shape .class ));
243
228
}
244
229
245
230
/**
246
231
* Generates the name of a deserializer function for shapes of a service.
247
232
*
248
- * @param symbol The symbol the deserializer function is being generated for.
233
+ * @param symbol The symbol the deserializer function is being generated for.
249
234
* @param protocol Name of the protocol being generated.
250
235
* @return Returns the generated function name.
251
236
*/
@@ -254,7 +239,8 @@ static String getDeserFunctionName(Symbol symbol, String protocol) {
254
239
String functionName = "deserialize" + ProtocolGenerator .getSanitizedName (protocol );
255
240
256
241
// 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 ));
258
244
259
245
return functionName ;
260
246
}
@@ -267,22 +253,22 @@ static String getDeserFunctionShortName(Symbol symbol) {
267
253
// e.g., de_ES for ExecuteStatement
268
254
String functionName = "de_" ;
269
255
// 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 ));
272
258
return functionName ;
273
259
}
274
260
275
261
/**
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.
278
264
*
279
265
* @param symbol The symbol the deserializer function is being generated for.
280
266
* @return Returns the generated function name.
281
267
*/
282
268
static String getGenericDeserFunctionName (Symbol symbol ) {
283
269
// 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 ));
286
272
}
287
273
288
274
static String getSerdeFunctionSymbolComponent (Symbol symbol , Shape shape ) {
@@ -302,11 +288,12 @@ static String getSerdeFunctionSymbolComponent(Symbol symbol, Shape shape) {
302
288
/**
303
289
* Returns a map of error names to their {@link ShapeId}.
304
290
*
305
- * @param context the generation context
291
+ * @param context the generation context
306
292
* @param operation the operation shape to retrieve errors for
307
293
* @return map of error names to {@link ShapeId}
308
294
*/
309
- default Map <String , ShapeId > getOperationErrors (GenerationContext context , OperationShape operation ) {
295
+ default Map <String , ShapeId > getOperationErrors (GenerationContext context ,
296
+ OperationShape operation ) {
310
297
return HttpProtocolGeneratorUtils .getOperationErrors (context , operation );
311
298
}
312
299
0 commit comments