-
Notifications
You must be signed in to change notification settings - Fork 932
Add duplicateAndRenameSharedEvents customization to support event shapes shared with multiple evenstreams #6031
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
alextwoods
wants to merge
8
commits into
master
Choose a base branch
from
alexwoo/eventstream-shared-customization
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+469
−17
Open
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
22d7938
Add processor to detect shared events + customization to duplicate.
alextwoods a045a43
Fix null memberShape
alextwoods 9056c2b
Improve test coverage
alextwoods 0603ba2
Merge branch 'master' into alexwoo/eventstream-shared-customization
alextwoods d4b1646
Add changelog
alextwoods 17a0463
Merge branch 'master' into alexwoo/eventstream-shared-customization
alextwoods d9c547c
Deep copy the new event shape
alextwoods c413f7d
Convert exceptions to ModelValidations
alextwoods File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"type": "bugfix", | ||
"category": "AWS SDK for Java v2", | ||
"contributor": "", | ||
"description": "Add duplicateAndRenameSharedEvents customization to support event shapes shared with multiple evenstreams." | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
...tware/amazon/awssdk/codegen/customization/processors/EventStreamSharedEventProcessor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.awssdk.codegen.customization.processors; | ||
|
||
import java.io.IOException; | ||
import java.io.StringWriter; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import software.amazon.awssdk.codegen.customization.CodegenCustomizationProcessor; | ||
import software.amazon.awssdk.codegen.internal.Jackson; | ||
import software.amazon.awssdk.codegen.model.intermediate.IntermediateModel; | ||
import software.amazon.awssdk.codegen.model.intermediate.ShapeModel; | ||
import software.amazon.awssdk.codegen.model.service.Member; | ||
import software.amazon.awssdk.codegen.model.service.ServiceModel; | ||
import software.amazon.awssdk.codegen.model.service.Shape; | ||
|
||
/** | ||
* Processor for eventstreams with shared events. This Processor does two things: 1. Apply the duplicateAndRenameSharedEvents | ||
* customization 2. Raise helpful error messages on untransfromed shared events. | ||
*/ | ||
public final class EventStreamSharedEventProcessor implements CodegenCustomizationProcessor { | ||
private static final Logger log = LoggerFactory.getLogger(EventStreamSharedEventProcessor.class); | ||
|
||
private final Map<String, Map<String, String>> duplicateAndRenameSharedEvents; | ||
|
||
public EventStreamSharedEventProcessor(Map<String, Map<String, String>> duplicateAndRenameSharedEvents) { | ||
this.duplicateAndRenameSharedEvents = duplicateAndRenameSharedEvents; | ||
} | ||
|
||
@Override | ||
public void preprocess(ServiceModel serviceModel) { | ||
if (duplicateAndRenameSharedEvents == null || duplicateAndRenameSharedEvents.isEmpty()) { | ||
return; | ||
} | ||
|
||
for (Map.Entry<String, Map<String, String>> eventStreamEntry : duplicateAndRenameSharedEvents.entrySet()) { | ||
|
||
String eventStreamName = eventStreamEntry.getKey(); | ||
Shape eventStreamShape = serviceModel.getShapes().get(eventStreamName); | ||
|
||
validateIsEventStream(eventStreamShape, eventStreamName); | ||
|
||
Map<String, Member> eventStreamMembers = eventStreamShape.getMembers(); | ||
for (Map.Entry<String, String> eventEntry : eventStreamEntry.getValue().entrySet()) { | ||
Member eventMemberToModify = eventStreamMembers.get(eventEntry.getKey()); | ||
|
||
if (eventMemberToModify == null) { | ||
throw new IllegalStateException( | ||
String.format("Cannot find event member [%s] in the eventstream [%s] when processing " | ||
+ "customization config duplicateAndRenameSharedEvents.%s", | ||
eventEntry.getKey(), eventStreamName, eventStreamName)); | ||
} | ||
|
||
String shapeToDuplicate = eventMemberToModify.getShape(); | ||
Shape eventMemberShape = serviceModel.getShape(shapeToDuplicate); | ||
|
||
if (eventMemberShape == null || !eventMemberShape.isEvent()) { | ||
throw new IllegalStateException( | ||
String.format("Error: [%s] must be an Event shape when processing " | ||
+ "customization config duplicateAndRenameSharedEvents.%s", | ||
eventEntry.getKey(), eventStreamName)); | ||
} | ||
|
||
String newShapeName = eventEntry.getValue(); | ||
if (serviceModel.getShapes().containsKey(newShapeName)) { | ||
throw new IllegalStateException( | ||
String.format("Error: [%s] is already in the model when processing " | ||
+ "customization config duplicateAndRenameSharedEvents.%s", | ||
newShapeName, eventStreamName)); | ||
} | ||
serviceModel.getShapes().put(newShapeName, duplicateShape(eventMemberShape)); | ||
eventMemberToModify.setShape(newShapeName); | ||
log.info("Duplicated and renamed event member on {} from {} -> {}", | ||
eventStreamName, shapeToDuplicate, newShapeName); | ||
} | ||
} | ||
} | ||
|
||
private Shape duplicateShape(Shape shape) { | ||
StringWriter writer = new StringWriter(); | ||
try { | ||
Jackson.writeWithObjectMapper(shape, writer); | ||
Shape duplicated = Jackson.load(Shape.class, writer.toString()); | ||
duplicated.setSynthetic(true); | ||
return duplicated; | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private static void validateIsEventStream(Shape shape, String name) { | ||
if (shape == null) { | ||
throw new IllegalStateException( | ||
String.format("Cannot find eventstream shape [%s] in the model when processing " | ||
+ "customization config duplicateAndRenameSharedEvents.%s", name, name)); | ||
} | ||
if (!shape.isEventstream()) { | ||
throw new IllegalStateException( | ||
String.format("Error: [%s] must be an EventStream when processing " | ||
+ "customization config duplicateAndRenameSharedEvents.%s", name, name)); | ||
} | ||
} | ||
|
||
@Override | ||
public void postprocess(IntermediateModel intermediateModel) { | ||
// validate that there are no events shared between multiple eventstreams. | ||
// events may be used multiple times in the same eventstream. | ||
Map<String, String> seenEvents = new HashMap<>(); | ||
|
||
for (ShapeModel shapeModel : intermediateModel.getShapes().values()) { | ||
if (shapeModel.isEventStream()) { | ||
shapeModel.getMembers().forEach(m -> { | ||
ShapeModel memberShape = intermediateModel.getShapes().get(m.getC2jShape()); | ||
if (memberShape != null && memberShape.isEvent()) { | ||
if (seenEvents.containsKey(memberShape.getShapeName()) | ||
&& !seenEvents.get(memberShape.getShapeName()).equals(shapeModel.getShapeName())) { | ||
throw new IllegalStateException( | ||
String.format("Event shape `%s` is shared between multiple EventStreams. Apply the " | ||
+ "duplicateAndRenameSharedEvents customization to resolve the issue.", | ||
memberShape.getShapeName())); | ||
} | ||
seenEvents.put(memberShape.getShapeName(), shapeModel.getShapeName()); | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
...e/amazon/awssdk/codegen/customization/processors/EventStreamSharedEventProcessorTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.awssdk.codegen.customization.processors; | ||
|
||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.junit.Assert.assertNotEquals; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
||
import java.io.File; | ||
import java.util.Map; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import software.amazon.awssdk.codegen.C2jModels; | ||
import software.amazon.awssdk.codegen.IntermediateModelBuilder; | ||
import software.amazon.awssdk.codegen.model.config.customization.CustomizationConfig; | ||
import software.amazon.awssdk.codegen.model.service.ServiceModel; | ||
import software.amazon.awssdk.codegen.model.service.Shape; | ||
import software.amazon.awssdk.codegen.utils.ModelLoaderUtils; | ||
import software.amazon.awssdk.utils.ImmutableMap; | ||
|
||
public class EventStreamSharedEventProcessorTest { | ||
private static final String RESOURCE_ROOT = "/software/amazon/awssdk/codegen/customization/processors" | ||
+ "/eventstreamsharedeventprocessor/"; | ||
|
||
private ServiceModel serviceModel; | ||
|
||
@Before | ||
public void setUp() { | ||
File serviceModelFile = | ||
new File(EventStreamSharedEventProcessorTest.class.getResource(RESOURCE_ROOT + "service-2.json").getFile()); | ||
serviceModel = ModelLoaderUtils.loadModel(ServiceModel.class, serviceModelFile); | ||
} | ||
|
||
@Test | ||
public void duplicatesAndRenamesSharedEvent() { | ||
File customizationConfigFile = | ||
new File(EventStreamSharedEventProcessorTest.class.getResource(RESOURCE_ROOT + "customization.config").getFile()); | ||
CustomizationConfig config = ModelLoaderUtils.loadModel(CustomizationConfig.class, customizationConfigFile); | ||
|
||
EventStreamSharedEventProcessor processor = | ||
new EventStreamSharedEventProcessor(config.getDuplicateAndRenameSharedEvents()); | ||
processor.preprocess(serviceModel); | ||
|
||
Shape newEventShape = serviceModel.getShape("PayloadB"); | ||
assertNotNull(newEventShape); | ||
assertNotEquals(serviceModel.getShape("Payload"), newEventShape); // shape is duplicated/deep copied | ||
assertTrue(newEventShape.isSynthetic()); | ||
assertEquals(serviceModel.getShape("Payload").getType(), newEventShape.getType()); | ||
assertEquals(serviceModel.getShape("Payload").getMembers().keySet(), newEventShape.getMembers().keySet()); | ||
|
||
Shape streamB = serviceModel.getShape("StreamB"); | ||
assertEquals("PayloadB", streamB.getMembers().get("Payload").getShape()); | ||
} | ||
|
||
@Test | ||
public void modelWithSharedEvents_raises() { | ||
CustomizationConfig emptyConfig = CustomizationConfig.create(); | ||
|
||
assertThatThrownBy(() -> new IntermediateModelBuilder( | ||
C2jModels.builder() | ||
.serviceModel(serviceModel) | ||
.customizationConfig(emptyConfig) | ||
.build()).build()) | ||
.isInstanceOf(IllegalStateException.class) | ||
.hasMessageContaining("Event shape `Payload` is shared between multiple EventStreams"); | ||
} | ||
|
||
@Test | ||
public void invalidCustomization_missingShape() { | ||
Map<String, Map<String, String>> duplicateAndRenameSharedEvents = ImmutableMap.of("MissingShape", null); | ||
|
||
EventStreamSharedEventProcessor processor = | ||
new EventStreamSharedEventProcessor(duplicateAndRenameSharedEvents); | ||
assertThatThrownBy(() -> processor.preprocess(serviceModel)) | ||
.isInstanceOf(IllegalStateException.class) | ||
.hasMessageContaining("Cannot find eventstream shape [MissingShape]"); | ||
} | ||
|
||
@Test | ||
public void invalidCustomization_notEventStream() { | ||
Map<String, Map<String, String>> duplicateAndRenameSharedEvents = ImmutableMap.of("Payload", null); | ||
|
||
EventStreamSharedEventProcessor processor = | ||
new EventStreamSharedEventProcessor(duplicateAndRenameSharedEvents); | ||
assertThatThrownBy(() -> processor.preprocess(serviceModel)) | ||
.isInstanceOf(IllegalStateException.class) | ||
.hasMessageContaining("Error: [Payload] must be an EventStream"); | ||
} | ||
|
||
@Test | ||
public void invalidCustomization_invalidMember() { | ||
Map<String, Map<String, String>> duplicateAndRenameSharedEvents = ImmutableMap.of( | ||
"StreamB", ImmutableMap.of("InvalidMember", "Payload")); | ||
|
||
EventStreamSharedEventProcessor processor = | ||
new EventStreamSharedEventProcessor(duplicateAndRenameSharedEvents); | ||
assertThatThrownBy(() -> processor.preprocess(serviceModel)) | ||
.isInstanceOf(IllegalStateException.class) | ||
.hasMessageContaining("Cannot find event member [InvalidMember] in the eventstream [StreamB]"); | ||
} | ||
|
||
@Test | ||
public void invalidCustomization_shapeAlreadyExists() { | ||
Map<String, Map<String, String>> duplicateAndRenameSharedEvents = ImmutableMap.of( | ||
"StreamB", ImmutableMap.of("Payload", "Payload")); | ||
|
||
EventStreamSharedEventProcessor processor = | ||
new EventStreamSharedEventProcessor(duplicateAndRenameSharedEvents); | ||
assertThatThrownBy(() -> processor.preprocess(serviceModel)) | ||
.isInstanceOf(IllegalStateException.class) | ||
.hasMessageContaining("Error: [Payload] is already in the model"); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...sdk/codegen/customization/processors/eventstreamsharedeventprocessor/customization.config
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"duplicateAndRenameSharedEvents": { | ||
"StreamB": { | ||
"Payload": "PayloadB" | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.