Skip to content

Commit

Permalink
Visit topological neighbors alphabetically
Browse files Browse the repository at this point in the history
  • Loading branch information
mtdowling committed Aug 28, 2020
1 parent 0bc2ce9 commit 8ab2c73
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.knowledge.KnowledgeIndex;
import software.amazon.smithy.model.knowledge.NeighborProviderIndex;
Expand Down Expand Up @@ -108,14 +109,20 @@ private void computeRecursiveShapes(Model model) {
private void visitShape(NeighborProvider provider, Shape shape) {
// Visit members before visiting containers. Note that no 'visited'
// set is needed since only non-recursive shapes are traversed.
// We sort the neighbors to better order the result.
Set<Shape> neighbors = new TreeSet<>();
for (Relationship rel : provider.getNeighbors(shape)) {
if (rel.getRelationshipType().getDirection() == RelationshipDirection.DIRECTED) {
if (!rel.getNeighborShapeId().equals(shape.getId()) && rel.getNeighborShape().isPresent()) {
visitShape(provider, rel.getNeighborShape().get());
neighbors.add(rel.getNeighborShape().get());
}
}
}

for (Shape neighbor : neighbors) {
visitShape(provider, neighbor);
}

shapes.add(shape);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ public void sortsTopologically() {
"smithy.example#MyString",
"smithy.example#BamList$member",
"smithy.example#BamList",
"smithy.example#Bar$bam",
"smithy.api#Integer",
"smithy.example#Bar$baz",
"smithy.example#Bar$bam",
"smithy.example#Bar",
"smithy.example#Foo$foo",
"smithy.example#Foo$bar",
"smithy.example#Foo$foo",
"smithy.example#Foo"));

assertThat(recursive, contains(
Expand Down

0 comments on commit 8ab2c73

Please sign in to comment.