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

Fix removal of applied non-prelude meta traits #2042

Merged
merged 1 commit into from
Nov 16, 2023
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 @@ -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
Loading