@@ -32,7 +32,7 @@ public abstract class StructureExampleGenerator {
32
32
/**
33
33
* Generates an example structure for API documentation, as an
34
34
* automated gap filler for operations that do not have
35
- * hand written examples.
35
+ * handwritten examples.
36
36
*
37
37
* Example for Athena::createPreparedStatement
38
38
* ```js
@@ -44,65 +44,74 @@ public abstract class StructureExampleGenerator {
44
44
* };
45
45
* ```
46
46
*/
47
- public static String generateStructuralHintDocumentation (Shape shape , Model model , boolean isComment ) {
47
+ public static String generateStructuralHintDocumentation (
48
+ Shape shape ,
49
+ Model model ,
50
+ boolean isComment ,
51
+ boolean isInput
52
+ ) {
48
53
StringBuilder buffer = new StringBuilder ();
49
- shape (shape , buffer , model , 0 , new ShapeTracker ());
54
+ shape (shape , buffer , model , 0 , new ShapeTracker (), isInput );
50
55
51
56
// replace non-leading whitespace with single space.
52
57
String s = Arrays .stream (
53
58
buffer .toString ()
54
- .split ("\n " ))
55
- .map (line -> line .replaceAll (
56
- "([\\ w\\ \" ,:\\ [\\ {] )\\ s+" ,
57
- "$1" )
58
- .replaceAll ("\\ s+$" , "" ))
59
- .collect (Collectors .joining ((isComment ) ? "\n // " : "\n " ));
59
+ .split ("\n " ))
60
+ .map (line -> line .replaceAll (
61
+ "([\\ w\\ \" ,:\\ [\\ {] )\\ s+" ,
62
+ "$1" )
63
+ .replaceAll ("\\ s+$" , "" ))
64
+ .collect (Collectors .joining ((isComment ) ? "\n // " : "\n " ));
60
65
61
66
return ((isComment ) ? "// " : "" ) + s .replaceAll (",$" , ";" );
62
67
}
63
68
64
69
private static void structure (StructureShape structureShape ,
65
- StringBuilder buffer , Model model ,
66
- int indentation ,
67
- ShapeTracker shapeTracker ) {
68
- if (structureShape .getAllMembers ().size () == 0 ) {
70
+ StringBuilder buffer ,
71
+ Model model ,
72
+ int indentation ,
73
+ ShapeTracker shapeTracker ,
74
+ boolean isInput ) {
75
+ if (structureShape .getAllMembers ().isEmpty ()) {
69
76
append (indentation , buffer , "{}," );
70
77
checkRequired (indentation , buffer , structureShape );
71
78
} else {
72
79
append (indentation , buffer ,
73
- "{" + (shapeTracker .getOccurrenceCount (structureShape ) == 1
74
- ? " // " + structureShape .getId ().getName ()
75
- : "" ));
80
+ "{" + (shapeTracker .getOccurrenceCount (structureShape ) == 1
81
+ ? " // " + structureShape .getId ().getName ()
82
+ : "" ));
76
83
checkRequired (indentation , buffer , structureShape );
77
84
structureShape .getAllMembers ().values ().forEach (member -> {
78
85
append (indentation + 2 , buffer , member .getMemberName () + ": " );
79
- shape (member , buffer , model , indentation + 2 , shapeTracker );
86
+ shape (member , buffer , model , indentation + 2 , shapeTracker , isInput );
80
87
});
81
88
append (indentation , buffer , "},\n " );
82
89
}
83
90
}
84
91
85
92
private static void union (UnionShape unionShape ,
86
- StringBuilder buffer ,
87
- Model model ,
88
- int indentation ,
89
- ShapeTracker shapeTracker ) {
93
+ StringBuilder buffer ,
94
+ Model model ,
95
+ int indentation ,
96
+ ShapeTracker shapeTracker ,
97
+ boolean isInput ) {
90
98
append (indentation , buffer , "{" + (shapeTracker .getOccurrenceCount (unionShape ) == 1
91
- ? " // " + unionShape .getId ().getName ()
92
- : "// " ) + " Union: only one key present" );
99
+ ? " // " + unionShape .getId ().getName ()
100
+ : "// " ) + " Union: only one key present" );
93
101
checkRequired (indentation , buffer , unionShape );
94
102
unionShape .getAllMembers ().values ().forEach (member -> {
95
103
append (indentation + 2 , buffer , member .getMemberName () + ": " );
96
- shape (member , buffer , model , indentation + 2 , shapeTracker );
104
+ shape (member , buffer , model , indentation + 2 , shapeTracker , isInput );
97
105
});
98
106
append (indentation , buffer , "},\n " );
99
107
}
100
108
101
109
private static void shape (Shape shape ,
102
- StringBuilder buffer ,
103
- Model model ,
104
- int indentation ,
105
- ShapeTracker shapeTracker ) {
110
+ StringBuilder buffer ,
111
+ Model model ,
112
+ int indentation ,
113
+ ShapeTracker shapeTracker ,
114
+ boolean isInput ) {
106
115
Shape target ;
107
116
if (shape instanceof MemberShape ) {
108
117
target = model .getShape (((MemberShape ) shape ).getTarget ()).get ();
@@ -123,17 +132,31 @@ private static void shape(Shape shape,
123
132
append (indentation , buffer , "Number(\" bigint\" )," );
124
133
break ;
125
134
case BLOB :
126
- if (target .hasTrait (StreamingTrait .class )) {
127
- append (indentation , buffer , "\" STREAMING_BLOB_VALUE\" ," );
135
+ if (isInput ) {
136
+ if (target .hasTrait (StreamingTrait .class )) {
137
+ append (indentation , buffer ,
138
+ "\" MULTIPLE_TYPES_ACCEPTED\" , // see \\ @smithy/types -> StreamingBlobPayloadInputTypes"
139
+ );
140
+ } else {
141
+ append (indentation , buffer ,
142
+ """
143
+ new Uint8Array(), // e.g. Buffer.from("") or new TextEncoder().encode("")""" );
144
+ }
128
145
} else {
129
- append (indentation , buffer , "\" BLOB_VALUE\" ," );
146
+ if (target .hasTrait (StreamingTrait .class )) {
147
+ append (indentation , buffer ,
148
+ "\" <SdkStream>\" , // see \\ @smithy/types -> StreamingBlobPayloadOutputTypes" );
149
+ } else {
150
+ append (indentation , buffer ,
151
+ "new Uint8Array()," );
152
+ }
130
153
}
131
154
break ;
132
155
case BOOLEAN :
133
156
append (indentation , buffer , "true || false," );
134
157
break ;
135
158
case BYTE :
136
- append (indentation , buffer , "\" BYTE_VALUE \" , " );
159
+ append (indentation , buffer , "0, // BYTE_VALUE " );
137
160
break ;
138
161
case DOCUMENT :
139
162
append (indentation , buffer , "\" DOCUMENT_VALUE\" ," );
@@ -163,50 +186,50 @@ private static void shape(Shape shape,
163
186
case SET :
164
187
case LIST :
165
188
append (indentation , buffer , "[" + (shapeTracker .getOccurrenceCount (target ) == 1
166
- ? " // " + target .getId ().getName ()
167
- : "" ));
189
+ ? " // " + target .getId ().getName ()
190
+ : "" ));
168
191
checkRequired (indentation , buffer , shape );
169
192
ListShape list = (ListShape ) target ;
170
- shape (list .getMember (), buffer , model , indentation + 2 , shapeTracker );
193
+ shape (list .getMember (), buffer , model , indentation + 2 , shapeTracker , isInput );
171
194
append (indentation , buffer , "],\n " );
172
195
break ;
173
196
case MAP :
174
197
append (indentation , buffer , "{" + (shapeTracker .getOccurrenceCount (target ) == 1
175
- ? " // " + target .getId ().getName ()
176
- : "" ));
198
+ ? " // " + target .getId ().getName ()
199
+ : "" ));
177
200
checkRequired (indentation , buffer , shape );
178
201
append (indentation + 2 , buffer , "\" <keys>\" : " );
179
202
MapShape map = (MapShape ) target ;
180
203
shape (model .getShape (map .getValue ().getTarget ()).get (), buffer , model , indentation + 2 ,
181
- shapeTracker );
204
+ shapeTracker , isInput );
182
205
append (indentation , buffer , "},\n " );
183
206
break ;
184
207
185
208
case STRUCTURE :
186
209
StructureShape structure = (StructureShape ) target ;
187
- structure (structure , buffer , model , indentation , shapeTracker );
210
+ structure (structure , buffer , model , indentation , shapeTracker , isInput );
188
211
break ;
189
212
case UNION :
190
213
UnionShape union = (UnionShape ) target ;
191
- union (union , buffer , model , indentation , shapeTracker );
214
+ union (union , buffer , model , indentation , shapeTracker , isInput );
192
215
break ;
193
216
194
217
case ENUM :
195
218
EnumShape enumShape = (EnumShape ) target ;
196
219
String enumeration = enumShape .getEnumValues ()
197
- .values ()
198
- .stream ()
199
- .map (s -> "\" " + s + "\" " )
200
- .collect (Collectors .joining (" || " ));
220
+ .values ()
221
+ .stream ()
222
+ .map (s -> "\" " + s + "\" " )
223
+ .collect (Collectors .joining (" || " ));
201
224
append (indentation , buffer , enumeration + "," );
202
225
break ;
203
226
case INT_ENUM :
204
227
IntEnumShape intEnumShape = (IntEnumShape ) target ;
205
228
String intEnumeration = intEnumShape .getEnumValues ()
206
- .values ()
207
- .stream ()
208
- .map (i -> Integer .toString (i ))
209
- .collect (Collectors .joining (" || " ));
229
+ .values ()
230
+ .stream ()
231
+ .map (i -> Integer .toString (i ))
232
+ .collect (Collectors .joining (" || " ));
210
233
append (indentation , buffer , intEnumeration + "," );
211
234
break ;
212
235
case OPERATION :
@@ -272,8 +295,8 @@ private static void append(int indentation, StringBuilder buffer, String tail) {
272
295
* This handles the case of recursive shapes.
273
296
*/
274
297
private static class ShapeTracker {
275
- private Map <Shape , Set <Integer >> depths = new HashMap <Shape , Set < Integer > >();
276
- private Map <Shape , Integer > occurrences = new HashMap <Shape , Integer >();
298
+ private final Map <Shape , Set <Integer >> depths = new HashMap <>();
299
+ private final Map <Shape , Integer > occurrences = new HashMap <>();
277
300
278
301
/**
279
302
* Mark that a shape is observed at depth.
@@ -291,8 +314,8 @@ public void mark(Shape shape, int depth) {
291
314
*/
292
315
public boolean shouldTruncate (Shape shape ) {
293
316
return (shape instanceof MapShape || shape instanceof UnionShape || shape instanceof StructureShape
294
- || shape instanceof ListShape || shape instanceof SetShape )
295
- && (getOccurrenceCount (shape ) > 5 || getOccurrenceDepths (shape ) > 2 );
317
+ || shape instanceof ListShape || shape instanceof SetShape )
318
+ && (getOccurrenceCount (shape ) > 5 || getOccurrenceDepths (shape ) > 2 );
296
319
}
297
320
298
321
/**
0 commit comments