From 2d06d8c142332a6d8e3154a778ceb02d015e03ae Mon Sep 17 00:00:00 2001 From: Steven Yuan Date: Sat, 23 Sep 2023 15:55:30 -0700 Subject: [PATCH] feat(experimentalIdentityAndAuth): add `getEndpointParameterInstructions()` to smithy context --- .../typescript/codegen/CommandGenerator.java | 5 ++ .../AddEndpointRuleSetIntegration.java | 32 +++++++++++ .../sections/SmithyContextCodeSection.java | 55 +++++++++++++++++++ ....codegen.integration.TypeScriptIntegration | 1 + 4 files changed, 93 insertions(+) create mode 100644 smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddEndpointRuleSetIntegration.java create mode 100644 smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/sections/SmithyContextCodeSection.java diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CommandGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CommandGenerator.java index 5c55890a4ad..f3d3a109992 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CommandGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CommandGenerator.java @@ -50,6 +50,7 @@ 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.typescript.codegen.sections.SmithyContextCodeSection; import software.amazon.smithy.typescript.codegen.validation.SensitiveDataFinder; import software.amazon.smithy.utils.OptionalUtils; import software.amazon.smithy.utils.SmithyInternalApi; @@ -361,6 +362,10 @@ private void generateCommandMiddlewareResolver(String configType) { writer.openBlock("[SMITHY_CONTEXT_KEY]: {", "},", () -> { writer.write("service: $S,", service.toShapeId().getName()); writer.write("operation: $S,", operation.toShapeId().getName()); + writer.injectSection(SmithyContextCodeSection.builder() + .service(service) + .operation(operation) + .build()); }); }); writer.write("const { requestHandler } = configuration;"); diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddEndpointRuleSetIntegration.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddEndpointRuleSetIntegration.java new file mode 100644 index 00000000000..12e058d87db --- /dev/null +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddEndpointRuleSetIntegration.java @@ -0,0 +1,32 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +package software.amazon.smithy.typescript.codegen.integration; + +import java.util.List; +import software.amazon.smithy.rulesengine.traits.EndpointRuleSetTrait; +import software.amazon.smithy.typescript.codegen.TypeScriptCodegenContext; +import software.amazon.smithy.typescript.codegen.TypeScriptWriter; +import software.amazon.smithy.typescript.codegen.sections.SmithyContextCodeSection; +import software.amazon.smithy.utils.CodeInterceptor; +import software.amazon.smithy.utils.CodeSection; +import software.amazon.smithy.utils.SmithyInternalApi; + +@SmithyInternalApi +public class AddEndpointRuleSetIntegration implements TypeScriptIntegration { + @Override + public List> interceptors( + TypeScriptCodegenContext codegenContext + ) { + return List.of(CodeInterceptor.appender(SmithyContextCodeSection.class, (w, s) -> { + if (s.getService().hasTrait(EndpointRuleSetTrait.ID)) { + w.openBlock("endpointRuleSet: {", "},", () -> { + w.write("getEndpointParameterInstructions: $T.getEndpointParameterInstructions,", + codegenContext.symbolProvider().toSymbol(s.getOperation())); + }); + } + })); + } +} diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/sections/SmithyContextCodeSection.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/sections/SmithyContextCodeSection.java new file mode 100644 index 00000000000..056662f3bcd --- /dev/null +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/sections/SmithyContextCodeSection.java @@ -0,0 +1,55 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +package software.amazon.smithy.typescript.codegen.sections; + +import software.amazon.smithy.model.shapes.OperationShape; +import software.amazon.smithy.model.shapes.ServiceShape; +import software.amazon.smithy.utils.CodeSection; +import software.amazon.smithy.utils.SmithyBuilder; +import software.amazon.smithy.utils.SmithyUnstableApi; + +@SmithyUnstableApi +public class SmithyContextCodeSection implements CodeSection { + private final ServiceShape service; + private final OperationShape operation; + + private SmithyContextCodeSection(Builder builder) { + service = SmithyBuilder.requiredState("service", builder.service); + operation = SmithyBuilder.requiredState("operation", builder.operation); + } + + public ServiceShape getService() { + return service; + } + + public OperationShape getOperation() { + return operation; + } + + public static Builder builder() { + return new Builder(); + } + + public static class Builder implements SmithyBuilder{ + private ServiceShape service; + private OperationShape operation; + + @Override + public SmithyContextCodeSection build() { + return new SmithyContextCodeSection(this); + } + + public Builder service(ServiceShape service) { + this.service = service; + return this; + } + + public Builder operation(OperationShape operation) { + this.operation = operation; + return this; + } + } +} diff --git a/smithy-typescript-codegen/src/main/resources/META-INF/services/software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration b/smithy-typescript-codegen/src/main/resources/META-INF/services/software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration index ed7a4969482..2e9b04f172c 100644 --- a/smithy-typescript-codegen/src/main/resources/META-INF/services/software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration +++ b/smithy-typescript-codegen/src/main/resources/META-INF/services/software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration @@ -13,3 +13,4 @@ software.amazon.smithy.typescript.codegen.integration.AddHttpApiKeyAuthPlugin software.amazon.smithy.typescript.codegen.integration.AddBaseServiceExceptionClass software.amazon.smithy.typescript.codegen.integration.AddSdkStreamMixinDependency software.amazon.smithy.typescript.codegen.integration.DefaultReadmeGenerator +software.amazon.smithy.typescript.codegen.integration.AddEndpointRuleSetIntegration