|
| 1 | +/* |
| 2 | + * Copyright 2019 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.typescript.codegen; |
| 17 | + |
| 18 | +import java.util.Map; |
| 19 | +import java.util.Set; |
| 20 | +import java.util.logging.Logger; |
| 21 | +import software.amazon.smithy.build.PluginContext; |
| 22 | +import software.amazon.smithy.model.Model; |
| 23 | +import software.amazon.smithy.model.shapes.LongShape; |
| 24 | +import software.amazon.smithy.model.shapes.ShapeId; |
| 25 | +import software.amazon.smithy.typescript.codegen.TypeScriptSettings; |
| 26 | +import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration; |
| 27 | +import software.amazon.smithy.utils.MapUtils; |
| 28 | +import software.amazon.smithy.utils.OptionalUtils; |
| 29 | +import software.amazon.smithy.utils.SetUtils; |
| 30 | +import software.amazon.smithy.utils.SmithyInternalApi; |
| 31 | + |
| 32 | +/** |
| 33 | + * Transform the S3 object size member from an integer to a long. |
| 34 | + */ |
| 35 | +@SmithyInternalApi |
| 36 | +public final class AddS3ObjectSizeMemberShapeType implements TypeScriptIntegration { |
| 37 | + private static final Logger LOGGER = Logger.getLogger(AddS3ObjectSizeMemberShapeType.class.getName()); |
| 38 | + |
| 39 | + private static final Map<ShapeId, Set<ShapeId>> SERVICE_TO_SHAPE_MAP = MapUtils.of( |
| 40 | + ShapeId.from("com.amazonaws.s3#AmazonS3"), SetUtils.of( |
| 41 | + ShapeId.from("com.amazonaws.s3#Size") |
| 42 | + ) |
| 43 | + ); |
| 44 | + |
| 45 | + @Override |
| 46 | + public byte getOrder() { |
| 47 | + // This integration should happen before other integrations that rely on the presence of this trait |
| 48 | + return -60; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public Model preprocessModel(PluginContext context, TypeScriptSettings settings) { |
| 53 | + Model model = context.getModel(); |
| 54 | + ShapeId serviceId = settings.getService(); |
| 55 | + if (!SERVICE_TO_SHAPE_MAP.containsKey(serviceId)) { |
| 56 | + return model; |
| 57 | + } |
| 58 | + |
| 59 | + Set<ShapeId> shapeIds = SERVICE_TO_SHAPE_MAP.get(serviceId); |
| 60 | + |
| 61 | + Model.Builder builder = model.toBuilder(); |
| 62 | + for (ShapeId shapeId : shapeIds) { |
| 63 | + OptionalUtils.ifPresentOrElse( |
| 64 | + model.getShape(shapeId), |
| 65 | + (shape) -> { |
| 66 | + if (shape.isLongShape()) { |
| 67 | + LOGGER.warning("shape is already modeled as an LONG does not require backfill"); |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + builder.addShape(LongShape.builder() |
| 72 | + .id(shape.getId()) |
| 73 | + .addTraits(shape.getAllTraits().values()) |
| 74 | + .build()); |
| 75 | + }, |
| 76 | + () -> LOGGER.warning("shape " + shapeId + " not found in " + serviceId + " model") |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + return builder.build(); |
| 81 | + } |
| 82 | +} |
0 commit comments