@@ -90,10 +90,10 @@ public static UnionShape getEventStreamOutputShape(GenerationContext context, Op
90
90
* Shapes that referred by event will be added.
91
91
*/
92
92
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 ,
97
97
Set <Shape > documentShapesToSerialize
98
98
) {
99
99
Model model = context .getModel ();
@@ -118,11 +118,11 @@ public void generateEventStreamSerializers(
118
118
});
119
119
eventShapesToMarshall .forEach (event -> {
120
120
generateEventMarshaller (
121
- context ,
122
- event ,
123
- documentContentType ,
124
- serializeInputEventDocumentPayload ,
125
- documentShapesToSerialize );
121
+ context ,
122
+ event ,
123
+ documentContentType ,
124
+ serializeInputEventDocumentPayload ,
125
+ documentShapesToSerialize );
126
126
});
127
127
}
128
128
@@ -177,31 +177,34 @@ public void generateEventStreamDeserializers(
177
177
178
178
private void generateEventStreamSerializer (GenerationContext context , UnionShape eventsUnion ) {
179
179
String methodName = getSerFunctionName (context , eventsUnion );
180
+ String methodLongName = ProtocolGenerator .getSerFunctionName (getSymbol (context , eventsUnion ),
181
+ context .getProtocolName ());
180
182
Symbol eventsUnionSymbol = getSymbol (context , eventsUnion );
181
183
TypeScriptWriter writer = context .getWriter ();
182
184
Model model = context .getModel ();
183
185
writer .addImport ("Message" , "__Message" , TypeScriptDependency .AWS_SDK_TYPES .packageName );
186
+
187
+ writer .writeDocs (methodLongName );
184
188
writer .openBlock ("const $L = (\n "
185
189
+ " input: any,\n "
186
190
+ " context: $L\n "
187
191
+ "): any => {" , "}" , methodName , getEventStreamSerdeContextType (context , eventsUnion ), () -> {
188
192
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 ) -> {
191
195
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
+ });
199
203
}
200
204
201
205
private String getSerFunctionName (GenerationContext context , Shape shape ) {
202
206
Symbol symbol = getSymbol (context , shape );
203
- String protocolName = context .getProtocolName ();
204
- return ProtocolGenerator .getSerFunctionName (symbol , protocolName );
207
+ return ProtocolGenerator .getSerFunctionShortName (symbol );
205
208
}
206
209
207
210
public String getEventSerFunctionName (GenerationContext context , Shape shape ) {
@@ -226,10 +229,10 @@ private Symbol getSymbol(GenerationContext context, Shape shape) {
226
229
}
227
230
228
231
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 ,
233
236
Set <Shape > documentShapesToSerialize
234
237
) {
235
238
String methodName = getEventSerFunctionName (context , event );
@@ -240,17 +243,17 @@ public void generateEventMarshaller(
240
243
+ " input: $T,\n "
241
244
+ " context: __SerdeContext\n "
242
245
+ "): __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
+ });
254
257
}
255
258
256
259
private void writeEventContentTypeHeader (
@@ -280,8 +283,8 @@ private Optional<MemberShape> getEventPayloadMember(StructureShape event) {
280
283
.filter (member -> member .hasTrait (EventPayloadTrait .class ))
281
284
.collect (Collectors .toList ());
282
285
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 ));
285
288
}
286
289
287
290
private void writeEventHeaders (GenerationContext context , StructureShape event ) {
@@ -324,8 +327,8 @@ private String getEventHeaderType(Shape shape) {
324
327
*/
325
328
private MemberShape getExplicitEventPayloadMember (StructureShape event ) {
326
329
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 );
329
332
}
330
333
331
334
private void writeEventBody (
@@ -347,13 +350,13 @@ private void writeEventBody(
347
350
writer .write ("body = context.utf8Decoder(input.$L);" , payloadMemberName );
348
351
} else if (payloadShape instanceof BlobShape || payloadShape instanceof StringShape ) {
349
352
Symbol symbol = getSymbol (context , payloadShape );
350
- String serFunctionName = ProtocolGenerator .getSerFunctionName (symbol , context . getProtocolName () );
353
+ String serFunctionName = ProtocolGenerator .getSerFunctionShortName (symbol );
351
354
documentShapesToSerialize .add (payloadShape );
352
355
writer .write ("body = $L(input.$L, context);" , payloadMemberName , serFunctionName );
353
356
serializeInputEventDocumentPayload .run ();
354
357
} else {
355
358
throw new CodegenException (String .format ("Unexpected shape type bound to event payload: `%s`" ,
356
- payloadShape .getType ()));
359
+ payloadShape .getType ()));
357
360
}
358
361
});
359
362
} else {
@@ -364,7 +367,7 @@ private void writeEventBody(
364
367
}
365
368
}
366
369
Symbol symbol = getSymbol (context , event );
367
- String serFunctionName = ProtocolGenerator .getSerFunctionName (symbol , context . getProtocolName () );
370
+ String serFunctionName = ProtocolGenerator .getSerFunctionShortName (symbol );
368
371
documentShapesToSerialize .add (event );
369
372
writer .write ("body = $L(input, context);" , serFunctionName );
370
373
serializeInputEventDocumentPayload .run ();
@@ -373,36 +376,39 @@ private void writeEventBody(
373
376
374
377
private void generateEventStreamDeserializer (GenerationContext context , UnionShape eventsUnion ) {
375
378
String methodName = getDeserFunctionName (context , eventsUnion );
379
+ String methodLongName = ProtocolGenerator .getDeserFunctionName (getSymbol (context , eventsUnion ),
380
+ context .getProtocolName ());
376
381
Symbol eventsUnionSymbol = getSymbol (context , eventsUnion );
377
382
TypeScriptWriter writer = context .getWriter ();
378
383
Model model = context .getModel ();
379
384
String contextType = getEventStreamSerdeContextType (context , eventsUnion );
385
+
386
+ writer .writeDocs (methodLongName );
380
387
writer .openBlock ("const $L = (\n "
381
388
+ " output: any,\n "
382
389
+ " context: $L\n "
383
390
+ "): 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
+ });
393
402
});
403
+ writer .write ("return {$$unknown: output};" );
394
404
});
395
405
});
396
- writer .write ("return {$$unknown: output};" );
397
406
});
398
- });
399
- });
400
407
}
401
408
402
409
private String getDeserFunctionName (GenerationContext context , Shape shape ) {
403
410
Symbol symbol = getSymbol (context , shape );
404
- String protocolName = context .getProtocolName ();
405
- return ProtocolGenerator .getDeserFunctionName (symbol , protocolName );
411
+ return ProtocolGenerator .getDeserFunctionShortName (symbol );
406
412
}
407
413
408
414
public String getEventDeserFunctionName (GenerationContext context , Shape shape ) {
@@ -423,15 +429,15 @@ public void generateEventUnmarshaller(
423
429
+ " output: any,\n "
424
430
+ " context: __SerdeContext\n "
425
431
+ "): 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
+ });
435
441
}
436
442
437
443
// Writes function content that unmarshall error event with error deserializer
@@ -444,7 +450,7 @@ private void generateErrorEventUnmarshaller(
444
450
TypeScriptWriter writer = context .getWriter ();
445
451
// If this is an error event, we need to generate the error deserializer.
446
452
errorShapesToDeserialize .add (event );
447
- String errorDeserMethodName = getDeserFunctionName (context , event ) + "Response " ;
453
+ String errorDeserMethodName = getDeserFunctionName (context , event ) + "Res " ;
448
454
if (isErrorCodeInBody ) {
449
455
// If error code is in body, parseBody() won't be called inside error deser. So we parse body here.
450
456
// It's ok to parse body here because body won't be streaming if 'isErrorCodeInBody' is set.
@@ -489,14 +495,14 @@ private void readEventBody(
489
495
} else if (payloadShape instanceof StructureShape || payloadShape instanceof UnionShape ) {
490
496
writer .write ("const data: any = await parseBody(output.body, context);" );
491
497
Symbol symbol = getSymbol (context , payloadShape );
492
- String deserFunctionName = ProtocolGenerator .getDeserFunctionName (symbol , context . getProtocolName () );
498
+ String deserFunctionName = ProtocolGenerator .getDeserFunctionShortName (symbol );
493
499
writer .write ("contents.$L = $L(data, context);" , payloadMemberName , deserFunctionName );
494
500
eventShapesToDeserialize .add (payloadShape );
495
501
}
496
502
} else {
497
503
writer .write ("const data: any = await parseBody(output.body, context);" );
498
504
Symbol symbol = getSymbol (context , event );
499
- String deserFunctionName = ProtocolGenerator .getDeserFunctionName (symbol , context . getProtocolName () );
505
+ String deserFunctionName = ProtocolGenerator .getDeserFunctionShortName (symbol );
500
506
writer .write ("Object.assign(contents, $L(data, context));" , deserFunctionName );
501
507
eventShapesToDeserialize .add (event );
502
508
}
0 commit comments