From e42814f8d2e8398c54605c91dc4f43cadb0b0666 Mon Sep 17 00:00:00 2001 From: Luc Talatinian Date: Mon, 26 Feb 2024 11:10:23 -0500 Subject: [PATCH] fold integrations --- .../smithy-go-codegen-test/smithy-build.json | 2 +- .../smithy/go/codegen/GoCodegenContext.java | 8 +- .../amazon/smithy/go/codegen/GoWriter.java | 1 - .../go/codegen/integration/GoIntegration.java | 8 + .../codegen/service/GoServiceIntegration.java | 166 ------------------ .../codegen/service/ServiceCodegenPlugin.java | 7 +- .../service/ServiceDirectedCodegen.java | 9 +- .../service/integration/DefaultProtocols.java | 14 +- ...mithy.go.codegen.integration.GoIntegration | 5 +- ...hy.go.codegen.service.GoServiceIntegration | 1 - 10 files changed, 39 insertions(+), 182 deletions(-) delete mode 100644 codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/service/GoServiceIntegration.java delete mode 100644 codegen/smithy-go-codegen/src/main/resources/META-INF/services/software.amazon.smithy.go.codegen.service.GoServiceIntegration diff --git a/codegen/smithy-go-codegen-test/smithy-build.json b/codegen/smithy-go-codegen-test/smithy-build.json index d0a3ff6a2..9f7bd5308 100644 --- a/codegen/smithy-go-codegen-test/smithy-build.json +++ b/codegen/smithy-go-codegen-test/smithy-build.json @@ -8,7 +8,7 @@ "generateGoMod": true, "goDirective": "1.18" }, - "go-service-codegen": { + "go-server-codegen": { "service": "example.weather#Weather", "module": "github.com/aws/smithy-go/internal/tests/service/weather", "moduleVersion": "0.0.1", diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoCodegenContext.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoCodegenContext.java index 3d1c47123..99ab77926 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoCodegenContext.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoCodegenContext.java @@ -20,14 +20,16 @@ import software.amazon.smithy.codegen.core.CodegenContext; import software.amazon.smithy.codegen.core.SymbolProvider; import software.amazon.smithy.codegen.core.WriterDelegator; -import software.amazon.smithy.go.codegen.service.GoServiceIntegration; +import software.amazon.smithy.go.codegen.integration.GoIntegration; import software.amazon.smithy.model.Model; +import software.amazon.smithy.utils.SmithyInternalApi; +@SmithyInternalApi public record GoCodegenContext( Model model, GoSettings settings, SymbolProvider symbolProvider, FileManifest fileManifest, WriterDelegator writerDelegator, - List integrations -) implements CodegenContext {} + List integrations +) implements CodegenContext {} diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoWriter.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoWriter.java index 3ccaba5d7..c6e98666d 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoWriter.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoWriter.java @@ -16,7 +16,6 @@ package software.amazon.smithy.go.codegen; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.List; diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/GoIntegration.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/GoIntegration.java index 6cf8b63e1..155371287 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/GoIntegration.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/GoIntegration.java @@ -26,6 +26,7 @@ import software.amazon.smithy.go.codegen.GoSettings.ArtifactType; import software.amazon.smithy.go.codegen.GoWriter; import software.amazon.smithy.go.codegen.TriConsumer; +import software.amazon.smithy.go.codegen.service.ServiceProtocolGenerator; import software.amazon.smithy.model.Model; import software.amazon.smithy.model.shapes.ServiceShape; import software.amazon.smithy.model.shapes.Shape; @@ -162,6 +163,13 @@ default List getProtocolGenerators() { return Collections.emptyList(); } + /** + * Gets a list of server protocol generators to register. Protocol generators should generally be written to accept + * the codegen context at construction time, such that all the model information necessary for codegen is available. + */ + default List getServerProtocolGenerators(GoCodegenContext ctx) { + return Collections.emptyList(); + } /** * Processes the finalized model before runtime plugins are consumed and diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/service/GoServiceIntegration.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/service/GoServiceIntegration.java deleted file mode 100644 index 7a4d42472..000000000 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/service/GoServiceIntegration.java +++ /dev/null @@ -1,166 +0,0 @@ -/* - * Copyright 2024 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.service; - -import java.util.Collections; -import java.util.List; -import java.util.function.Consumer; -import software.amazon.smithy.codegen.core.SmithyIntegration; -import software.amazon.smithy.codegen.core.SymbolProvider; -import software.amazon.smithy.codegen.core.WriterDelegator; -import software.amazon.smithy.go.codegen.GoCodegenContext; -import software.amazon.smithy.go.codegen.GoSettings; -import software.amazon.smithy.go.codegen.GoWriter; -import software.amazon.smithy.go.codegen.TriConsumer; -import software.amazon.smithy.model.Model; -import software.amazon.smithy.model.shapes.Shape; -import software.amazon.smithy.utils.SmithyUnstableApi; - -@SmithyUnstableApi -public interface GoServiceIntegration extends SmithyIntegration { - /** - * Gets the sort order of the customization from -128 to 127. - * - *

Customizations are applied according to this sort order. Lower values - * are executed before higher values (for example, -128 comes before 0, - * comes before 127). Customizations default to 0, which is the middle point - * between the minimum and maximum order values. The customization - * applied later can override the runtime configurations that provided - * by customizations applied earlier. - * - * @return Returns the sort order, defaulting to 0. - */ - default byte getOrder() { - return 0; - } - - default GoSettings.ArtifactType getArtifactType() { - return GoSettings.ArtifactType.SERVER; - } - - /** - * Preprocess the model before code generation. - * - *

This can be used to remove unsupported features, remove traits - * from shapes (e.g., make members optional), etc. - * - * @param model model definition. - * @param settings Setting used to generate. - * @return Returns the updated model. - */ - default Model preprocessModel(Model model, GoSettings settings) { - return model; - } - - /** - * Updates the {@link SymbolProvider} used when generating code. - * - *

This can be used to customize the names of shapes, the package - * that code is generated into, add dependencies, add imports, etc. - * - * @param settings Setting used to generate. - * @param model Model being generated. - * @param symbolProvider The original {@code SymbolProvider}. - * @return The decorated {@code SymbolProvider}. - */ - default SymbolProvider decorateSymbolProvider(GoSettings settings, Model model, SymbolProvider symbolProvider) { - return symbolProvider; - } - - /** - * Writes additional files. - * - * Called each time a writer is used that defines a shape. - * - *

Any mutations made on the writer (for example, adding - * section interceptors) are removed after the callback has completed; - * the callback is invoked in between pushing and popping state from - * the writer. - * - * @param settings Settings used to generate. - * @param model Model to generate from. - * @param symbolProvider Symbol provider used for codegen. - * @param writer Writer that will be used. - * @param definedShape Shape that is being defined in the writer. - */ - default void onShapeWriterUse( - GoSettings settings, Model model, SymbolProvider symbolProvider, GoWriter writer, Shape definedShape - ) { - } - - /** - * Writes additional files. - * - * @param settings Settings used to generate. - * @param model Model to generate from. - * @param symbolProvider Symbol provider used for codegen. - * @param writerFactory A factory function that takes the name of a file - * to write and a {@code Consumer} that receives a - * {@link GoSettings} to perform the actual writing to the file. - */ - default void writeAdditionalFiles( - GoSettings settings, - Model model, - SymbolProvider symbolProvider, - TriConsumer> writerFactory - ) { - } - - /** - * Writes additional files. - * - * @param settings Settings used to generate. - * @param model Model to generate from. - * @param symbolProvider Symbol provider used for codegen. - * @param delegator Writer delegator used to manage writer for the file. - */ - default void writeAdditionalFiles( - GoSettings settings, Model model, SymbolProvider symbolProvider, WriterDelegator delegator - ) { - } - - /** - * Gets a list of protocol generators to register. - * - * @return Returns the list of protocol generators to register. - */ - default List getProtocolGenerators(GoCodegenContext ctx) { - return Collections.emptyList(); - } - - /** - * Processes the finalized model before runtime plugins are consumed and - * code generation starts. This plugin can be used to add RuntimeClientPlugins - * to the integration's list of plugin. - * - * @param settings Settings used to generate. - * @param model Model to generate from. - */ - default void processFinalizedModel(GoSettings settings, Model model) { - } - - /** - * Processes the given serviceId and may return a unmodified, modified, or replacement value. - * - * @param settings Settings used to generate - * @param model model to generate from - * @param serviceId the serviceId - * @return the new serviceId - */ - default String processServiceId(GoSettings settings, Model model, String serviceId) { - return serviceId; - } -} diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/service/ServiceCodegenPlugin.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/service/ServiceCodegenPlugin.java index 306df3edf..df6a75bb7 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/service/ServiceCodegenPlugin.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/service/ServiceCodegenPlugin.java @@ -22,6 +22,7 @@ import software.amazon.smithy.go.codegen.GoCodegenContext; import software.amazon.smithy.go.codegen.GoSettings; import software.amazon.smithy.go.codegen.GoWriter; +import software.amazon.smithy.go.codegen.integration.GoIntegration; import software.amazon.smithy.utils.SmithyInternalApi; /** @@ -33,7 +34,7 @@ public final class ServiceCodegenPlugin implements SmithyBuildPlugin { @Override public String getName() { - return "go-service-codegen"; + return "go-server-codegen"; } @Override @@ -61,14 +62,14 @@ public void execute(PluginContext context) { private void generate(PluginContext context) { CodegenDirector runner = new CodegenDirector<>(); runner.model(context.getModel()); runner.directedCodegen(new ServiceDirectedCodegen()); - runner.integrationClass(GoServiceIntegration.class); + runner.integrationClass(GoIntegration.class); runner.fileManifest(context.getFileManifest()); diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/service/ServiceDirectedCodegen.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/service/ServiceDirectedCodegen.java index d3cae557f..1f1ef1c7f 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/service/ServiceDirectedCodegen.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/service/ServiceDirectedCodegen.java @@ -49,19 +49,22 @@ import software.amazon.smithy.go.codegen.StructureGenerator; import software.amazon.smithy.go.codegen.SymbolVisitor; import software.amazon.smithy.go.codegen.UnionGenerator; +import software.amazon.smithy.go.codegen.integration.GoIntegration; import software.amazon.smithy.model.knowledge.TopDownIndex; import software.amazon.smithy.model.shapes.EnumShape; import software.amazon.smithy.model.shapes.IntEnumShape; import software.amazon.smithy.model.shapes.StructureShape; +import software.amazon.smithy.utils.SmithyInternalApi; -public class ServiceDirectedCodegen implements DirectedCodegen { +@SmithyInternalApi +public class ServiceDirectedCodegen implements DirectedCodegen { @Override public SymbolProvider createSymbolProvider(CreateSymbolProviderDirective directive) { return new SymbolVisitor(withUnit(directive.model()), directive.settings()); } @Override - public GoCodegenContext createContext(CreateContextDirective directive) { + public GoCodegenContext createContext(CreateContextDirective directive) { return new GoCodegenContext( withUnit(directive.model()), directive.settings(), @@ -200,7 +203,7 @@ private ServiceProtocolGenerator resolveProtocolGenerator(GoCodegenContext ctx) var service = ctx.settings().getService(model); var protocolGenerators = ctx.integrations().stream() - .flatMap(it -> it.getProtocolGenerators(ctx).stream()) + .flatMap(it -> it.getServerProtocolGenerators(ctx).stream()) .filter(it -> service.hasTrait(it.getProtocol())) .toList(); if (protocolGenerators.isEmpty()) { diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/service/integration/DefaultProtocols.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/service/integration/DefaultProtocols.java index a7c738a2b..eef2442f4 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/service/integration/DefaultProtocols.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/service/integration/DefaultProtocols.java @@ -17,14 +17,22 @@ import java.util.List; import software.amazon.smithy.go.codegen.GoCodegenContext; -import software.amazon.smithy.go.codegen.service.GoServiceIntegration; +import software.amazon.smithy.go.codegen.GoSettings; +import software.amazon.smithy.go.codegen.integration.GoIntegration; import software.amazon.smithy.go.codegen.service.ServiceProtocolGenerator; import software.amazon.smithy.go.codegen.service.protocol.aws.AwsJson10ProtocolGenerator; import software.amazon.smithy.utils.ListUtils; +import software.amazon.smithy.utils.SmithyInternalApi; -public class DefaultProtocols implements GoServiceIntegration { +@SmithyInternalApi +public class DefaultProtocols implements GoIntegration { @Override - public List getProtocolGenerators(GoCodegenContext ctx) { + public GoSettings.ArtifactType getArtifactType() { + return GoSettings.ArtifactType.SERVER; + } + + @Override + public List getServerProtocolGenerators(GoCodegenContext ctx) { return ListUtils.of( new AwsJson10ProtocolGenerator(ctx) ); diff --git a/codegen/smithy-go-codegen/src/main/resources/META-INF/services/software.amazon.smithy.go.codegen.integration.GoIntegration b/codegen/smithy-go-codegen/src/main/resources/META-INF/services/software.amazon.smithy.go.codegen.integration.GoIntegration index 5c8f81c63..f9ce96950 100644 --- a/codegen/smithy-go-codegen/src/main/resources/META-INF/services/software.amazon.smithy.go.codegen.integration.GoIntegration +++ b/codegen/smithy-go-codegen/src/main/resources/META-INF/services/software.amazon.smithy.go.codegen.integration.GoIntegration @@ -13,4 +13,7 @@ software.amazon.smithy.go.codegen.endpoints.EndpointClientPluginsGenerator software.amazon.smithy.go.codegen.integration.auth.SigV4AuthScheme software.amazon.smithy.go.codegen.integration.auth.AnonymousAuthScheme -software.amazon.smithy.go.codegen.requestcompression.RequestCompression \ No newline at end of file +software.amazon.smithy.go.codegen.requestcompression.RequestCompression + +# server +software.amazon.smithy.go.codegen.service.integration.DefaultProtocols \ No newline at end of file diff --git a/codegen/smithy-go-codegen/src/main/resources/META-INF/services/software.amazon.smithy.go.codegen.service.GoServiceIntegration b/codegen/smithy-go-codegen/src/main/resources/META-INF/services/software.amazon.smithy.go.codegen.service.GoServiceIntegration deleted file mode 100644 index 2f8222d93..000000000 --- a/codegen/smithy-go-codegen/src/main/resources/META-INF/services/software.amazon.smithy.go.codegen.service.GoServiceIntegration +++ /dev/null @@ -1 +0,0 @@ -software.amazon.smithy.go.codegen.service.integration.DefaultProtocols \ No newline at end of file