|
27 | 27 | import java.util.stream.Collectors; |
28 | 28 |
|
29 | 29 | import org.apache.commons.lang3.StringUtils; |
| 30 | +import org.apache.commons.lang3.tuple.Pair; |
30 | 31 | import software.amazon.smithy.jmespath.JmespathExpression; |
31 | 32 |
|
32 | 33 | public class C2jModelToGeneratorModelTransformer { |
@@ -336,38 +337,46 @@ header insertion there. So strip this out of the model (affects S3's PutObjectR |
336 | 337 | // Header only event |
337 | 338 | shape.setEventPayloadType(null); |
338 | 339 | } else if (shape.hasEventPayloadMembers() || shape.getMembers().size() == 1) { |
339 | | - if (shape.getMembers().size() == 1) { |
340 | | - shape.getMembers().entrySet().stream().forEach(memberEntry -> { |
341 | | - /** |
342 | | - * Note: this is complicated and potentially not completely correct. |
343 | | - * So touch at your own risk until we have protocol tests supported. |
344 | | - * In summary: |
345 | | - * - we need to determine how to serialize events in eventstream |
346 | | - * - to specify payload there is an eventpayload trait |
347 | | - * - but what happens if that trait is not specified |
348 | | - * - if there is one field and its a string or struct then we assume that field is event payload |
349 | | - * - if there is one field and its a blob within structure and not explicitly marked as eventpayload then parent shape is eventpayload |
350 | | - * - if that one field is of any other type then treat parent shape as eventpayload |
351 | | - * - if there is more than one field then parent shape is the payload |
352 | | - */ |
353 | | - Shape memberShape = memberEntry.getValue().getShape(); |
354 | | - if (memberShape.isString() || |
355 | | - memberShape.isBlob() && !shape.isStructure() || |
356 | | - memberShape.isBlob() && memberEntry.getValue().isEventPayload() || |
357 | | - memberShape.isStructure()) { |
358 | | - memberEntry.getValue().setEventPayload(true); |
359 | | - shape.setEventPayloadMemberName(memberEntry.getKey()); |
360 | | - shape.setEventPayloadType(memberShape.getType()); |
361 | | - } else { |
362 | | - if (!shape.getType().equals("structure")) { |
363 | | - throw new RuntimeException("Event shape should always has \"structure\" type if single member cannot be event payload."); |
364 | | - } |
365 | | - shape.setEventPayloadType(shape.getType()); |
| 340 | + if (shape.getMembers().values().stream().filter(member -> !member.isEventHeader()).count() == 1) { |
| 341 | + final List<Map.Entry<String, ShapeMember>> memberEntries = shape.getMembers().entrySet().stream() |
| 342 | + .filter(member -> !member.getValue().isEventHeader()) |
| 343 | + .collect(Collectors.toList()); |
| 344 | + if (memberEntries.size() != 1) { |
| 345 | + throw new RuntimeException("Event shape should have exactly one payload member for event payload."); |
| 346 | + } |
| 347 | + /** |
| 348 | + * Note: this is complicated and potentially not completely correct. |
| 349 | + * So touch at your own risk until we have protocol tests supported. |
| 350 | + * In summary: |
| 351 | + * - we need to determine how to serialize events in eventstream |
| 352 | + * - to specify payload there is an eventpayload trait |
| 353 | + * - but what happens if that trait is not specified |
| 354 | + * - if there is one field and its a string or struct then we assume that field is event payload |
| 355 | + * - if there is one field and its a blob within structure and not explicitly marked as eventpayload then parent shape is eventpayload |
| 356 | + * - if that one field is of any other type then treat parent shape as eventpayload |
| 357 | + * - if there is more than one field then parent shape is the payload |
| 358 | + */ |
| 359 | + final Map.Entry<String, ShapeMember> memberEntry = memberEntries.get(0); |
| 360 | + final Shape memberShape = memberEntry.getValue().getShape(); |
| 361 | + if (memberShape.isString() || |
| 362 | + memberShape.isBlob() && !shape.isStructure() || |
| 363 | + memberShape.isBlob() && memberEntry.getValue().isEventPayload() || |
| 364 | + memberShape.isStructure()) { |
| 365 | + memberEntry.getValue().setEventPayload(true); |
| 366 | + shape.setEventPayloadMemberName(memberEntry.getKey()); |
| 367 | + shape.setEventPayloadType(memberShape.getType()); |
| 368 | + } else { |
| 369 | + if (!shape.getType().equals("structure")) { |
| 370 | + throw new RuntimeException("Event shape should always has \"structure\" type if single member cannot be event payload."); |
366 | 371 | } |
367 | | - |
368 | | - }); |
| 372 | + shape.setEventPayloadType(shape.getType()); |
| 373 | + } |
| 374 | + shape.setEventStreamHeaders(shape.getMembers().entrySet().stream() |
| 375 | + .filter(headerEntry -> headerEntry.getValue().isEventHeader()) |
| 376 | + .map(headerEntry -> Pair.of(headerEntry.getKey(), headerEntry.getValue().getShape())) |
| 377 | + .collect(Collectors.toMap(Pair::getKey, Pair::getValue))); |
369 | 378 | } else { |
370 | | - throw new RuntimeException("Event shape used in Event Stream should only has one member if it has event payload member."); |
| 379 | + throw new RuntimeException("Event shape used in Event Stream should only has one non header member if it has event payload member."); |
371 | 380 | } |
372 | 381 | } else if (shape.getMembers().size() > 1) { |
373 | 382 | if (!shape.getType().equals("structure")) { |
|
0 commit comments