|
18 | 18 | import software.amazon.smithy.model.shapes.ListShape;
|
19 | 19 | import software.amazon.smithy.model.shapes.MapShape;
|
20 | 20 | import software.amazon.smithy.model.shapes.MemberShape;
|
| 21 | +import software.amazon.smithy.model.shapes.SetShape; |
21 | 22 | import software.amazon.smithy.model.shapes.Shape;
|
22 | 23 | import software.amazon.smithy.model.shapes.StructureShape;
|
23 | 24 | import software.amazon.smithy.model.shapes.UnionShape;
|
@@ -104,9 +105,10 @@ private static void shape(Shape shape,
|
104 | 105 | target = shape;
|
105 | 106 | }
|
106 | 107 |
|
107 |
| - shapeTracker.mark(shape, indentation); |
108 |
| - if (shapeTracker.getOccurrenceDepths(shape) > 2) { |
109 |
| - append(indentation, buffer, "\"<" + shape.getId().getName() + ">\",\n"); |
| 108 | + shapeTracker.mark(target, indentation); |
| 109 | + if (shapeTracker.shouldTruncate(target)) { |
| 110 | + append(indentation, buffer, "\"<" + target.getId().getName() + ">\","); |
| 111 | + checkRequired(indentation, buffer, shape); |
110 | 112 | } else {
|
111 | 113 | switch (target.getType()) {
|
112 | 114 | case BIG_DECIMAL:
|
@@ -261,23 +263,41 @@ private static void append(int indentation, StringBuilder buffer, String tail) {
|
261 | 263 | * This handles the case of recursive shapes.
|
262 | 264 | */
|
263 | 265 | private static class ShapeTracker {
|
264 |
| - private Map<Shape, Set<Integer>> data = new HashMap<Shape, Set<Integer>>(); |
| 266 | + private Map<Shape, Set<Integer>> depths = new HashMap<Shape, Set<Integer>>(); |
| 267 | + private Map<Shape, Integer> occurrences = new HashMap<Shape, Integer>(); |
265 | 268 |
|
266 | 269 | /**
|
267 | 270 | * Mark that a shape is observed at depth.
|
268 | 271 | */
|
269 | 272 | public void mark(Shape shape, int depth) {
|
270 |
| - if (!data.containsKey(shape)) { |
271 |
| - data.put(shape, new HashSet<>()); |
| 273 | + if (!depths.containsKey(shape)) { |
| 274 | + depths.put(shape, new HashSet<>()); |
272 | 275 | }
|
273 |
| - data.get(shape).add(depth); |
| 276 | + depths.get(shape).add(depth); |
| 277 | + occurrences.put(shape, occurrences.getOrDefault(shape, 0) + 1); |
| 278 | + } |
| 279 | + |
| 280 | + /** |
| 281 | + * @return whether the shape should be truncated. |
| 282 | + */ |
| 283 | + public boolean shouldTruncate(Shape shape) { |
| 284 | + return (shape instanceof MapShape || shape instanceof UnionShape || shape instanceof StructureShape |
| 285 | + || shape instanceof ListShape || shape instanceof SetShape) |
| 286 | + && (getOccurrenceCount(shape) > 2 || getOccurrenceDepths(shape) > 2); |
274 | 287 | }
|
275 | 288 |
|
276 | 289 | /**
|
277 | 290 | * @return the number of distinct depths in which the shape appears.
|
278 | 291 | */
|
279 | 292 | public int getOccurrenceDepths(Shape shape) {
|
280 |
| - return data.getOrDefault(shape, Collections.emptySet()).size(); |
| 293 | + return depths.getOrDefault(shape, Collections.emptySet()).size(); |
| 294 | + } |
| 295 | + |
| 296 | + /** |
| 297 | + * @return total appearances of the shape. |
| 298 | + */ |
| 299 | + public int getOccurrenceCount(Shape shape) { |
| 300 | + return occurrences.getOrDefault(shape, 0); |
281 | 301 | }
|
282 | 302 | }
|
283 | 303 | }
|
0 commit comments