|
| 1 | +/* |
| 2 | + * Copyright 2020 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.Collections; |
| 19 | +import java.util.HashSet; |
| 20 | +import java.util.Set; |
| 21 | +import java.util.stream.Collectors; |
| 22 | +import software.amazon.smithy.build.PluginContext; |
| 23 | +import software.amazon.smithy.model.Model; |
| 24 | +import software.amazon.smithy.model.node.Node; |
| 25 | +import software.amazon.smithy.model.shapes.Shape; |
| 26 | +import software.amazon.smithy.model.shapes.ShapeId; |
| 27 | +import software.amazon.smithy.model.shapes.StringShape; |
| 28 | +import software.amazon.smithy.model.traits.EnumDefinition; |
| 29 | +import software.amazon.smithy.model.traits.EnumTrait; |
| 30 | +import software.amazon.smithy.model.traits.Trait; |
| 31 | +import software.amazon.smithy.model.transform.ModelTransformer; |
| 32 | +import software.amazon.smithy.typescript.codegen.TypeScriptSettings; |
| 33 | +import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration; |
| 34 | +import software.amazon.smithy.utils.IoUtils; |
| 35 | + |
| 36 | +/** |
| 37 | + * Strips enum names from enums that GA'd without them. |
| 38 | + * |
| 39 | + * A number of enums had names back-filled after GA. Since the type generated would change, |
| 40 | + * this is backwards-incompatible. This integration ensures that clients generated |
| 41 | + * preserve backwards-compatibility by stripping names from enums that were known to have |
| 42 | + * launched without them. |
| 43 | + */ |
| 44 | +public final class StripNewEnumNames implements TypeScriptIntegration { |
| 45 | + private final Set<ShapeId> enumsToStrip; |
| 46 | + |
| 47 | + public StripNewEnumNames() { |
| 48 | + // Load the list of enums |
| 49 | + Node json = Node.parse(IoUtils.readUtf8Url(getClass().getResource("enums-to-strip.json"))); |
| 50 | + Set<ShapeId> toStrip = new HashSet<>(); |
| 51 | + json.asArrayNode().ifPresent(array -> array.forEach(node -> { |
| 52 | + node.asStringNode().ifPresent(stringNode -> { |
| 53 | + toStrip.add(ShapeId.from(stringNode.getValue())); |
| 54 | + }); |
| 55 | + })); |
| 56 | + enumsToStrip = Collections.unmodifiableSet(toStrip); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public Model preprocessModel(PluginContext context, TypeScriptSettings settings) { |
| 61 | + Model model = context.getModel(); |
| 62 | + Set<Shape> shapesToUpdate = model.shapes(StringShape.class) |
| 63 | + .filter(shape -> enumsToStrip.contains(shape.getId())) |
| 64 | + .flatMap(shape -> Trait.flatMapStream(shape, EnumTrait.class)) |
| 65 | + // Replace the existing enum trait with an updated version |
| 66 | + .map(pair -> pair.getKey().toBuilder().addTrait(stripNames(pair.getValue())).build()) |
| 67 | + .collect(Collectors.toSet()); |
| 68 | + return ModelTransformer.create().replaceShapes(model, shapesToUpdate); |
| 69 | + } |
| 70 | + |
| 71 | + private EnumTrait stripNames(EnumTrait trait) { |
| 72 | + // Use toBuilder to ensure that any other information (e.g. source location) is preserved. |
| 73 | + EnumTrait.Builder builder = trait.toBuilder().clearEnums(); |
| 74 | + for (EnumDefinition definition : trait.getValues()) { |
| 75 | + // Setting the name to null effectively removes it |
| 76 | + builder.addEnum(definition.toBuilder().name(null).build()); |
| 77 | + } |
| 78 | + return builder.build(); |
| 79 | + } |
| 80 | +} |
0 commit comments