Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add addTrait method to ModelAssembler #470

Merged
merged 2 commits into from
Jun 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
import software.amazon.smithy.model.node.Node;
import software.amazon.smithy.model.shapes.AbstractShapeBuilder;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.model.traits.Trait;
import software.amazon.smithy.model.traits.TraitFactory;
import software.amazon.smithy.model.validation.ValidatedResult;
import software.amazon.smithy.model.validation.ValidationEvent;
Expand Down Expand Up @@ -113,6 +115,7 @@ public final class ModelAssembler {
private final List<Node> documentNodes = new ArrayList<>();
private final List<Model> mergeModels = new ArrayList<>();
private final List<AbstractShapeBuilder<?, ?>> shapes = new ArrayList<>();
private final List<PendingTrait> pendingTraits = new ArrayList<>();
private final Map<String, Node> metadata = new HashMap<>();
private final Map<String, Object> properties = new HashMap<>();
private boolean disablePrelude;
Expand Down Expand Up @@ -142,6 +145,7 @@ public ModelAssembler copy() {
assembler.documentNodes.addAll(documentNodes);
assembler.mergeModels.addAll(mergeModels);
assembler.shapes.addAll(shapes);
assembler.pendingTraits.addAll(pendingTraits);
assembler.metadata.putAll(metadata);
assembler.disablePrelude = disablePrelude;
assembler.properties.putAll(properties);
Expand Down Expand Up @@ -173,6 +177,7 @@ public ModelAssembler copy() {
*/
public ModelAssembler reset() {
shapes.clear();
pendingTraits.clear();
metadata.clear();
mergeModels.clear();
inputStreamModels.clear();
Expand Down Expand Up @@ -365,6 +370,19 @@ public ModelAssembler addShapes(Shape... shapes) {
return this;
}

/**
* Explicitly adds a trait to a shape in the assembled model.
*
* @param target Shape to add the trait to.
* @param trait Trait to add.
* @return Returns the assembler.
*/
public ModelAssembler addTrait(ShapeId target, Trait trait) {
PendingTrait pendingTrait = new PendingTrait(target, trait);
this.pendingTraits.add(pendingTrait);
return this;
}

/**
* Merges a loaded model into the model assembler.
*
Expand Down Expand Up @@ -499,6 +517,9 @@ private ValidatedResult<Model> doAssemble(LoaderVisitor visitor) {

shapes.forEach(visitor::onShape);
metadata.forEach(visitor::onMetadata);
for (PendingTrait pendingTrait : pendingTraits) {
visitor.onTrait(pendingTrait.id, pendingTrait.trait);
}

for (Model model : mergeModels) {
mergeModelIntoVisitor(model, visitor);
Expand Down Expand Up @@ -546,4 +567,15 @@ private List<Validator> assembleValidators() {
copiedValidators.addAll(validators);
return copiedValidators;
}

private static final class PendingTrait {
final ShapeId id;
final Trait trait;

// A pending trait that's already created.
PendingTrait(ShapeId id, Trait trait) {
this.id = id;
this.trait = trait;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@
import software.amazon.smithy.model.node.Node;
import software.amazon.smithy.model.node.ObjectNode;
import software.amazon.smithy.model.node.StringNode;
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.StringShape;
import software.amazon.smithy.model.traits.DocumentationTrait;
import software.amazon.smithy.model.traits.MediaTypeTrait;
import software.amazon.smithy.model.traits.SensitiveTrait;
import software.amazon.smithy.model.traits.SuppressTrait;
import software.amazon.smithy.model.validation.Severity;
import software.amazon.smithy.model.validation.ValidatedResult;
import software.amazon.smithy.model.validation.ValidationEvent;
Expand Down Expand Up @@ -88,6 +90,21 @@ public void addsExplicitShapes() {
assertThat(result.unwrap().getShape(ShapeId.from("ns.foo#Bar")), is(Optional.of(shape)));
}

@Test
public void addsExplicitTraits() {
StringShape shape = StringShape.builder().id("ns.foo#Bar").build();
SuppressTrait trait = SuppressTrait.builder().build();
ValidatedResult<Model> result = new ModelAssembler()
.addShape(shape)
.addTrait(shape.toShapeId(), trait)
.assemble();

assertThat(result.getValidationEvents(), empty());
Shape resultShape = result.unwrap().getShape(ShapeId.from("ns.foo#Bar")).get();
assertTrue(resultShape.findTrait("smithy.api#suppress").isPresent());
srchase marked this conversation as resolved.
Show resolved Hide resolved
assertTrue(resultShape.getTrait(SuppressTrait.class).isPresent());
}

@Test
public void addsExplicitDocumentNode_1_0_0() {
ObjectNode node = Node.objectNode()
Expand Down