Skip to content

Commit

Permalink
feat: add service codegen framework (#497)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucix-aws authored Mar 1, 2024
1 parent 1951f00 commit 5893827
Show file tree
Hide file tree
Showing 39 changed files with 2,342 additions and 96 deletions.
1 change: 1 addition & 0 deletions codegen/smithy-go-codegen-test/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,6 @@ repositories {

dependencies {
implementation("software.amazon.smithy:smithy-protocol-test-traits:$smithyVersion")
implementation("software.amazon.smithy:smithy-aws-traits:$smithyVersion")
implementation(project(":smithy-go-codegen"))
}
1 change: 1 addition & 0 deletions codegen/smithy-go-codegen-test/model/main.smithy
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use smithy.waiters#waitable
/// Provides weather forecasts.
@httpBearerAuth
@fakeProtocol
@aws.protocols#awsJson1_0
@paginated(inputToken: "nextToken", outputToken: "nextToken", pageSize: "pageSize")
service Weather {
version: "2006-03-01",
Expand Down
7 changes: 7 additions & 0 deletions codegen/smithy-go-codegen-test/smithy-build.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
"moduleVersion": "0.0.1",
"generateGoMod": true,
"goDirective": "1.18"
},
"go-server-codegen": {
"service": "example.weather#Weather",
"module": "github.com/aws/smithy-go/internal/tests/server/weather",
"moduleVersion": "0.0.1",
"generateGoMod": true,
"goDirective": "1.18"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import software.amazon.smithy.codegen.core.Symbol;
import software.amazon.smithy.codegen.core.SymbolDependency;
import software.amazon.smithy.codegen.core.SymbolProvider;
import software.amazon.smithy.go.codegen.GoSettings.ArtifactType;
import software.amazon.smithy.go.codegen.integration.GoIntegration;
import software.amazon.smithy.go.codegen.integration.ProtocolGenerator;
import software.amazon.smithy.go.codegen.integration.RuntimeClientPlugin;
Expand All @@ -51,10 +52,12 @@
import software.amazon.smithy.model.traits.EnumTrait;
import software.amazon.smithy.model.transform.ModelTransformer;
import software.amazon.smithy.utils.OptionalUtils;
import software.amazon.smithy.utils.SmithyInternalApi;

/**
* Orchestrates Go client generation.
*/
@SmithyInternalApi
final class CodegenVisitor extends ShapeVisitor.Default<Void> {

private static final Logger LOGGER = Logger.getLogger(CodegenVisitor.class.getName());
Expand All @@ -79,8 +82,10 @@ final class CodegenVisitor extends ShapeVisitor.Default<Void> {
LOGGER.info("Attempting to discover GoIntegration from the classpath...");
ServiceLoader.load(GoIntegration.class, loader)
.forEach(integration -> {
LOGGER.info(() -> "Adding GoIntegration: " + integration.getClass().getName());
integrations.add(integration);
if (integration.getArtifactType().equals(ArtifactType.CLIENT)) {
LOGGER.info(() -> "Adding GoIntegration: " + integration.getClass().getName());
integrations.add(integration);
}
});
integrations.sort(Comparator.comparingInt(GoIntegration::getOrder));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,21 @@
import software.amazon.smithy.model.shapes.StringShape;
import software.amazon.smithy.model.traits.EnumDefinition;
import software.amazon.smithy.model.traits.EnumTrait;
import software.amazon.smithy.utils.SmithyInternalApi;
import software.amazon.smithy.utils.StringUtils;

/**
* Renders enums and their constants.
*/
final class EnumGenerator implements Runnable {
@SmithyInternalApi
public final class EnumGenerator implements Runnable {
private static final Logger LOGGER = Logger.getLogger(EnumGenerator.class.getName());

private final SymbolProvider symbolProvider;
private final GoWriter writer;
private final StringShape shape;

EnumGenerator(SymbolProvider symbolProvider, GoWriter writer, StringShape shape) {
public EnumGenerator(SymbolProvider symbolProvider, GoWriter writer, StringShape shape) {
this.symbolProvider = symbolProvider;
this.writer = writer;
this.shape = shape;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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;

import java.util.List;
import software.amazon.smithy.build.FileManifest;
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.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<GoWriter> writerDelegator,
List<GoIntegration> integrations
) implements CodegenContext<GoSettings, GoWriter, GoIntegration> {}
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@
import software.amazon.smithy.codegen.core.SymbolProvider;
import software.amazon.smithy.codegen.core.SymbolReference;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.utils.SmithyInternalApi;

/**
* Manages writers for Go files.Based off of GoWriterDelegator adding support
* for getting shape specific GoWriters.
*/
@SmithyInternalApi
public final class GoDelegator extends GoWriterDelegator {
private final SymbolProvider symbolProvider;

GoDelegator(FileManifest fileManifest, SymbolProvider symbolProvider) {
public GoDelegator(FileManifest fileManifest, SymbolProvider symbolProvider) {
super(fileManifest);

this.symbolProvider = symbolProvider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,21 @@
import java.util.logging.Logger;
import software.amazon.smithy.build.FileManifest;
import software.amazon.smithy.codegen.core.CodegenException;
import software.amazon.smithy.utils.SmithyInternalApi;

/**
* Generates a go.mod file for the project.
*
* <p>See here for more information on the format: https://github.com/golang/go/wiki/Modules#gomod
*/
final class GoModGenerator {
@SmithyInternalApi
public final class GoModGenerator {

private static final Logger LOGGER = Logger.getLogger(GoModGenerator.class.getName());

private GoModGenerator() {}

static void writeGoMod(
public static void writeGoMod(
GoSettings settings,
FileManifest manifest,
GoModuleInfo goModuleInfo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
import software.amazon.smithy.model.node.ObjectNode;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.utils.SmithyInternalApi;

/**
* Settings used by {@link GoCodegenPlugin}.
*/
@SmithyInternalApi
public final class GoSettings {

private static final String SERVICE = "service";
Expand All @@ -45,6 +47,13 @@ public final class GoSettings {
private Boolean generateGoMod = false;
private String goDirective = GoModuleInfo.DEFAULT_GO_DIRECTIVE;
private ShapeId protocol;
private ArtifactType artifactType;

@SmithyInternalApi
public enum ArtifactType {
CLIENT,
SERVER;
}

/**
* Create a settings object from a configuration object node.
Expand All @@ -53,10 +62,15 @@ public final class GoSettings {
* @return Returns the extracted settings.
*/
public static GoSettings from(ObjectNode config) {
return from(config, ArtifactType.CLIENT);
}

@SmithyInternalApi
public static GoSettings from(ObjectNode config, ArtifactType artifactType) {
GoSettings settings = new GoSettings();
config.warnIfAdditionalProperties(
Arrays.asList(SERVICE, MODULE_NAME, MODULE_DESCRIPTION, MODULE_VERSION, GENERATE_GO_MOD, GO_DIRECTIVE));

settings.setArtifactType(artifactType);
settings.setService(config.expectStringMember(SERVICE).expectShapeId());
settings.setModuleName(config.expectStringMember(MODULE_NAME).getValue());
settings.setModuleDescription(config.getStringMemberOrDefault(
Expand Down Expand Up @@ -121,6 +135,15 @@ public void setModuleName(String moduleName) {
this.moduleName = Objects.requireNonNull(moduleName);
}

/**
* Sets the name of the module to generate.
*
* @param moduleName The name of the module to generate.
*/
public void setModule(String moduleName) {
this.moduleName = Objects.requireNonNull(moduleName);
}

/**
* Gets the optional module description for the module that will be generated.
*
Expand Down Expand Up @@ -241,4 +264,14 @@ public ShapeId resolveServiceProtocol(
public void setProtocol(ShapeId protocol) {
this.protocol = Objects.requireNonNull(protocol);
}

@SmithyInternalApi
public ArtifactType getArtifactType() {
return artifactType;
}

@SmithyInternalApi
public void setArtifactType(ArtifactType artifactType) {
this.artifactType = artifactType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,39 @@ public static final class Context {
public static final Symbol Background = SmithyGoDependency.CONTEXT.valueSymbol("Background");
}

public static final class Time {
public static final Symbol Time = SmithyGoDependency.TIME.pointableSymbol("Time");
}

public static final class Encoding {
public static final class Json {
public static final Symbol NewDecoder = SmithyGoDependency.JSON.valueSymbol("NewDecoder");
public static final Symbol Number = SmithyGoDependency.JSON.valueSymbol("Number");
}

public static final class Base64 {
public static final Symbol StdEncoding = SmithyGoDependency.BASE64.valueSymbol("StdEncoding");
}
}

public static final class Crypto {
public static final class Rand {
public static final Symbol Reader = SmithyGoDependency.CRYPTORAND.valueSymbol("Reader");
}
}

public static final class Fmt {
public static final Symbol Errorf = SmithyGoDependency.FMT.valueSymbol("Errorf");
public static final Symbol Sprintf = SmithyGoDependency.FMT.valueSymbol("Sprintf");
}

public static final class Net {
public static final class Http {
public static final Symbol Request = SmithyGoDependency.NET_HTTP.pointableSymbol("Request");
public static final Symbol Response = SmithyGoDependency.NET_HTTP.pointableSymbol("Response");
public static final Symbol Server = SmithyGoDependency.NET_HTTP.pointableSymbol("Server");
public static final Symbol Handler = SmithyGoDependency.NET_HTTP.valueSymbol("Handler");
public static final Symbol ResponseWriter = SmithyGoDependency.NET_HTTP.valueSymbol("ResponseWriter");
}
}

Expand Down
Loading

0 comments on commit 5893827

Please sign in to comment.