Skip to content

Minor refactoring endpoint trait code generation #883

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,6 @@ public final class HostPrefixProcessor {

private static final Pattern PATTERN = Pattern.compile(CURLY_BRACES_PATTERN);

/**
* This is the same as the {@link EndpointTrait#hostPrefix} expression with labels replaced by "%s"
*
* For example, if expression in host trait is "{Bucket}-{AccountId}-", then
* hostWithStringSpecifier will be "%s-%s-"
*/
private String hostWithStringSpecifier;

/**
* The list of member c2j names in input shape that are referenced in the host expression.
*
Expand All @@ -50,7 +42,6 @@ public final class HostPrefixProcessor {
private List<String> c2jNames;

public HostPrefixProcessor(String hostExpression) {
this.hostWithStringSpecifier = hostExpression;
this.c2jNames = new ArrayList<>();
replaceHostLabelsWithStringSpecifier(hostExpression);
}
Expand All @@ -68,14 +59,9 @@ private void replaceHostLabelsWithStringSpecifier(String hostExpression) {
while (matcher.find()) {
String matched = matcher.group(1);
c2jNames.add(matched);
hostWithStringSpecifier = hostWithStringSpecifier.replaceFirst("\\{" + matched + "}", "%s");
}
}

public String hostWithStringSpecifier() {
return hostWithStringSpecifier;
}

public List<String> c2jNames() {
return c2jNames;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.squareup.javapoet.TypeName;
import com.squareup.javapoet.TypeVariableName;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import javax.lang.model.element.Modifier;
import software.amazon.awssdk.auth.signer.EventStreamAws4Signer;
import software.amazon.awssdk.awscore.AwsRequestOverrideConfiguration;
Expand Down Expand Up @@ -172,22 +171,16 @@ static CodeBlock addEndpointTraitCode(OperationModel opModel) {
String hostPrefix = opModel.getEndpointTrait().getHostPrefix();
HostPrefixProcessor processor = new HostPrefixProcessor(hostPrefix);

builder.addStatement("String hostPrefix = $S", hostPrefix);

if (processor.c2jNames().isEmpty()) {
builder.addStatement("String resolvedHostExpression = $S", processor.hostWithStringSpecifier());
} else {
processor.c2jNames()
.forEach(name -> builder.addStatement("$T.paramNotBlank($L, $S)", Validate.class,
inputShapeMemberGetter(opModel, name),
name));

builder.addStatement("String resolvedHostExpression = String.format($S, $L)",
processor.hostWithStringSpecifier(),
processor.c2jNames().stream()
.map(n -> inputShapeMemberGetter(opModel, n))
.collect(Collectors.joining(",")));
}
processor.c2jNames()
.forEach(name -> builder.addStatement("$T.paramNotBlank($L, $S)", Validate.class,
inputShapeMemberGetter(opModel, name),
name));

builder.add("String resolvedHostExpression = $S", hostPrefix);
processor.c2jNames().stream()
.forEach(name -> builder.add(".replace(\"{$L}\", $L)",
name, inputShapeMemberGetter(opModel, name)));
builder.add(";");
}

return builder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

package software.amazon.awssdk.codegen.model.service;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.empty;
Expand All @@ -29,7 +28,6 @@ public void staticHostLabel() {
String hostPrefix = "data-";

HostPrefixProcessor processor = new HostPrefixProcessor(hostPrefix);
assertThat(processor.hostWithStringSpecifier(), equalTo("data-"));
assertThat(processor.c2jNames(), empty());
}

Expand All @@ -38,7 +36,6 @@ public void inputShapeLabels() {
String hostPrefix = "{Bucket}-{AccountId}.";

HostPrefixProcessor processor = new HostPrefixProcessor(hostPrefix);
assertThat(processor.hostWithStringSpecifier(), equalTo("%s-%s."));
assertThat(processor.c2jNames(), contains("Bucket", "AccountId"));
}

Expand All @@ -48,7 +45,6 @@ public void emptyCurlyBraces() {
String host = "{}.foo";

HostPrefixProcessor processor = new HostPrefixProcessor(host);
assertThat(processor.hostWithStringSpecifier(), equalTo("{}.foo"));
assertThat(processor.c2jNames(), empty());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"requestUri": "/"
},
"endpoint": {
"hostPrefix": "{StringMember}-foo."
"hostPrefix": "{StringMember}-{HostPrefixMember}."
},
"input": {
"shape": "APostOperationRequest"
Expand Down Expand Up @@ -179,6 +179,11 @@
"shape": "String",
"hostLabel": true,
"documentation": "<p>Member to compute the endpoint host</p>"
},
"HostPrefixMember": {
"shape": "String",
"hostLabel": true,
"documentation": "<p>Second member to compute the endpoint host</p>"
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,10 @@ public final String serviceName() {
@Override
public CompletableFuture<APostOperationResponse> aPostOperation(APostOperationRequest aPostOperationRequest) {
try {
String hostPrefix = "{StringMember}-foo.";
Validate.paramNotBlank(aPostOperationRequest.stringMember(), "StringMember");
String resolvedHostExpression = String.format("%s-foo.", aPostOperationRequest.stringMember());
Validate.paramNotBlank(aPostOperationRequest.hostPrefixMember(), "HostPrefixMember");
String resolvedHostExpression = "{StringMember}-{HostPrefixMember}.".replace("{StringMember}",
aPostOperationRequest.stringMember()).replace("{HostPrefixMember}", aPostOperationRequest.hostPrefixMember());
JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false)
.isPayloadJson(true).build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,10 @@ public final String serviceName() {
@Override
public APostOperationResponse aPostOperation(APostOperationRequest aPostOperationRequest) throws InvalidInputException,
AwsServiceException, SdkClientException, JsonException {
String hostPrefix = "{StringMember}-foo.";
Validate.paramNotBlank(aPostOperationRequest.stringMember(), "StringMember");
String resolvedHostExpression = String.format("%s-foo.", aPostOperationRequest.stringMember());
Validate.paramNotBlank(aPostOperationRequest.hostPrefixMember(), "HostPrefixMember");
String resolvedHostExpression = "{StringMember}-{HostPrefixMember}.".replace("{StringMember}",
aPostOperationRequest.stringMember()).replace("{HostPrefixMember}", aPostOperationRequest.hostPrefixMember());
JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false)
.isPayloadJson(true).build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ public final String serviceName() {
@Override
public APostOperationResponse aPostOperation(APostOperationRequest aPostOperationRequest) throws InvalidInputException,
AwsServiceException, SdkClientException, QueryException {
String hostPrefix = "foo-";
String resolvedHostExpression = "foo-";

HttpResponseHandler<APostOperationResponse> responseHandler = protocolFactory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"requestUri":"/{PathParam}"
},
"endpoint": {
"hostPrefix": "{StringMember}-"
"hostPrefix": "{StringMember}-{PathIdempotentToken}."
},
"input":{"shape":"EndpointTraitStructureTwo"},
"output":{"shape":"EndpointTraitStructureTwo"}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ public void hostExpression_withInputMemberLabel() throws URISyntaxException {
verify(mockHttpClient).prepareRequest(httpRequestCaptor.capture());

SdkHttpRequest request = httpRequestCaptor.getAllValues().get(0).httpRequest();
assertThat(request.host()).isEqualTo("123456-localhost.com");
assertThat(request.host()).isEqualTo("123456-dummypath.localhost.com");
assertThat(request.port()).isEqualTo(443);
assertThat(request.encodedPath()).isEqualTo("/dummypath");
assertThat(request.getUri()).isEqualTo(new URI("http://123456-localhost.com:443/dummypath"));
assertThat(request.getUri()).isEqualTo(new URI("http://123456-dummypath.localhost.com:443/dummypath"));
}
}

Expand Down Expand Up @@ -137,6 +137,7 @@ public void clientWithDisabledHostPrefix_withInputMemberLabel_usesOriginalUri()
try {
clientWithDisabledHostPrefix.endpointTraitTwo(EndpointTraitTwoRequest.builder()
.stringMember("123456")
.pathIdempotentToken("dummypath")
.build());
Assert.fail("Expected an exception");
} catch (SdkClientException exception) {
Expand Down