|
6 | 6 |
|
7 | 7 | import java.util.ArrayList; |
8 | 8 | import java.util.Collection; |
9 | | -import java.util.Collections; |
10 | 9 | import java.util.HashMap; |
11 | 10 | import java.util.HashSet; |
12 | 11 | import java.util.List; |
|
16 | 15 | import java.util.Set; |
17 | 16 | import java.util.TreeSet; |
18 | 17 | import software.amazon.smithy.model.Model; |
19 | | -import software.amazon.smithy.model.loader.TopologicalShapeSort; |
20 | 18 | import software.amazon.smithy.model.shapes.AbstractShapeBuilder; |
21 | 19 | import software.amazon.smithy.model.shapes.CollectionShape; |
22 | 20 | import software.amazon.smithy.model.shapes.MemberShape; |
|
26 | 24 | import software.amazon.smithy.model.shapes.StructureShape; |
27 | 25 | import software.amazon.smithy.model.shapes.UnionShape; |
28 | 26 | import software.amazon.smithy.model.traits.MixinTrait; |
| 27 | +import software.amazon.smithy.utils.DependencyGraph; |
29 | 28 |
|
30 | 29 | /** |
31 | 30 | * Replaces shapes while ensuring that the transformed model is in a |
@@ -161,30 +160,35 @@ private void updateMixins(Model model, Model.Builder builder, Collection<Shape> |
161 | 160 | } |
162 | 161 |
|
163 | 162 | // Topologically sort the updated shapes to ensure shapes are updated in order. |
164 | | - TopologicalShapeSort sorter = new TopologicalShapeSort(); |
| 163 | + DependencyGraph<ShapeId> dependencyGraph = new DependencyGraph<>(); |
165 | 164 |
|
166 | 165 | // Add shapes that are mixins or use mixins. |
167 | 166 | for (Shape shape : model.toSet()) { |
168 | 167 | if (!shape.isMemberShape() && (shape.hasTrait(MixinTrait.ID) || !shape.getMixins().isEmpty())) { |
169 | | - sorter.enqueue(shape); |
| 168 | + dependencyGraph.add(shape.getId()); |
| 169 | + dependencyGraph.addDependencies(shape.getId(), shape.getMixins()); |
170 | 170 | } |
171 | 171 | } |
172 | 172 |
|
173 | 173 | // Add _all_ of the replacements in case mixins or the Mixin trait were removed from updated shapes. |
174 | 174 | for (Shape shape : replacements) { |
175 | 175 | // Enqueue member shapes with empty dependencies since the parent will be enqueued with them. |
176 | | - if (shape.isMemberShape()) { |
177 | | - sorter.enqueue(shape.getId(), Collections.emptySet()); |
178 | | - } else { |
179 | | - sorter.enqueue(shape); |
| 176 | + dependencyGraph.add(shape.getId()); |
| 177 | + if (!shape.isMemberShape()) { |
| 178 | + dependencyGraph.setDependencies(shape.getId(), shape.getMixins()); |
180 | 179 | } |
181 | 180 | } |
182 | 181 |
|
183 | | - List<ShapeId> sorted = sorter.dequeueSortedShapes(); |
184 | | - for (ShapeId toRebuild : sorted) { |
185 | | - Shape shape = updatedShapes.containsKey(toRebuild) |
186 | | - ? updatedShapes.get(toRebuild) |
187 | | - : model.expectShape(toRebuild); |
| 182 | + for (ShapeId toRebuild : dependencyGraph.toSortedList()) { |
| 183 | + Shape shape; |
| 184 | + if (updatedShapes.containsKey(toRebuild)) { |
| 185 | + shape = updatedShapes.get(toRebuild); |
| 186 | + } else if (model.getShape(toRebuild).isPresent()) { |
| 187 | + shape = model.getShape(toRebuild).get(); |
| 188 | + } else { |
| 189 | + continue; |
| 190 | + } |
| 191 | + |
188 | 192 | if (!shape.getMixins().isEmpty()) { |
189 | 193 | // We don't clear mixins here because a shape might have an inherited |
190 | 194 | // mixin member that was updated with an applied trait. Clearing mixins |
|
0 commit comments