22
22
import software .amazon .smithy .model .shapes .Shape ;
23
23
import software .amazon .smithy .model .shapes .StructureShape ;
24
24
import software .amazon .smithy .model .shapes .UnionShape ;
25
+ import software .amazon .smithy .model .traits .InputTrait ;
25
26
import software .amazon .smithy .model .traits .RequiredTrait ;
26
27
import software .amazon .smithy .model .traits .StreamingTrait ;
27
28
@@ -32,7 +33,7 @@ public abstract class StructureExampleGenerator {
32
33
/**
33
34
* Generates an example structure for API documentation, as an
34
35
* automated gap filler for operations that do not have
35
- * hand written examples.
36
+ * handwritten examples.
36
37
*
37
38
* Example for Athena::createPreparedStatement
38
39
* ```js
@@ -46,7 +47,8 @@ public abstract class StructureExampleGenerator {
46
47
*/
47
48
public static String generateStructuralHintDocumentation (Shape shape , Model model , boolean isComment ) {
48
49
StringBuilder buffer = new StringBuilder ();
49
- shape (shape , buffer , model , 0 , new ShapeTracker ());
50
+ boolean isInput = shape .hasTrait (InputTrait .class );
51
+ shape (shape , buffer , model , 0 , new ShapeTracker (), isInput );
50
52
51
53
// replace non-leading whitespace with single space.
52
54
String s = Arrays .stream (
@@ -62,10 +64,12 @@ public static String generateStructuralHintDocumentation(Shape shape, Model mode
62
64
}
63
65
64
66
private static void structure (StructureShape structureShape ,
65
- StringBuilder buffer , Model model ,
66
- int indentation ,
67
- ShapeTracker shapeTracker ) {
68
- if (structureShape .getAllMembers ().size () == 0 ) {
67
+ StringBuilder buffer ,
68
+ Model model ,
69
+ int indentation ,
70
+ ShapeTracker shapeTracker ,
71
+ boolean isInput ) {
72
+ if (structureShape .getAllMembers ().isEmpty ()) {
69
73
append (indentation , buffer , "{}," );
70
74
checkRequired (indentation , buffer , structureShape );
71
75
} else {
@@ -76,33 +80,35 @@ private static void structure(StructureShape structureShape,
76
80
checkRequired (indentation , buffer , structureShape );
77
81
structureShape .getAllMembers ().values ().forEach (member -> {
78
82
append (indentation + 2 , buffer , member .getMemberName () + ": " );
79
- shape (member , buffer , model , indentation + 2 , shapeTracker );
83
+ shape (member , buffer , model , indentation + 2 , shapeTracker , isInput );
80
84
});
81
85
append (indentation , buffer , "},\n " );
82
86
}
83
87
}
84
88
85
89
private static void union (UnionShape unionShape ,
86
- StringBuilder buffer ,
87
- Model model ,
88
- int indentation ,
89
- ShapeTracker shapeTracker ) {
90
+ StringBuilder buffer ,
91
+ Model model ,
92
+ int indentation ,
93
+ ShapeTracker shapeTracker ,
94
+ boolean isInput ) {
90
95
append (indentation , buffer , "{" + (shapeTracker .getOccurrenceCount (unionShape ) == 1
91
96
? " // " + unionShape .getId ().getName ()
92
97
: "// " ) + " Union: only one key present" );
93
98
checkRequired (indentation , buffer , unionShape );
94
99
unionShape .getAllMembers ().values ().forEach (member -> {
95
100
append (indentation + 2 , buffer , member .getMemberName () + ": " );
96
- shape (member , buffer , model , indentation + 2 , shapeTracker );
101
+ shape (member , buffer , model , indentation + 2 , shapeTracker , isInput );
97
102
});
98
103
append (indentation , buffer , "},\n " );
99
104
}
100
105
101
106
private static void shape (Shape shape ,
102
- StringBuilder buffer ,
103
- Model model ,
104
- int indentation ,
105
- ShapeTracker shapeTracker ) {
107
+ StringBuilder buffer ,
108
+ Model model ,
109
+ int indentation ,
110
+ ShapeTracker shapeTracker ,
111
+ boolean isInput ) {
106
112
Shape target ;
107
113
if (shape instanceof MemberShape ) {
108
114
target = model .getShape (((MemberShape ) shape ).getTarget ()).get ();
@@ -123,17 +129,30 @@ private static void shape(Shape shape,
123
129
append (indentation , buffer , "Number(\" bigint\" )," );
124
130
break ;
125
131
case BLOB :
126
- if (target .hasTrait (StreamingTrait .class )) {
127
- append (indentation , buffer , "\" STREAMING_BLOB_VALUE\" ," );
132
+ if (isInput ) {
133
+ if (target .hasTrait (StreamingTrait .class )) {
134
+ append (indentation , buffer ,
135
+ "\" MULTIPLE_TYPES_ACCEPTED\" , // see \@ smithy/types -> StreamingBlobPayloadInputTypes" );
136
+ } else {
137
+ append (indentation , buffer ,
138
+ """
139
+ new Uint8Array(), // e.g. Buffer.from("") or new TextEncoder().encode("")""" );
140
+ }
128
141
} else {
129
- append (indentation , buffer , "\" BLOB_VALUE\" ," );
142
+ if (target .hasTrait (StreamingTrait .class )) {
143
+ append (indentation , buffer ,
144
+ "\" <SdkStream>\" , // see \@ smithy/types -> StreamingBlobPayloadOutputTypes" );
145
+ } else {
146
+ append (indentation , buffer ,
147
+ "new Uint8Array()," );
148
+ }
130
149
}
131
150
break ;
132
151
case BOOLEAN :
133
152
append (indentation , buffer , "true || false," );
134
153
break ;
135
154
case BYTE :
136
- append (indentation , buffer , "\" BYTE_VALUE \" , " );
155
+ append (indentation , buffer , "0, // BYTE_VALUE " );
137
156
break ;
138
157
case DOCUMENT :
139
158
append (indentation , buffer , "\" DOCUMENT_VALUE\" ," );
@@ -167,7 +186,7 @@ private static void shape(Shape shape,
167
186
: "" ));
168
187
checkRequired (indentation , buffer , shape );
169
188
ListShape list = (ListShape ) target ;
170
- shape (list .getMember (), buffer , model , indentation + 2 , shapeTracker );
189
+ shape (list .getMember (), buffer , model , indentation + 2 , shapeTracker , isInput );
171
190
append (indentation , buffer , "],\n " );
172
191
break ;
173
192
case MAP :
@@ -178,17 +197,17 @@ private static void shape(Shape shape,
178
197
append (indentation + 2 , buffer , "\" <keys>\" : " );
179
198
MapShape map = (MapShape ) target ;
180
199
shape (model .getShape (map .getValue ().getTarget ()).get (), buffer , model , indentation + 2 ,
181
- shapeTracker );
200
+ shapeTracker , isInput );
182
201
append (indentation , buffer , "},\n " );
183
202
break ;
184
203
185
204
case STRUCTURE :
186
205
StructureShape structure = (StructureShape ) target ;
187
- structure (structure , buffer , model , indentation , shapeTracker );
206
+ structure (structure , buffer , model , indentation , shapeTracker , isInput );
188
207
break ;
189
208
case UNION :
190
209
UnionShape union = (UnionShape ) target ;
191
- union (union , buffer , model , indentation , shapeTracker );
210
+ union (union , buffer , model , indentation , shapeTracker , isInput );
192
211
break ;
193
212
194
213
case ENUM :
@@ -272,8 +291,8 @@ private static void append(int indentation, StringBuilder buffer, String tail) {
272
291
* This handles the case of recursive shapes.
273
292
*/
274
293
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 >();
294
+ private final Map <Shape , Set <Integer >> depths = new HashMap <>();
295
+ private final Map <Shape , Integer > occurrences = new HashMap <>();
277
296
278
297
/**
279
298
* Mark that a shape is observed at depth.
0 commit comments