Skip to content

Commit

Permalink
Fix removal of applied non-prelude meta traits
Browse files Browse the repository at this point in the history
  • Loading branch information
kstich committed Nov 16, 2023
1 parent ac20bce commit e7f833d
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@

package software.amazon.smithy.model.neighbor;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;
Expand All @@ -23,9 +27,9 @@
import software.amazon.smithy.model.loader.Prelude;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.model.traits.TraitDefinition;
import software.amazon.smithy.utils.FunctionalUtils;
import software.amazon.smithy.utils.OptionalUtils;

/**
* Finds trait definitions that are not connected to a service shape.
Expand Down Expand Up @@ -64,10 +68,28 @@ public Set<Shape> compute(Model model) {
.map(Shape::getAllTraits)
.flatMap(traits -> traits.keySet().stream())
.distinct()
.flatMap(traitId -> OptionalUtils.stream(model.getShape(traitId)))
.flatMap(traitId -> getTraitShapes(model, traitId).stream())
.filter(keepFilter)
.forEach(unused::remove);

return unused;
}

private Collection<Shape> getTraitShapes(Model model, ShapeId traitId) {
return getTraitShapes(model, traitId, new HashMap<>()).values();
}

private Map<ShapeId, Shape> getTraitShapes(Model model, ShapeId traitId, Map<ShapeId, Shape> traitShapes) {
Optional<Shape> initialTraitShapeOp = model.getShape(traitId);
if (initialTraitShapeOp.isPresent()) {
Shape initialTraitShape = initialTraitShapeOp.get();
traitShapes.put(traitId, initialTraitShape);
for (ShapeId metaTraitId : initialTraitShape.getAllTraits().keySet()) {
if (!metaTraitId.equals(TraitDefinition.ID) && !traitShapes.containsKey(metaTraitId)) {
traitShapes.putAll(getTraitShapes(model, metaTraitId, traitShapes));
}
}
}
return traitShapes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
}
},
"traits": {
"smithy.api#trait": {}
"smithy.api#trait": {},
"ns.foo#meta": {},
"ns.foo#threeMeta": {}
}
},
"ns.foo#quux": {
Expand All @@ -23,6 +25,41 @@
"smithy.api#trait": {}
}
},
"ns.foo#meta": {
"type": "structure",
"members": {
"member": {
"target": "smithy.api#String"
}
},
"traits": {
"smithy.api#trait": {},
"ns.foo#tooMeta": {},
"ns.foo#threeMeta": {}
}
},
"ns.foo#tooMeta": {
"type": "structure",
"members": {
"member": {
"target": "smithy.api#String"
}
},
"traits": {
"smithy.api#trait": {}
}
},
"ns.foo#threeMeta": {
"type": "structure",
"members": {
"member": {
"target": "smithy.api#String"
}
},
"traits": {
"smithy.api#trait": {}
}
},
"ns.foo#MyService": {
"type": "service",
"version": "2017-01-19",
Expand Down

0 comments on commit e7f833d

Please sign in to comment.