Skip to content

Commit

Permalink
Modify retry middleware usage, and unexport endpoint middleware helpers.
Browse files Browse the repository at this point in the history
  • Loading branch information
skmcgrail committed Oct 2, 2020
1 parent 30a09c2 commit b9d8556
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ public List<RuntimeClientPlugin> getClientPlugins() {
.build())
.build(),

// Add newAttempt middleware to operation stack
// Add retryer middleware to operation stack
RuntimeClientPlugin.builder()
.registerMiddleware(MiddlewareRegistrar.builder()
.resolvedFunction(SymbolUtils.createValueSymbolBuilder(
"AddRetryMiddlewares", AwsGoDependency.AWS_RETRY)
AwsRetryMiddlewareHelper.ADD_RETRY_MIDDLEWARES_HELPER)
.build())
.useClientOptions()
.build())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2020 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.aws.go.codegen;

import software.amazon.smithy.codegen.core.Symbol;
import software.amazon.smithy.codegen.core.SymbolProvider;
import software.amazon.smithy.go.codegen.GoDelegator;
import software.amazon.smithy.go.codegen.GoSettings;
import software.amazon.smithy.go.codegen.GoWriter;
import software.amazon.smithy.go.codegen.SmithyGoDependency;
import software.amazon.smithy.go.codegen.SymbolUtils;
import software.amazon.smithy.go.codegen.integration.GoIntegration;
import software.amazon.smithy.model.Model;

public class AwsRetryMiddlewareHelper implements GoIntegration {
public static final String ADD_RETRY_MIDDLEWARES_HELPER = "addRetryMiddlewares";

@Override
public void writeAdditionalFiles(
GoSettings settings,
Model model,
SymbolProvider symbolProvider,
GoDelegator delegator
) {
delegator.useShapeWriter(settings.getService(model), this::generateRetryMiddlewareHelpers);
}

private void generateRetryMiddlewareHelpers(GoWriter writer) {
Symbol stackSymbol = SymbolUtils.createPointableSymbolBuilder("Stack", SmithyGoDependency.SMITHY_MIDDLEWARE)
.build();
Symbol addRetryMiddlewares = SymbolUtils.createValueSymbolBuilder("AddRetryMiddlewares",
AwsGoDependency.AWS_RETRY).build();

writer.openBlock("func $L(stack $P, o Options) error {", "}", ADD_RETRY_MIDDLEWARES_HELPER, stackSymbol,
() -> {
writer.write("return $T(stack, o.$L)", addRetryMiddlewares, AddAwsConfigFields.RETRYER_CONFIG_NAME);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,7 @@ private void writeServiceSignerConfig(Model model, GoWriter writer, ServiceShape
* @return if the SigV4 trait is used by the service.
*/
public static boolean isSupportedAuthentication(Model model, ServiceShape serviceShape) {
return model.getKnowledge(ServiceIndex.class)
.getAuthSchemes(serviceShape).values().stream().anyMatch(trait -> trait.getClass()
return ServiceIndex.of(model).getAuthSchemes(serviceShape).values().stream().anyMatch(trait -> trait.getClass()
.equals(SigV4Trait.class));
}

Expand All @@ -135,8 +134,7 @@ public static boolean isSupportedAuthentication(Model model, ServiceShape servic
* @return if SigV4Trait is an auth scheme for the operation and service.
*/
public static boolean hasSigV4AuthScheme(Model model, ServiceShape service, OperationShape operation) {
ServiceIndex serviceIndex = model.getKnowledge(ServiceIndex.class);
Map<ShapeId, Trait> auth = serviceIndex.getEffectiveAuthSchemes(service.getId(), operation.getId());
Map<ShapeId, Trait> auth = ServiceIndex.of(model).getEffectiveAuthSchemes(service.getId(), operation.getId());
return auth.containsKey(SigV4Trait.ID) && !operation.hasTrait(OptionalAuthTrait.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
*/
final class EndpointGenerator implements Runnable {
public static final String MIDDLEWARE_NAME = "ResolveEndpoint";
public static final String ADD_MIDDLEWARE_HELPER_NAME = String.format("Add%sMiddleware", MIDDLEWARE_NAME);
public static final String ADD_MIDDLEWARE_HELPER_NAME = String.format("add%sMiddleware", MIDDLEWARE_NAME);
public static final String RESOLVER_INTERFACE_NAME = "EndpointResolver";
public static final String RESOLVER_FUNC_NAME = "EndpointResolverFunc";
public static final String RESOLVER_OPTIONS = "ResolverOptions";
Expand Down Expand Up @@ -192,32 +192,22 @@ private void generateMiddleware(GoWriter writer) {

Symbol stackSymbol = SymbolUtils.createPointableSymbolBuilder("Stack", SmithyGoDependency.SMITHY_MIDDLEWARE)
.build();
Symbol optionsSymbol = SymbolUtils.createValueSymbolBuilder(String.format("%sMiddlewareOptions",
MIDDLEWARE_NAME)).build();

// Generate Middleware options interface
writer.openBlock("type $T interface {", "}", optionsSymbol, () -> {
writer.write("GetEndpointResolver() $L", RESOLVER_INTERFACE_NAME);
writer.write("GetEndpointOptions() $L", RESOLVER_OPTIONS);
});
writer.write("");

// Generate Middleware Adder Helper
writer.openBlock("func $L(stack $P, options $T) {", "}", ADD_MIDDLEWARE_HELPER_NAME, stackSymbol,
optionsSymbol, () -> {
writer.addUseImports(SmithyGoDependency.SMITHY_MIDDLEWARE);
String closeBlock = String.format("}, \"%s\", middleware.Before)",
ProtocolUtils.OPERATION_SERIALIZER_MIDDLEWARE_ID);
writer.openBlock("stack.Serialize.Insert(&$T{", closeBlock,
middleware.getMiddlewareSymbol(),
() -> {
writer.write("Resolver: options.GetEndpointResolver(),");
writer.write("Options: options.GetEndpointOptions(),");
});
});
writer.openBlock("func $L(stack $P, o Options) error {", "}", ADD_MIDDLEWARE_HELPER_NAME, stackSymbol, () -> {
writer.addUseImports(SmithyGoDependency.SMITHY_MIDDLEWARE);
String closeBlock = String.format("}, \"%s\", middleware.Before)",
ProtocolUtils.OPERATION_SERIALIZER_MIDDLEWARE_ID);
writer.openBlock("return stack.Serialize.Insert(&$T{", closeBlock,
middleware.getMiddlewareSymbol(),
() -> {
writer.write("Resolver: o.EndpointResolver,");
writer.write("Options: o.EndpointOptions,");
});
});
writer.write("");
// Generate Middleware Remover Helper
writer.openBlock("func Remove$LMiddleware(stack $P) error {", "}", middleware.getMiddlewareSymbol(),
writer.openBlock("func remove$LMiddleware(stack $P) error {", "}", middleware.getMiddlewareSymbol(),
stackSymbol, () -> {
writer.write("return stack.Serialize.Remove((&$T{}).ID())", middleware.getMiddlewareSymbol());
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected void generateOperationDocumentSerializer(
OperationShape operation
) {
Model model = context.getModel();
HttpBindingIndex bindingIndex = model.getKnowledge(HttpBindingIndex.class);
HttpBindingIndex bindingIndex = HttpBindingIndex.of(model);
Set<MemberShape> documentBindings = bindingIndex.getRequestBindings(operation, HttpBinding.Location.DOCUMENT)
.stream()
.map(HttpBinding::getMember)
Expand Down Expand Up @@ -184,7 +184,7 @@ protected void writeMiddlewareDocumentDeserializerDelegator(
boolean isShapeWithPayloadBinding = isShapeWithResponseBindings(model, operation, HttpBinding.Location.PAYLOAD);
if (isShapeWithPayloadBinding) {
// since payload trait can only be applied to a single member in a output shape
MemberShape memberShape = model.getKnowledge(HttpBindingIndex.class)
MemberShape memberShape = HttpBindingIndex.of(model)
.getResponseBindings(operation, HttpBinding.Location.PAYLOAD).stream()
.findFirst()
.orElseThrow(() -> new CodegenException("Expected payload binding member"))
Expand Down Expand Up @@ -258,7 +258,7 @@ protected void generateOperationDocumentDeserializer(
OperationShape operation
) {
Model model = context.getModel();
HttpBindingIndex bindingIndex = model.getKnowledge(HttpBindingIndex.class);
HttpBindingIndex bindingIndex = HttpBindingIndex.of(model);
Set<MemberShape> documentBindings = bindingIndex.getResponseBindings(operation, HttpBinding.Location.DOCUMENT)
.stream()
.map(HttpBinding::getMember)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ protected TimestampFormatTrait.Format getDocumentTimestampFormat() {
@Override
protected void generateOperationDocumentSerializer(GenerationContext context, OperationShape operation) {
Model model = context.getModel();
HttpBindingIndex bindingIndex = model.getKnowledge(HttpBindingIndex.class);
HttpBindingIndex bindingIndex = HttpBindingIndex.of(model);

Set<MemberShape> documentBindings = bindingIndex.getRequestBindings(operation, HttpBinding.Location.DOCUMENT)
.stream()
Expand Down Expand Up @@ -213,7 +213,7 @@ protected void writeMiddlewareDocumentDeserializerDelegator(

if (isShapeWithResponseBindings(model, operation, HttpBinding.Location.PAYLOAD)) {
// since payload trait can only be applied to a single member in a output shape
MemberShape memberShape = model.getKnowledge(HttpBindingIndex.class)
MemberShape memberShape = HttpBindingIndex.of(model)
.getResponseBindings(operation, HttpBinding.Location.PAYLOAD).stream()
.findFirst()
.orElseThrow(() -> new CodegenException("Expected payload binding member"))
Expand All @@ -239,7 +239,7 @@ protected void generateOperationDocumentDeserializer(
GenerationContext context, OperationShape operation
) {
Model model = context.getModel();
HttpBindingIndex bindingIndex = model.getKnowledge(HttpBindingIndex.class);
HttpBindingIndex bindingIndex = HttpBindingIndex.of(model);
Set<MemberShape> documentBindings = bindingIndex.getResponseBindings(operation, HttpBinding.Location.DOCUMENT)
.stream()
.map(HttpBinding::getMember)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ software.amazon.smithy.aws.go.codegen.AwsSignatureVersion4
software.amazon.smithy.aws.go.codegen.AwsIdempotencyTokenProvider
software.amazon.smithy.aws.go.codegen.AwsClientUserAgent
software.amazon.smithy.aws.go.codegen.AwsSdkServiceId
software.amazon.smithy.aws.go.codegen.AwsRetryMiddlewareHelper
software.amazon.smithy.aws.go.codegen.AWSRequestIDRetriever
software.amazon.smithy.aws.go.codegen.AWSResponseErrorWrapper
software.amazon.smithy.aws.go.codegen.customization.DynamoDBValidateResponseChecksum
Expand Down

0 comments on commit b9d8556

Please sign in to comment.