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 ;
@@ -68,7 +69,10 @@ private static void structure(StructureShape structureShape,
68
69
append (indentation , buffer , "{}," );
69
70
checkRequired (indentation , buffer , structureShape );
70
71
} else {
71
- append (indentation , buffer , "{" );
72
+ append (indentation , buffer ,
73
+ "{" + (shapeTracker .getOccurrenceCount (structureShape ) == 1
74
+ ? " // " + structureShape .getId ().getName ()
75
+ : "" ));
72
76
checkRequired (indentation , buffer , structureShape );
73
77
structureShape .getAllMembers ().values ().forEach (member -> {
74
78
append (indentation + 2 , buffer , member .getMemberName () + ": " );
@@ -83,7 +87,9 @@ private static void union(UnionShape unionShape,
83
87
Model model ,
84
88
int indentation ,
85
89
ShapeTracker shapeTracker ) {
86
- append (indentation , buffer , "{ // Union: only one key present" );
90
+ append (indentation , buffer , "{" + (shapeTracker .getOccurrenceCount (unionShape ) == 1
91
+ ? " // " + unionShape .getId ().getName ()
92
+ : "// " ) + " Union: only one key present" );
87
93
checkRequired (indentation , buffer , unionShape );
88
94
unionShape .getAllMembers ().values ().forEach (member -> {
89
95
append (indentation + 2 , buffer , member .getMemberName () + ": " );
@@ -104,9 +110,10 @@ private static void shape(Shape shape,
104
110
target = shape ;
105
111
}
106
112
107
- shapeTracker .mark (shape , indentation );
108
- if (shapeTracker .getOccurrenceDepths (shape ) > 2 ) {
109
- append (indentation , buffer , "\" <" + shape .getId ().getName () + ">\" ,\n " );
113
+ shapeTracker .mark (target , indentation );
114
+ if (shapeTracker .shouldTruncate (target )) {
115
+ append (indentation , buffer , "\" <" + target .getId ().getName () + ">\" ," );
116
+ checkRequired (indentation , buffer , shape );
110
117
} else {
111
118
switch (target .getType ()) {
112
119
case BIG_DECIMAL :
@@ -155,14 +162,18 @@ private static void shape(Shape shape,
155
162
156
163
case SET :
157
164
case LIST :
158
- append (indentation , buffer , "[" );
165
+ append (indentation , buffer , "[" + (shapeTracker .getOccurrenceCount (target ) == 1
166
+ ? " // " + target .getId ().getName ()
167
+ : "" ));
159
168
checkRequired (indentation , buffer , shape );
160
169
ListShape list = (ListShape ) target ;
161
170
shape (list .getMember (), buffer , model , indentation + 2 , shapeTracker );
162
171
append (indentation , buffer , "],\n " );
163
172
break ;
164
173
case MAP :
165
- append (indentation , buffer , "{" );
174
+ append (indentation , buffer , "{" + (shapeTracker .getOccurrenceCount (target ) == 1
175
+ ? " // " + target .getId ().getName ()
176
+ : "" ));
166
177
checkRequired (indentation , buffer , shape );
167
178
append (indentation + 2 , buffer , "\" <keys>\" : " );
168
179
MapShape map = (MapShape ) target ;
@@ -261,23 +272,41 @@ private static void append(int indentation, StringBuilder buffer, String tail) {
261
272
* This handles the case of recursive shapes.
262
273
*/
263
274
private static class ShapeTracker {
264
- private Map <Shape , Set <Integer >> data = new HashMap <Shape , Set <Integer >>();
275
+ private Map <Shape , Set <Integer >> depths = new HashMap <Shape , Set <Integer >>();
276
+ private Map <Shape , Integer > occurrences = new HashMap <Shape , Integer >();
265
277
266
278
/**
267
279
* Mark that a shape is observed at depth.
268
280
*/
269
281
public void mark (Shape shape , int depth ) {
270
- if (!data .containsKey (shape )) {
271
- data .put (shape , new HashSet <>());
282
+ if (!depths .containsKey (shape )) {
283
+ depths .put (shape , new HashSet <>());
272
284
}
273
- data .get (shape ).add (depth );
285
+ depths .get (shape ).add (depth );
286
+ occurrences .put (shape , occurrences .getOrDefault (shape , 0 ) + 1 );
287
+ }
288
+
289
+ /**
290
+ * @return whether the shape should be truncated.
291
+ */
292
+ public boolean shouldTruncate (Shape shape ) {
293
+ return (shape instanceof MapShape || shape instanceof UnionShape || shape instanceof StructureShape
294
+ || shape instanceof ListShape || shape instanceof SetShape )
295
+ && (getOccurrenceCount (shape ) > 5 || getOccurrenceDepths (shape ) > 2 );
274
296
}
275
297
276
298
/**
277
299
* @return the number of distinct depths in which the shape appears.
278
300
*/
279
301
public int getOccurrenceDepths (Shape shape ) {
280
- return data .getOrDefault (shape , Collections .emptySet ()).size ();
302
+ return depths .getOrDefault (shape , Collections .emptySet ()).size ();
303
+ }
304
+
305
+ /**
306
+ * @return total appearances of the shape.
307
+ */
308
+ public int getOccurrenceCount (Shape shape ) {
309
+ return occurrences .getOrDefault (shape , 0 );
281
310
}
282
311
}
283
312
}
0 commit comments