|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 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 |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 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. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.awssdk.codegen.customization.processors; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.io.StringWriter; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.Map; |
| 22 | +import org.slf4j.Logger; |
| 23 | +import org.slf4j.LoggerFactory; |
| 24 | +import software.amazon.awssdk.codegen.customization.CodegenCustomizationProcessor; |
| 25 | +import software.amazon.awssdk.codegen.internal.Jackson; |
| 26 | +import software.amazon.awssdk.codegen.model.intermediate.IntermediateModel; |
| 27 | +import software.amazon.awssdk.codegen.model.intermediate.ShapeModel; |
| 28 | +import software.amazon.awssdk.codegen.model.service.Member; |
| 29 | +import software.amazon.awssdk.codegen.model.service.ServiceModel; |
| 30 | +import software.amazon.awssdk.codegen.model.service.Shape; |
| 31 | +import software.amazon.awssdk.codegen.validation.ModelInvalidException; |
| 32 | +import software.amazon.awssdk.codegen.validation.ValidationEntry; |
| 33 | +import software.amazon.awssdk.codegen.validation.ValidationErrorId; |
| 34 | +import software.amazon.awssdk.codegen.validation.ValidationErrorSeverity; |
| 35 | + |
| 36 | +/** |
| 37 | + * Processor for eventstreams with shared events. This Processor does two things: 1. Apply the duplicateAndRenameSharedEvents |
| 38 | + * customization 2. Raise helpful error messages on untransfromed shared events. |
| 39 | + */ |
| 40 | +public final class EventStreamSharedEventProcessor implements CodegenCustomizationProcessor { |
| 41 | + private static final Logger log = LoggerFactory.getLogger(EventStreamSharedEventProcessor.class); |
| 42 | + |
| 43 | + private final Map<String, Map<String, String>> duplicateAndRenameSharedEvents; |
| 44 | + |
| 45 | + public EventStreamSharedEventProcessor(Map<String, Map<String, String>> duplicateAndRenameSharedEvents) { |
| 46 | + this.duplicateAndRenameSharedEvents = duplicateAndRenameSharedEvents; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public void preprocess(ServiceModel serviceModel) { |
| 51 | + if (duplicateAndRenameSharedEvents == null || duplicateAndRenameSharedEvents.isEmpty()) { |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + for (Map.Entry<String, Map<String, String>> eventStreamEntry : duplicateAndRenameSharedEvents.entrySet()) { |
| 56 | + |
| 57 | + String eventStreamName = eventStreamEntry.getKey(); |
| 58 | + Shape eventStreamShape = serviceModel.getShapes().get(eventStreamName); |
| 59 | + |
| 60 | + validateIsEventStream(eventStreamShape, eventStreamName); |
| 61 | + |
| 62 | + Map<String, Member> eventStreamMembers = eventStreamShape.getMembers(); |
| 63 | + for (Map.Entry<String, String> eventEntry : eventStreamEntry.getValue().entrySet()) { |
| 64 | + Member eventMemberToModify = eventStreamMembers.get(eventEntry.getKey()); |
| 65 | + |
| 66 | + if (eventMemberToModify == null) { |
| 67 | + throw ModelInvalidException.fromEntry(ValidationEntry.create( |
| 68 | + ValidationErrorId.INVALID_CODEGEN_CUSTOMIZATION, |
| 69 | + ValidationErrorSeverity.DANGER, |
| 70 | + String.format("Cannot find event member [%s] in the eventstream [%s] when processing " |
| 71 | + + "customization config duplicateAndRenameSharedEvents.%s", |
| 72 | + eventEntry.getKey(), eventStreamName, eventStreamName))); |
| 73 | + } |
| 74 | + |
| 75 | + String shapeToDuplicate = eventMemberToModify.getShape(); |
| 76 | + Shape eventMemberShape = serviceModel.getShape(shapeToDuplicate); |
| 77 | + |
| 78 | + if (eventMemberShape == null || !eventMemberShape.isEvent()) { |
| 79 | + throw ModelInvalidException.fromEntry(ValidationEntry.create( |
| 80 | + ValidationErrorId.INVALID_CODEGEN_CUSTOMIZATION, |
| 81 | + ValidationErrorSeverity.DANGER, |
| 82 | + String.format("Error: [%s] must be an Event shape when processing " |
| 83 | + + "customization config duplicateAndRenameSharedEvents.%s", |
| 84 | + eventEntry.getKey(), eventStreamName))); |
| 85 | + } |
| 86 | + |
| 87 | + String newShapeName = eventEntry.getValue(); |
| 88 | + if (serviceModel.getShapes().containsKey(newShapeName)) { |
| 89 | + throw ModelInvalidException.fromEntry(ValidationEntry.create( |
| 90 | + ValidationErrorId.INVALID_CODEGEN_CUSTOMIZATION, |
| 91 | + ValidationErrorSeverity.DANGER, |
| 92 | + String.format("Error: [%s] is already in the model when processing " |
| 93 | + + "customization config duplicateAndRenameSharedEvents.%s", |
| 94 | + newShapeName, eventStreamName))); |
| 95 | + } |
| 96 | + serviceModel.getShapes().put(newShapeName, duplicateShape(eventMemberShape)); |
| 97 | + eventMemberToModify.setShape(newShapeName); |
| 98 | + log.info("Duplicated and renamed event member on {} from {} -> {}", |
| 99 | + eventStreamName, shapeToDuplicate, newShapeName); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + private Shape duplicateShape(Shape shape) { |
| 105 | + StringWriter writer = new StringWriter(); |
| 106 | + try { |
| 107 | + Jackson.writeWithObjectMapper(shape, writer); |
| 108 | + Shape duplicated = Jackson.load(Shape.class, writer.toString()); |
| 109 | + duplicated.setSynthetic(true); |
| 110 | + return duplicated; |
| 111 | + } catch (IOException e) { |
| 112 | + throw new RuntimeException(e); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + private static void validateIsEventStream(Shape shape, String name) { |
| 117 | + if (shape == null) { |
| 118 | + throw ModelInvalidException.fromEntry(ValidationEntry.create( |
| 119 | + ValidationErrorId.INVALID_CODEGEN_CUSTOMIZATION, |
| 120 | + ValidationErrorSeverity.DANGER, |
| 121 | + String.format("Cannot find eventstream shape [%s] in the model when processing " |
| 122 | + + "customization config duplicateAndRenameSharedEvents.%s", name, name))); |
| 123 | + } |
| 124 | + if (!shape.isEventstream()) { |
| 125 | + throw ModelInvalidException.fromEntry(ValidationEntry.create( |
| 126 | + ValidationErrorId.INVALID_CODEGEN_CUSTOMIZATION, |
| 127 | + ValidationErrorSeverity.DANGER, |
| 128 | + String.format("Error: [%s] must be an EventStream when processing " |
| 129 | + + "customization config duplicateAndRenameSharedEvents.%s", name, name))); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public void postprocess(IntermediateModel intermediateModel) { |
| 135 | + // validate that there are no events shared between multiple eventstreams. |
| 136 | + // events may be used multiple times in the same eventstream. |
| 137 | + Map<String, String> seenEvents = new HashMap<>(); |
| 138 | + |
| 139 | + for (ShapeModel shapeModel : intermediateModel.getShapes().values()) { |
| 140 | + if (shapeModel.isEventStream()) { |
| 141 | + shapeModel.getMembers().forEach(m -> { |
| 142 | + ShapeModel memberShape = intermediateModel.getShapes().get(m.getC2jShape()); |
| 143 | + if (memberShape != null && memberShape.isEvent()) { |
| 144 | + if (seenEvents.containsKey(memberShape.getShapeName()) |
| 145 | + && !seenEvents.get(memberShape.getShapeName()).equals(shapeModel.getShapeName())) { |
| 146 | + throw ModelInvalidException.fromEntry(ValidationEntry.create( |
| 147 | + ValidationErrorId.SHARED_EVENTSTREAM_EVENT, |
| 148 | + ValidationErrorSeverity.DANGER, |
| 149 | + String.format("Event shape `%s` is shared between multiple EventStreams. Apply the " |
| 150 | + + "duplicateAndRenameSharedEvents customization to resolve the issue.", |
| 151 | + memberShape.getShapeName()) |
| 152 | + )); |
| 153 | + } |
| 154 | + seenEvents.put(memberShape.getShapeName(), shapeModel.getShapeName()); |
| 155 | + } |
| 156 | + }); |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments