Skip to content

Commit

Permalink
Handle Operation<Request>/<Result> possible type name conflict at a h…
Browse files Browse the repository at this point in the history
…igher abstraction level
  • Loading branch information
SergeyRyabinin committed Nov 16, 2023
1 parent eac247e commit 66aa643
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ public class C2jModelToGeneratorModelTransformer {
"GeneratedPolicyResult"
);

private static List<String> SHAPE_SDK_RESULT_SUFFIX = ImmutableList.of(
"Result", "SdkResult", "CppSdkResult"
);
private static List<String> SHAPE_SDK_REQUEST_SUFFIX = ImmutableList.of(
"Request", "SdkRequest", "CppSdkRequest"
);

public C2jModelToGeneratorModelTransformer(C2jServiceModel c2jServiceModel, boolean standalone) {
this.c2jServiceModel = c2jServiceModel;
this.standalone = standalone;
Expand Down Expand Up @@ -548,8 +555,8 @@ Operation convertOperation(C2jOperation c2jOperation) {

// input
if (c2jOperation.getInput() != null) {
String requestName = c2jOperation.getName() + "Request";
Shape requestShape = renameShape(shapes.get(c2jOperation.getInput().getShape()), requestName);
Shape requestShape = renameShape(shapes.get(c2jOperation.getInput().getShape()), c2jOperation.getName(), SHAPE_SDK_REQUEST_SUFFIX);

requestShape.setRequest(true);
requestShape.setReferenced(true);
requestShape.getReferencedBy().add(c2jOperation.getName());
Expand All @@ -562,7 +569,7 @@ Operation convertOperation(C2jOperation c2jOperation) {
}
if(requestShape.getLocationName() != null && requestShape.getLocationName().length() > 0 &&
(requestShape.getPayload() == null || requestShape.getPayload().length() == 0) ) {
requestShape.setPayload(requestName);
requestShape.setPayload(requestShape.getName());
}

requestShape.setSignBody(true);
Expand Down Expand Up @@ -597,8 +604,8 @@ Operation convertOperation(C2jOperation c2jOperation) {

// output
if (c2jOperation.getOutput() != null) {
String resultName = c2jOperation.getName() + "Result";
Shape resultShape = renameShape(shapes.get(c2jOperation.getOutput().getShape()), resultName);
Shape resultShape = renameShape(shapes.get(c2jOperation.getOutput().getShape()), c2jOperation.getName(), SHAPE_SDK_RESULT_SUFFIX);

resultShape.setResult(true);
resultShape.setReferenced(true);
resultShape.getReferencedBy().add(c2jOperation.getName());
Expand Down Expand Up @@ -651,44 +658,54 @@ Operation convertOperation(C2jOperation c2jOperation) {
return operation;
}

Shape renameShape(Shape shape, String name) {
if (shape.getName().equals(name)) {
return shape;
}

// Detect any conflicts with shape name defined by service team, need to rename it if so.
Optional<String> conflicted = shapes.keySet().stream()
.filter(shapeName -> name.equals(shapeName) ||
(shape.getMembers().keySet().stream().anyMatch(memberName -> memberName.equals(shapeName) ||
shape.getMembers().values().stream().anyMatch(shapeMember -> LEGACY_RENAMED_APIS.contains(shapeMember.getShape().getName()))) &&
(name.equals("Get" + shapeName) || name.equals("Set" + shapeName)))).findFirst();
if (conflicted.isPresent()) {
String originalShapeName = conflicted.get();
String newShapeName = "";
switch(originalShapeName) {
case "CopyObjectResult":
newShapeName = "CopyObjectResultDetails";
renameShapeMember(shape, "CopyObjectResult", originalShapeName, newShapeName, newShapeName, true);
break;
case "BatchUpdateScheduleResult":
shapes.remove(originalShapeName);
break;
case "GeneratedPolicyResult":
newShapeName = "GeneratedPolicyResults";
renameShapeMember(shape, "generatedPolicyResult", originalShapeName, newShapeName, newShapeName, false);
break;
case "SearchResult":
newShapeName = "SearchResultDetails";
renameShapeMember(shape, "results", originalShapeName, "results", newShapeName, true);
break;
default:
throw new RuntimeException("Unhandled shape name conflict: " + name);
Shape renameShape(Shape shape, String baseName, List<String> suffixOptions) {
String newName = null;
Iterator<String> suffixIt = suffixOptions.iterator();
while (suffixIt.hasNext()) {
newName = baseName + suffixIt.next();

if (shape.getName().equals(newName)) {
return shape;
}

// Detect any conflicts with shape name defined by service team, need to rename it if so.
String finalNewName = newName;
Optional<String> conflicted = shapes.keySet().stream()
.filter(shapeName -> finalNewName.equals(shapeName) ||
(shape.getMembers().keySet().stream().anyMatch(memberName -> memberName.equals(shapeName) ||
shape.getMembers().values().stream().anyMatch(shapeMember -> LEGACY_RENAMED_APIS.contains(shapeMember.getShape().getName()))) &&
(finalNewName.equals("Get" + shapeName) || finalNewName.equals("Set" + shapeName)))).findFirst();
if (conflicted.isPresent()) {
String originalShapeName = conflicted.get();
String newShapeName = "";
switch (originalShapeName) {
case "CopyObjectResult":
newShapeName = "CopyObjectResultDetails";
renameShapeMember(shape, "CopyObjectResult", originalShapeName, newShapeName, newShapeName, true);
break;
case "BatchUpdateScheduleResult":
shapes.remove(originalShapeName);
break;
case "GeneratedPolicyResult":
newShapeName = "GeneratedPolicyResults";
renameShapeMember(shape, "generatedPolicyResult", originalShapeName, newShapeName, newShapeName, false);
break;
case "SearchResult":
newShapeName = "SearchResultDetails";
renameShapeMember(shape, "results", originalShapeName, "results", newShapeName, true);
break;
default:
if (!suffixIt.hasNext())
throw new RuntimeException("Unhandled shape name conflict: " + newName);
}
} else {
break;
}
}

Shape cloned = cloneShape(shape);
cloned.setName(name);
shapes.put(name, cloned);
cloned.setName(newName);
shapes.put(newName, cloned);
return cloned;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ namespace Model
/**
* Underlying Event Stream Handler which is used to define callback functions.
*/
inline ${operation.name}Request& WithEventStreamHandler(const ${operation.name}Handler& value) { SetEventStreamHandler(value); return *this; }
inline ${operation.request.shape.name}& WithEventStreamHandler(const ${operation.name}Handler& value) { SetEventStreamHandler(value); return *this; }

#end
#if($operation.requestCompressionRequired)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,11 @@ static void ${operation.name}RequestShutdownCallback(void *user_data)
#end
#if($operation.request)
#if($operation.name == "PutObject")
userData->putResponseHandler(userData->s3CrtClient, *(reinterpret_cast<const ${operation.name}Request*>(userData->originalRequest)), std::move(outcome), userData->asyncCallerContext);
userData->putResponseHandler(userData->s3CrtClient, *(reinterpret_cast<const ${operation.request.shape.name}*>(userData->originalRequest)), std::move(outcome), userData->asyncCallerContext);
#elseif($operation.name == "GetObject")
userData->getResponseHandler(userData->s3CrtClient, *(reinterpret_cast<const ${operation.name}Request*>(userData->originalRequest)), std::move(outcome), userData->asyncCallerContext);
userData->getResponseHandler(userData->s3CrtClient, *(reinterpret_cast<const ${operation.request.shape.name}*>(userData->originalRequest)), std::move(outcome), userData->asyncCallerContext);
#elseif($operation.name == "CopyObject")
userData->copyResponseHandler(userData->s3CrtClient, *(reinterpret_cast<const ${operation.name}Request*>(userData->originalRequest)), std::move(outcome), userData->asyncCallerContext);
userData->copyResponseHandler(userData->s3CrtClient, *(reinterpret_cast<const ${operation.request.shape.name}*>(userData->originalRequest)), std::move(outcome), userData->asyncCallerContext);
#end
#else
(*handler)(userData->s3CrtClient, outcome, userData->userCallbackContext);
Expand Down Expand Up @@ -290,7 +290,7 @@ void ${className}::${operation.name}Async(${constText}${operation.request.shape.
#else
InitCommonCrtRequestOption(userData, &options, &request, uri, Aws::Http::HttpMethod::HTTP_${operation.http.method});
#end
options.shutdown_callback = ${operation.name}RequestShutdownCallback;
options.shutdown_callback = ${operation.request.shape.name}RequestShutdownCallback;
#if($operation.name == "PutObject")
options.type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT;
#elseif($operation.name == "GetObject")
Expand Down

0 comments on commit 66aa643

Please sign in to comment.