58
58
import software .amazon .smithy .model .traits .ErrorTrait ;
59
59
import software .amazon .smithy .model .traits .EventHeaderTrait ;
60
60
import software .amazon .smithy .model .traits .EventPayloadTrait ;
61
+ import software .amazon .smithy .model .traits .HostLabelTrait ;
61
62
import software .amazon .smithy .model .traits .HttpErrorTrait ;
62
63
import software .amazon .smithy .model .traits .HttpTrait ;
63
64
import software .amazon .smithy .model .traits .MediaTypeTrait ;
@@ -1105,7 +1106,6 @@ private void generateOperationRequestDeserializer(
1105
1106
+ " output: $T,\n "
1106
1107
+ " context: $L\n "
1107
1108
+ "): Promise<$T> => {" , "}" , methodName , requestType , contextType , inputType , () -> {
1108
- // TODO: deserialize endpoint
1109
1109
// Start deserializing the response.
1110
1110
writer .openBlock ("const contents: $T = {" , "};" , inputType , () -> {
1111
1111
// Only set a type and the members if we have output.
@@ -1119,6 +1119,7 @@ private void generateOperationRequestDeserializer(
1119
1119
});
1120
1120
readQueryString (context , operation , bindingIndex );
1121
1121
readPath (context , operation , bindingIndex , trait );
1122
+ readHost (context , operation );
1122
1123
readRequestHeaders (context , operation , bindingIndex , "output" );
1123
1124
List <HttpBinding > documentBindings = readRequestBody (context , operation , bindingIndex );
1124
1125
// Track all shapes bound to the document so their deserializers may be generated.
@@ -1178,13 +1179,50 @@ private void readPath(
1178
1179
}
1179
1180
writer .write ("const pathRegex = new RegExp($S);" , pathRegexBuilder .toString ());
1180
1181
writer .write ("const parsedPath: RegExpMatchArray = output.endpoint.path.match(pathRegex);" );
1181
- for (HttpBinding binding : pathBindings ) {
1182
- Shape target = context .getModel ().expectShape (binding .getMember ().getTarget ());
1183
- String memberName = context .getSymbolProvider ().toMemberName (binding .getMember ());
1184
- String labelValue = getOutputValue (context , binding .getLocation (),
1185
- "parsedPath.groups." + binding .getLocationName (), binding .getMember (), target );
1186
- writer .write ("contents.$L = $L;" , memberName , labelValue );
1182
+ writer .openBlock ("if (parsedPath.groups !== undefined) {" , "}" , () -> {
1183
+ for (HttpBinding binding : pathBindings ) {
1184
+ Shape target = context .getModel ().expectShape (binding .getMember ().getTarget ());
1185
+ String memberName = context .getSymbolProvider ().toMemberName (binding .getMember ());
1186
+ String labelValue = getOutputValue (context , binding .getLocation (),
1187
+ "parsedPath.groups." + binding .getLocationName (), binding .getMember (), target );
1188
+ writer .write ("contents.$L = $L;" , memberName , labelValue );
1189
+ }
1190
+ });
1191
+ }
1192
+
1193
+ private void readHost (GenerationContext context , OperationShape operation ) {
1194
+ TypeScriptWriter writer = context .getWriter ();
1195
+ if (!operation .hasTrait (EndpointTrait .class )) {
1196
+ return ;
1187
1197
}
1198
+ EndpointTrait endpointTrait = operation .expectTrait (EndpointTrait .class );
1199
+ if (endpointTrait .getHostPrefix ().getLabels ().isEmpty ()) {
1200
+ return ;
1201
+ }
1202
+ // Anchor to the beginning since we're looking at the host's prefix
1203
+ StringBuilder endpointRegexBuilder = new StringBuilder ("^" );
1204
+ for (Segment segment : endpointTrait .getHostPrefix ().getSegments ()) {
1205
+ if (segment .isLabel ()) {
1206
+ // Create a named capture group for the segment so we can grab it later without regard to order.
1207
+ endpointRegexBuilder .append (String .format ("(?<%s>.*)" , segment .getContent ()));
1208
+ } else {
1209
+ endpointRegexBuilder .append (segment .getContent ().replace ("." , "\\ ." ));
1210
+ }
1211
+ }
1212
+ writer .write ("const hostRegex = new RegExp($S);" , endpointRegexBuilder .toString ());
1213
+ writer .write ("const parsedHost: RegExpMatchArray = output.endpoint.path.match(pathRegex);" );
1214
+ Shape input = context .getModel ().expectShape (operation .getInput ().get ());
1215
+ writer .openBlock ("if (parsedHost.groups !== undefined) {" , "}" , () -> {
1216
+ for (MemberShape member : input .members ()) {
1217
+ if (member .hasTrait (HostLabelTrait .class )) {
1218
+ Shape target = context .getModel ().expectShape (member .getTarget ());
1219
+ String memberName = context .getSymbolProvider ().toMemberName (member );
1220
+ String labelValue = getOutputValue (context , Location .LABEL ,
1221
+ "parsedHost.groups." + member .getMemberName (), member , target );
1222
+ writer .write ("contents.$L = $L;" , memberName , labelValue );
1223
+ }
1224
+ }
1225
+ });
1188
1226
}
1189
1227
1190
1228
private void generateOperationResponseDeserializer (
0 commit comments