Skip to content

Commit

Permalink
feat(endpoint): endpoints v2 codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
kuhe committed Sep 12, 2022
1 parent d32963f commit d145496
Show file tree
Hide file tree
Showing 12 changed files with 703 additions and 3 deletions.
1 change: 1 addition & 0 deletions smithy-typescript-codegen/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ buildscript {

dependencies {
api("software.amazon.smithy:smithy-codegen-core:${rootProject.extra["smithyVersion"]}")
api("software.amazon.smithy:smithy-rules-engine:${rootProject.extra["smithyVersion"]}")
api("software.amazon.smithy:smithy-waiters:${rootProject.extra["smithyVersion"]}")
implementation("software.amazon.smithy:smithy-protocol-test-traits:${rootProject.extra["smithyVersion"]}")
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@
import software.amazon.smithy.model.traits.EnumTrait;
import software.amazon.smithy.model.traits.PaginatedTrait;
import software.amazon.smithy.model.validation.ValidationEvent;
import software.amazon.smithy.rulesengine.traits.EndpointRuleSetTrait;
import software.amazon.smithy.typescript.codegen.TypeScriptSettings.ArtifactType;
import software.amazon.smithy.typescript.codegen.endpointsV2.EndpointsV2Generator;
import software.amazon.smithy.typescript.codegen.integration.ProtocolGenerator;
import software.amazon.smithy.typescript.codegen.integration.RuntimeClientPlugin;
import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration;
Expand Down Expand Up @@ -323,6 +325,7 @@ public Void serviceShape(ServiceShape shape) {
}
if (settings.generateClient() || settings.generateServerSdk()) {
generateCommands(shape);
generateEndpointV2(shape);
}

if (settings.generateServerSdk()) {
Expand Down Expand Up @@ -444,4 +447,14 @@ private void generateCommands(ServiceShape shape) {
}
}
}

private void generateEndpointV2(ServiceShape shape) {
if (!shape.hasTrait(EndpointRuleSetTrait.class)) {
return;
}

new EndpointsV2Generator(
settings, model, fileManifest
).run();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
import software.amazon.smithy.model.shapes.OperationShape;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.model.shapes.StructureShape;
import software.amazon.smithy.rulesengine.traits.EndpointRuleSetTrait;
import software.amazon.smithy.typescript.codegen.endpointsV2.EndpointsParamNameMap;
import software.amazon.smithy.typescript.codegen.endpointsV2.RuleSetParameterFinder;
import software.amazon.smithy.typescript.codegen.integration.ProtocolGenerator;
import software.amazon.smithy.typescript.codegen.integration.RuntimeClientPlugin;
import software.amazon.smithy.utils.OptionalUtils;
Expand Down Expand Up @@ -64,6 +67,7 @@ final class CommandGenerator implements Runnable {
private final Symbol outputType;
private final ProtocolGenerator protocolGenerator;
private final ApplicationProtocol applicationProtocol;
private final boolean hasEndpointRuleSetTrait;

CommandGenerator(
TypeScriptSettings settings,
Expand Down Expand Up @@ -91,6 +95,9 @@ final class CommandGenerator implements Runnable {
operationIndex = OperationIndex.of(model);
inputType = symbol.expectProperty("inputType", Symbol.class);
outputType = symbol.expectProperty("outputType", Symbol.class);
hasEndpointRuleSetTrait = service.hasTrait(
EndpointRuleSetTrait.class
);
}

@Override
Expand Down Expand Up @@ -185,6 +192,46 @@ private void generateCommandMiddlewareResolver(String configType) {
// Add customizations.
addCommandSpecificPlugins();

// EndpointsV2
if (hasEndpointRuleSetTrait) {
writer.addImport(
"getEndpointPlugin",
"getEndpointPlugin",
"@aws-sdk/middleware-endpoint"
);
writer.openBlock(
"this.middlewareStack.use(getEndpointPlugin(configuration, {",
"}));",
() -> {
RuleSetParameterFinder parameterFinder = new RuleSetParameterFinder(service);
parameterFinder.getBuiltInParams().forEach((name, type) -> {
writer.write(
"$L: { type: \"builtInParams\", name: \"$L\" },",
name, EndpointsParamNameMap.getLocalName(name)
);
});
parameterFinder.getClientContextParams().forEach((name, type) -> {
writer.write(
"$L: { type: \"clientContextParams\", name: \"$L\" },",
name, EndpointsParamNameMap.getLocalName(name)
);
});
parameterFinder.getStaticContextParamValues(operation).forEach((name, value) -> {
writer.write(
"$L: { type: \"staticContextParams\", value: $L },",
name, value
);
});
parameterFinder.getContextParams(operation).forEach((name, type) -> {
writer.write(
"$L: { type: \"contextParams\", name: \"$L\" },",
name, EndpointsParamNameMap.getLocalName(name)
);
});
}
);
}

// Resolve the middleware stack.
writer.write("\nconst stack = clientStack.concat(this.middlewareStack);\n");
writer.write("const { logger } = configuration;");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,6 @@ private void generateMalformedRequestTest(OperationShape operation, HttpMalforme
});
writer.write("const r = await handler.handle(request, {});").write("");

// Assert that the function has been called exactly once.
writer.write("expect(testFunction.mock.calls.length).toBe(0);");

writeHttpResponseAssertions(testCase.getResponse());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import software.amazon.smithy.model.knowledge.TopDownIndex;
import software.amazon.smithy.model.shapes.OperationShape;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.rulesengine.traits.EndpointRuleSetTrait;
import software.amazon.smithy.typescript.codegen.integration.RuntimeClientPlugin;
import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration;
import software.amazon.smithy.utils.OptionalUtils;
Expand Down Expand Up @@ -169,7 +170,15 @@ private void generateConfig() {
if (!inputTypes.isEmpty()) {
writer.indent();
for (SymbolReference symbolReference : inputTypes) {
writer.write("& $T", symbolReference);
if (service.hasTrait(EndpointRuleSetTrait.class)
&& symbolReference.getAlias().equals("EndpointInputConfig")) {
writer.write("& $T<$L>", symbolReference, "EndpointParameters");
} else {
writer.write("& $T", symbolReference);
}
}
if (service.hasTrait(EndpointRuleSetTrait.class)) {
writer.write("& ClientInputEndpointParameters");
}
writer.dedent();
}
Expand All @@ -189,7 +198,17 @@ private void generateConfig() {
writer.indent();
runtimePlugins.stream()
.flatMap(p -> OptionalUtils.stream(p.getResolvedConfig()))
.forEach(symbol -> writer.write("& $T", symbol));
.forEach(symbol -> {
if (service.hasTrait(EndpointRuleSetTrait.class)
&& symbol.getAlias().equals("EndpointResolvedConfig")) {
writer.write("& $T<$L>", symbol, "EndpointParameters");
} else {
writer.write("& $T", symbol);
}
});
if (service.hasTrait(EndpointRuleSetTrait.class)) {
writer.write("& ClientResolvedEndpointParameters");
}
writer.dedent();
}

Expand Down Expand Up @@ -331,6 +350,14 @@ private void generateConstructor() {
}
}

if (service.hasTrait(EndpointRuleSetTrait.class)) {
configVariable++;
writer.write("let $L = $L($L);",
generateConfigVariable(configVariable),
"resolveClientEndpointParameters",
generateConfigVariable(configVariable - 1));
}

writer.write("super($L);", generateConfigVariable(configVariable));
writer.write("this.config = $L;", generateConfigVariable(configVariable));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public enum TypeScriptDependency implements SymbolDependencyContainer {
MIDDLEWARE_SERDE("dependencies", "@aws-sdk/middleware-serde", true),
MIDDLEWARE_RETRY("dependencies", "@aws-sdk/middleware-retry", true),
MIDDLEWARE_STACK("dependencies", "@aws-sdk/middleware-stack", true),
MIDDLEWARE_ENDPOINTS_V2("dependencies", "@aws-sdk/middleware-endpoint", true),

AWS_CRYPTO_SHA256_BROWSER("dependencies", "@aws-crypto/sha256-browser", "2.0.0", true),
AWS_CRYPTO_SHA256_JS("dependencies", "@aws-crypto/sha256-js", "2.0.0", true),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.smithy.typescript.codegen.endpointsV2;

import java.util.Map;
import software.amazon.smithy.utils.MapUtils;


public final class EndpointsParamNameMap {
/**
* Map of EndpointsV2 ruleset param name to existing JSv3 config param names.
*/
private static final Map<String, String> MAPPING = MapUtils.of(
"Region", "region",
"UseFIPS", "useFipsEndpoint",
"UseDualStack", "useDualstackEndpoint",
"ForcePathStyle", "forcePathStyle",
"Accelerate", "useAccelerateEndpoint",
"DisableMRAP", "disableMultiregionAccessPoints",
"UseArnRegion", "useArnRegion"
);

private EndpointsParamNameMap() {}

public static String getLocalName(String endpointsV2ParamName) {
return MAPPING.getOrDefault(endpointsV2ParamName, endpointsV2ParamName);
}
}
Loading

0 comments on commit d145496

Please sign in to comment.