-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for 1.0 downgrades and serialization
This commit adds the ability to downgrade the in-memory semantic model to 1.0 models, and adds support for serializing 1.0 JSON AST models to ModelSerializer.
- Loading branch information
Showing
14 changed files
with
788 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
smithy-model/src/main/java/software/amazon/smithy/model/transform/DowngradeToV1.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/* | ||
* Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.smithy.model.transform; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import software.amazon.smithy.model.Model; | ||
import software.amazon.smithy.model.knowledge.NullableIndex; | ||
import software.amazon.smithy.model.shapes.MemberShape; | ||
import software.amazon.smithy.model.shapes.ResourceShape; | ||
import software.amazon.smithy.model.shapes.Shape; | ||
import software.amazon.smithy.model.shapes.ShapeId; | ||
import software.amazon.smithy.model.shapes.ShapeType; | ||
import software.amazon.smithy.model.shapes.StructureShape; | ||
import software.amazon.smithy.model.traits.AddedDefaultTrait; | ||
import software.amazon.smithy.model.traits.ClientOptionalTrait; | ||
import software.amazon.smithy.model.traits.DefaultTrait; | ||
import software.amazon.smithy.model.traits.NotPropertyTrait; | ||
import software.amazon.smithy.model.traits.PropertyTrait; | ||
|
||
final class DowngradeToV1 { | ||
|
||
Model transform(ModelTransformer transformer, Model model) { | ||
// Flatten and remove mixins since they aren't part of IDL 1.0 | ||
model = transformer.flattenAndRemoveMixins(model); | ||
|
||
// Change enums to string shapes and intEnums to integers. | ||
model = downgradeEnums(transformer, model); | ||
|
||
// Remove resource properties | ||
model = removeResourceProperties(transformer, model); | ||
|
||
// Remove default traits that do not correlate to box traits from v1. | ||
model = removeUnnecessaryDefaults(transformer, model); | ||
|
||
return removeOtherV2Traits(transformer, model); | ||
} | ||
|
||
private Model downgradeEnums(ModelTransformer transformer, Model model) { | ||
Map<ShapeId, ShapeType> typeChanges = new HashMap<>(); | ||
|
||
for (Shape shape : model.getEnumShapes()) { | ||
typeChanges.put(shape.getId(), ShapeType.STRING); | ||
} | ||
|
||
for (Shape shape : model.getIntEnumShapes()) { | ||
typeChanges.put(shape.getId(), ShapeType.INTEGER); | ||
} | ||
|
||
return transformer.changeShapeType(model, typeChanges); | ||
} | ||
|
||
private Model removeResourceProperties(ModelTransformer transformer, Model model) { | ||
List<Shape> updates = new ArrayList<>(); | ||
|
||
// Remove the "properties" key from resources. | ||
for (ResourceShape shape : model.getResourceShapes()) { | ||
if (shape.hasProperties()) { | ||
updates.add(shape.toBuilder().properties(Collections.emptyMap()).build()); | ||
} | ||
} | ||
|
||
// Remove @notProperty. | ||
for (Shape shape : model.getShapesWithTrait(NotPropertyTrait.class)) { | ||
updates.add(Shape.shapeToBuilder(shape).removeTrait(NotPropertyTrait.ID).build()); | ||
} | ||
|
||
// Remove @property. | ||
for (Shape shape : model.getShapesWithTrait(PropertyTrait.class)) { | ||
updates.add(Shape.shapeToBuilder(shape).removeTrait(PropertyTrait.ID).build()); | ||
} | ||
|
||
return transformer.replaceShapes(model, updates); | ||
} | ||
|
||
private Model removeUnnecessaryDefaults(ModelTransformer transformer, Model model) { | ||
Set<Shape> updates = new HashSet<>(); | ||
|
||
// Remove addedDefault traits, and any found default trait if present. | ||
for (MemberShape shape : model.getMemberShapesWithTrait(AddedDefaultTrait.class)) { | ||
updates.add(shape.toBuilder().removeTrait(DefaultTrait.ID).removeTrait(AddedDefaultTrait.ID).build()); | ||
} | ||
|
||
for (Shape shape : model.getShapesWithTrait(DefaultTrait.class)) { | ||
DefaultTrait trait = shape.expectTrait(DefaultTrait.class); | ||
// Members with a null default are considered boxed. Keep the trait to retain consistency with other | ||
// indexes and checks. | ||
if (!trait.toNode().isNullNode()) { | ||
if (!NullableIndex.isDefaultZeroValueOfTypeInV1(trait.toNode(), shape.getType())) { | ||
updates.add(Shape.shapeToBuilder(shape) | ||
.removeTrait(DefaultTrait.ID) | ||
.removeTrait(AddedDefaultTrait.ID) | ||
.build()); | ||
} | ||
} | ||
} | ||
|
||
return transformer.replaceShapes(model, updates); | ||
} | ||
|
||
private Model removeOtherV2Traits(ModelTransformer transformer, Model model) { | ||
Set<Shape> updates = new HashSet<>(); | ||
|
||
for (StructureShape structure : model.getStructureShapes()) { | ||
for (MemberShape member : structure.getAllMembers().values()) { | ||
if (member.hasTrait(ClientOptionalTrait.class)) { | ||
updates.add(member.toBuilder().removeTrait(ClientOptionalTrait.ID).build()); | ||
} | ||
} | ||
} | ||
|
||
return transformer.replaceShapes(model, updates); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.