Skip to content

Commit 0711503

Browse files
fix(codegen): strip names from enums (#1837)
* Update checkstyle copyright header to allow 2020/1 * Add integration to strip names from enums This adds an integration to strip names from enums that GA'd without them in order to preserve backwards compatibility.
1 parent f2a47e8 commit 0711503

File tree

4 files changed

+633
-2
lines changed

4 files changed

+633
-2
lines changed

codegen/config/checkstyle/checkstyle.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
</module>
3131

3232
<!-- Files must contain a copyright header. -->
33-
<module name="Header">
33+
<module name="RegexpHeader">
3434
<property name="header"
35-
value="/*\n * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.\n"/>
35+
value="/\*\n \* Copyright 20(19|20|21) Amazon\.com, Inc\. or its affiliates\. All Rights Reserved\.\n"/>
3636
<property name="fileExtensions" value="java"/>
3737
</module>
3838

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
}

codegen/smithy-aws-typescript-codegen/src/main/resources/META-INF/services/software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ software.amazon.smithy.aws.typescript.codegen.AddHttp2Dependency
1414
software.amazon.smithy.aws.typescript.codegen.AddTranscribeStreamingDependency
1515
software.amazon.smithy.aws.typescript.codegen.AddUserAgentDependency
1616
software.amazon.smithy.aws.typescript.codegen.AddOmitRetryHeadersDependency
17+
software.amazon.smithy.aws.typescript.codegen.StripNewEnumNames

0 commit comments

Comments
 (0)