|
| 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.Map; |
| 21 | +import java.util.logging.Logger; |
| 22 | +import software.amazon.smithy.go.codegen.GoSettings; |
| 23 | +import software.amazon.smithy.go.codegen.integration.GoIntegration; |
| 24 | +import software.amazon.smithy.model.Model; |
| 25 | +import software.amazon.smithy.model.node.Node; |
| 26 | +import software.amazon.smithy.model.node.NumberNode; |
| 27 | +import software.amazon.smithy.model.shapes.MemberShape; |
| 28 | +import software.amazon.smithy.model.shapes.Shape; |
| 29 | +import software.amazon.smithy.model.shapes.ShapeId; |
| 30 | +import software.amazon.smithy.model.traits.DefaultTrait; |
| 31 | +import software.amazon.smithy.model.transform.ModelTransformer; |
| 32 | +import software.amazon.smithy.utils.MapUtils; |
| 33 | + |
| 34 | +/** |
| 35 | + * AWS SDK for Go V2 Integrations for SQS services |
| 36 | + */ |
| 37 | +public class SQSCustomizations implements GoIntegration { |
| 38 | + private static final Logger LOGGER = Logger.getLogger(SQSCustomizations.class.getName()); |
| 39 | + private static final ShapeId SQS_SERVICE_ID = ShapeId.from("com.amazonaws.sqs#AmazonSQS"); |
| 40 | + |
| 41 | + /** |
| 42 | + * Default traits that need to be backfilled |
| 43 | + */ |
| 44 | + private static final Map<ShapeId, Node> DEFAULT_TRAIT_BACKFILL = MapUtils.of( |
| 45 | + ShapeId.from("com.amazonaws.sqs#SendMessageRequest$DelaySeconds"), NumberNode.from(0), |
| 46 | + ShapeId.from("com.amazonaws.sqs#ChangeMessageVisibilityBatchRequestEntry$VisibilityTimeout"), NumberNode.from(0), |
| 47 | + ShapeId.from("com.amazonaws.sqs#SendMessageBatchRequestEntry$DelaySeconds"), NumberNode.from(0), |
| 48 | + ShapeId.from("com.amazonaws.sqs#ChangeMessageVisibilityRequest$VisibilityTimeout"), NumberNode.from(0), |
| 49 | + ShapeId.from("com.amazonaws.sqs#ReceiveMessageRequest$WaitTimeSeconds"), NumberNode.from(0), |
| 50 | + ShapeId.from("com.amazonaws.sqs#ReceiveMessageRequest$VisibilityTimeout"), NumberNode.from(0), |
| 51 | + ShapeId.from("com.amazonaws.sqs#ReceiveMessageRequest$MaxNumberOfMessages"), NumberNode.from(0)); |
| 52 | + |
| 53 | + @Override |
| 54 | + public Model preprocessModel(Model model, GoSettings settings) { |
| 55 | + ShapeId serviceId = settings.getService(); |
| 56 | + if (!serviceId.equals(SQS_SERVICE_ID)) { |
| 57 | + return model; |
| 58 | + } |
| 59 | + |
| 60 | + List<Shape> updates = new ArrayList<>(); |
| 61 | + |
| 62 | + // Patch default traits to members to avoid breaking changes |
| 63 | + for (Map.Entry<ShapeId, Node> entry : DEFAULT_TRAIT_BACKFILL.entrySet()) { |
| 64 | + ShapeId memberShapeId = entry.getKey(); |
| 65 | + MemberShape memberShape = model.expectShape(memberShapeId, MemberShape.class); |
| 66 | + if (memberShape.hasTrait(DefaultTrait.class)) { |
| 67 | + DefaultTrait defaultTrait = memberShape.expectTrait(DefaultTrait.class); |
| 68 | + LOGGER.warning("Overwriting default trait for `" + memberShapeId + "` with value: `" |
| 69 | + + Node.prettyPrintJson(defaultTrait.toNode()) + "`"); |
| 70 | + } |
| 71 | + updates.add(memberShape.toBuilder() |
| 72 | + .addTrait(new DefaultTrait(entry.getValue())) |
| 73 | + .build()); |
| 74 | + } |
| 75 | + |
| 76 | + return ModelTransformer.create().replaceShapes(model, updates); |
| 77 | + } |
| 78 | +} |
0 commit comments