Skip to content

Commit d91a387

Browse files
committed
improve documentation truncation behavior
1 parent 2409063 commit d91a387

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/documentation/StructureExampleGenerator.java

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import software.amazon.smithy.model.shapes.ListShape;
1919
import software.amazon.smithy.model.shapes.MapShape;
2020
import software.amazon.smithy.model.shapes.MemberShape;
21+
import software.amazon.smithy.model.shapes.SetShape;
2122
import software.amazon.smithy.model.shapes.Shape;
2223
import software.amazon.smithy.model.shapes.StructureShape;
2324
import software.amazon.smithy.model.shapes.UnionShape;
@@ -104,9 +105,10 @@ private static void shape(Shape shape,
104105
target = shape;
105106
}
106107

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);
110112
} else {
111113
switch (target.getType()) {
112114
case BIG_DECIMAL:
@@ -261,23 +263,41 @@ private static void append(int indentation, StringBuilder buffer, String tail) {
261263
* This handles the case of recursive shapes.
262264
*/
263265
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>();
265268

266269
/**
267270
* Mark that a shape is observed at depth.
268271
*/
269272
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<>());
272275
}
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);
274287
}
275288

276289
/**
277290
* @return the number of distinct depths in which the shape appears.
278291
*/
279292
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);
281301
}
282302
}
283303
}

0 commit comments

Comments
 (0)