Skip to content

Commit 829131f

Browse files
author
Steven Yuan
authored
Add SQSCustomizations (#2108)
Adds a customization to add default traits to the following members and corresponding top-level input synthetic members, to ensure the default trait is always present: - `com.amazonaws.sqs#SendMessageRequest$DelaySeconds` - `com.amazonaws.sqs#ChangeMessageVisibilityBatchRequestEntry$VisibilityTimeout` - `com.amazonaws.sqs#SendMessageBatchRequestEntry$DelaySeconds` - `com.amazonaws.sqs#ChangeMessageVisibilityRequest$VisibilityTimeout` - `com.amazonaws.sqs#ReceiveMessageRequest$WaitTimeSeconds` - `com.amazonaws.sqs#ReceiveMessageRequest$VisibilityTimeout` - `com.amazonaws.sqs#ReceiveMessageRequest$MaxNumberOfMessages`
1 parent 9d08e7d commit 829131f

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* Copyright 2023 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.smithy.aws.go.codegen.customization;
17+
18+
import java.util.ArrayList;
19+
import java.util.List;
20+
import java.util.Optional;
21+
import java.util.logging.Logger;
22+
import software.amazon.smithy.go.codegen.CodegenUtils;
23+
import software.amazon.smithy.go.codegen.GoSettings;
24+
import software.amazon.smithy.go.codegen.integration.GoIntegration;
25+
import software.amazon.smithy.model.Model;
26+
import software.amazon.smithy.model.node.Node;
27+
import software.amazon.smithy.model.node.NumberNode;
28+
import software.amazon.smithy.model.shapes.IntegerShape;
29+
import software.amazon.smithy.model.shapes.MemberShape;
30+
import software.amazon.smithy.model.shapes.Shape;
31+
import software.amazon.smithy.model.shapes.ShapeId;
32+
import software.amazon.smithy.model.traits.DefaultTrait;
33+
import software.amazon.smithy.model.transform.ModelTransformer;
34+
import software.amazon.smithy.utils.ListUtils;
35+
import software.amazon.smithy.utils.SmithyUnstableApi;
36+
37+
/**
38+
* AWS SDK for Go V2 Integrations for the Amazon SQS service
39+
*/
40+
@SmithyUnstableApi
41+
public class SQSCustomizations implements GoIntegration {
42+
private static final Logger LOGGER = Logger.getLogger(SQSCustomizations.class.getName());
43+
44+
private static final ShapeId SQS_SERVICE_ID = ShapeId.from("com.amazonaws.sqs#AmazonSQS");
45+
private static final ShapeId NON_NULLABLE_INTEGER_ID = ShapeId.from("com.amazonaws.sqs#Integer");
46+
47+
private static final DefaultTrait DEFAULT_ZERO_TRAIT = new DefaultTrait(NumberNode.from(0L));
48+
49+
/**
50+
* Default traits that need to be backfilled
51+
*/
52+
private static final List<ShapeId> DEFAULT_TRAIT_BACKFILL = ListUtils.of(
53+
// Structure Shapes
54+
ShapeId.from("com.amazonaws.sqs#ChangeMessageVisibilityBatchRequestEntry$VisibilityTimeout"),
55+
ShapeId.from("com.amazonaws.sqs#SendMessageBatchRequestEntry$DelaySeconds"),
56+
// Top-level Input Shape Members
57+
ShapeId.from("com.amazonaws.sqs#SendMessageRequest$DelaySeconds"),
58+
ShapeId.from("com.amazonaws.sqs#ChangeMessageVisibilityRequest$VisibilityTimeout"),
59+
ShapeId.from("com.amazonaws.sqs#ReceiveMessageRequest$WaitTimeSeconds"),
60+
ShapeId.from("com.amazonaws.sqs#ReceiveMessageRequest$VisibilityTimeout"),
61+
ShapeId.from("com.amazonaws.sqs#ReceiveMessageRequest$MaxNumberOfMessages"),
62+
// Synthetic-equivalent Top-level Input Shape Members
63+
// Note that "Request" is translated to "Input" in synthetic members
64+
ShapeId.from("smithy.go.synthetic#SendMessageInput$DelaySeconds"),
65+
ShapeId.from("smithy.go.synthetic#ChangeMessageVisibilityInput$VisibilityTimeout"),
66+
ShapeId.from("smithy.go.synthetic#ReceiveMessageInput$WaitTimeSeconds"),
67+
ShapeId.from("smithy.go.synthetic#ReceiveMessageInput$VisibilityTimeout"),
68+
ShapeId.from("smithy.go.synthetic#ReceiveMessageInput$MaxNumberOfMessages"));
69+
70+
@Override
71+
public Model preprocessModel(Model model, GoSettings settings) {
72+
ShapeId serviceId = settings.getService();
73+
if (!serviceId.equals(SQS_SERVICE_ID)) {
74+
return model;
75+
}
76+
77+
// Add non-nullable integer shape
78+
model = model.toBuilder()
79+
.addShape(IntegerShape.builder()
80+
.id(NON_NULLABLE_INTEGER_ID)
81+
.addTrait(DEFAULT_ZERO_TRAIT)
82+
.build())
83+
.build();
84+
85+
// Process Default traits that need to be backfilled
86+
List<Shape> updates = new ArrayList<>();
87+
for (ShapeId memberShapeId : DEFAULT_TRAIT_BACKFILL) {
88+
Optional<MemberShape> memberShapeOptional = model
89+
.getShape(memberShapeId)
90+
.flatMap(s -> s.asMemberShape());
91+
String memberShapeNamespace = memberShapeId.getNamespace();
92+
// Synthetic shapes could be missing if the upstream model changes are deduped
93+
// and the synthetic shapes are no longer needed.
94+
if (!memberShapeOptional.isPresent() && memberShapeNamespace.equals(CodegenUtils.getSyntheticTypeNamespace())) {
95+
LOGGER.warning(String.format("SQS service synthetic member shape `" + memberShapeId
96+
+ "` is not present in the model, so could not be backfilled with a default trait."));
97+
continue;
98+
}
99+
MemberShape memberShape = memberShapeOptional.get();
100+
// Overwrite default trait to maintain backward compatibility
101+
if (memberShape.hasTrait(DefaultTrait.class)) {
102+
DefaultTrait defaultTrait = memberShape.expectTrait(DefaultTrait.class);
103+
LOGGER.warning("Overwriting default trait for SQS service member shape `" + memberShapeId
104+
+ "` with value: `" + Node.prettyPrintJson(defaultTrait.toNode()) + "`");
105+
}
106+
// Patch member with default trait and change target to non-nullable integer shape
107+
updates.add(memberShape.toBuilder()
108+
.addTrait(DEFAULT_ZERO_TRAIT)
109+
.target(NON_NULLABLE_INTEGER_ID)
110+
.build());
111+
}
112+
return ModelTransformer.create().replaceShapes(model, updates);
113+
}
114+
}

codegen/smithy-aws-go-codegen/src/main/resources/META-INF/services/software.amazon.smithy.go.codegen.integration.GoIntegration

+1
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,4 @@ software.amazon.smithy.aws.go.codegen.EndpointDiscoveryGenerator
5050
software.amazon.smithy.aws.go.codegen.customization.S3100Continue
5151
software.amazon.smithy.aws.go.codegen.customization.ApiGatewayExportsNullabilityExceptionIntegration
5252
software.amazon.smithy.aws.go.codegen.customization.LambdaRecursionDetection
53+
software.amazon.smithy.aws.go.codegen.customization.SQSCustomizations

0 commit comments

Comments
 (0)