Skip to content

Commit

Permalink
refactor endpoint bindings/middleware (#455)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucix-aws committed Oct 16, 2023
1 parent 5264be9 commit 5e6671f
Show file tree
Hide file tree
Showing 15 changed files with 478 additions and 589 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Set;
import java.util.TreeSet;
import software.amazon.smithy.codegen.core.CodegenException;
import software.amazon.smithy.codegen.core.Symbol;
import software.amazon.smithy.codegen.core.SymbolDependency;
import software.amazon.smithy.codegen.core.SymbolDependencyContainer;
import software.amazon.smithy.utils.SetUtils;
Expand Down Expand Up @@ -146,6 +147,24 @@ public String getVersion() {
return version;
}

/**
* Creates a Symbol for a name exported by this package.
* @param name The name.
* @return The symbol.
*/
public Symbol valueSymbol(String name) {
return SymbolUtils.createValueSymbolBuilder(name, this).build();
}

/**
* Creates a pointable Symbol for a name exported by this package.
* @param name The name.
* @return The symbol.
*/
public Symbol pointableSymbol(String name) {
return SymbolUtils.createPointableSymbolBuilder(name, this).build();
}

@Override
public List<SymbolDependency> getDependencies() {
Set<SymbolDependency> symbolDependencySet = new TreeSet<>(SetUtils.of(getSymbolDependency()));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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.go.codegen;

import software.amazon.smithy.codegen.core.Symbol;

/**
* Collection of Symbol constants for types in the go standard library.
*/
@SuppressWarnings({"checkstyle:ConstantName", "checkstyle:LineLength"})
public final class GoStdlibTypes {
private GoStdlibTypes() { }

public static final class Context {
public static final Symbol Context = SmithyGoDependency.CONTEXT.valueSymbol("Context");
}

public static final class Fmt {
public static final Symbol Errorf = SmithyGoDependency.FMT.valueSymbol("Errorf");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,17 @@ public static Writable goTemplate(String contents, Map<String, Object>... args)
};
}

/**
* Returns a Writable for the string and args to be composed inline to another writer's contents.
*
* @param content string to write.
* @param args Arguments to use when evaluating the contents string.
* @return Writable to be evaluated.
*/
public static Writable goTemplate(Object content, Object... args) {
return writer -> writer.write(content, args);
}

public static Writable goDocTemplate(String contents) {
return goDocTemplate(contents, new HashMap<>());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import software.amazon.smithy.codegen.core.CodegenException;
import software.amazon.smithy.codegen.core.Symbol;
import software.amazon.smithy.codegen.core.SymbolProvider;
import software.amazon.smithy.go.codegen.endpoints.EndpointParameterOperationBindingsGenerator;
import software.amazon.smithy.go.codegen.integration.MiddlewareRegistrar;
import software.amazon.smithy.go.codegen.integration.ProtocolGenerator;
import software.amazon.smithy.go.codegen.integration.RuntimeClientPlugin;
Expand Down Expand Up @@ -134,9 +135,12 @@ public void run() {
func (*$T) operationName() string {
return $S
}
$W
""",
inputSymbol,
operationSymbol.getName()
operationSymbol.getName(),
new EndpointParameterOperationBindingsGenerator(operation, inputShape, inputSymbol).generate()
);

// The output structure gets a metadata member added.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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.go.codegen;

import software.amazon.smithy.codegen.core.Symbol;

/**
* Collection of Symbol constants for types in the smithy-go runtime.
*/
@SuppressWarnings({"checkstyle:ConstantName", "checkstyle:LineLength"})
public final class SmithyGoTypes {
private SmithyGoTypes() { }

public static final class Ptr {
public static final Symbol String = SmithyGoDependency.SMITHY_PTR.valueSymbol("String");
public static final Symbol Bool = SmithyGoDependency.SMITHY_PTR.valueSymbol("Bool");
}

public static final class Middleware {
public static final Symbol Stack = SmithyGoDependency.SMITHY_MIDDLEWARE.pointableSymbol("Stack");
public static final Symbol SerializeInput = SmithyGoDependency.SMITHY_MIDDLEWARE.pointableSymbol("SerializeInput");
public static final Symbol SerializeOutput = SmithyGoDependency.SMITHY_MIDDLEWARE.pointableSymbol("SerializeOutput");
public static final Symbol SerializeHandler = SmithyGoDependency.SMITHY_MIDDLEWARE.pointableSymbol("SerializeHandler");
public static final Symbol Metadata = SmithyGoDependency.SMITHY_MIDDLEWARE.pointableSymbol("Metadata");
}

public static final class Transport {
public static final class Http {
public static final Symbol Request = SmithyGoDependency.SMITHY_HTTP_TRANSPORT.pointableSymbol("Request");
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
import java.util.List;
import java.util.Optional;
import software.amazon.smithy.codegen.core.Symbol;
import software.amazon.smithy.codegen.core.SymbolProvider;
import software.amazon.smithy.go.codegen.GoCodegenPlugin;
import software.amazon.smithy.go.codegen.GoSettings;
import software.amazon.smithy.go.codegen.SymbolUtils;
import software.amazon.smithy.go.codegen.integration.ConfigField;
Expand Down Expand Up @@ -48,11 +46,6 @@ public class EndpointClientPluginsGenerator implements GoIntegration {

private final List<RuntimeClientPlugin> runtimeClientPlugins = new ArrayList<>();


private static String getAddEndpointMiddlewareFuncName(String operationName) {
return String.format("add%sResolveEndpointMiddleware", operationName);
}

private static String getExportedParameterName(Parameter parameter) {
return StringUtils.capitalize(parameter.getName().getName().getValue());
}
Expand Down Expand Up @@ -106,18 +99,13 @@ public void processFinalizedModel(GoSettings settings, Model model) {
for (ToShapeId operationId : topDownIndex.getContainedOperations(service)) {
OperationShape operationShape = model.expectShape(operationId.toShapeId(), OperationShape.class);

SymbolProvider symbolProvider = GoCodegenPlugin.createSymbolProvider(model, settings);

String inputHelperFuncName = getAddEndpointMiddlewareFuncName(
symbolProvider.toSymbol(operationShape).getName()
);
Symbol addFunc = SymbolUtils.createValueSymbolBuilder(EndpointMiddlewareGenerator.ADD_FUNC_NAME).build();
runtimeClientPlugins.add(RuntimeClientPlugin.builder()
.operationPredicate((m, s, o) -> {
return o.equals(operationShape);
})
.registerMiddleware(MiddlewareRegistrar.builder()
.resolvedFunction(SymbolUtils.createValueSymbolBuilder(inputHelperFuncName)
.build())
.resolvedFunction(addFunc)
.useClientOptions()
.build())
.build());
Expand Down
Loading

0 comments on commit 5e6671f

Please sign in to comment.