Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Yuan committed Jan 29, 2024
1 parent a7f46b9 commit 7d82b5a
Show file tree
Hide file tree
Showing 22 changed files with 582 additions and 22 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/service/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 Down Expand Up @@ -79,8 +80,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
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.SmithyUnstableApi;

/**
* Manages writers for Go files.Based off of GoWriterDelegator adding support
* for getting shape specific GoWriters.
*/
@SmithyUnstableApi
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,6 +25,7 @@
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}.
Expand All @@ -45,6 +46,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 +61,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 @@ -241,4 +254,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 @@ -788,7 +788,7 @@ boolean writeShapeDocs(Shape shape) {
* @param shape Shape to write the documentation of.
* @return Returns true if docs were written.
*/
boolean writePackageShapeDocs(Shape shape) {
public boolean writePackageShapeDocs(Shape shape) {
return shape.getTrait(DocumentationTrait.class)
.map(DocumentationTrait::getValue)
.map(docs -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,21 @@
import software.amazon.smithy.model.shapes.MemberShape;
import software.amazon.smithy.model.traits.DocumentationTrait;
import software.amazon.smithy.model.traits.EnumValueTrait;
import software.amazon.smithy.utils.SmithyInternalApi;
import software.amazon.smithy.utils.StringUtils;

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

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

IntEnumGenerator(SymbolProvider symbolProvider, GoWriter writer, IntEnumShape shape) {
public IntEnumGenerator(SymbolProvider symbolProvider, GoWriter writer, IntEnumShape shape) {
this.symbolProvider = symbolProvider;
this.writer = writer;
this.shape = shape;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@
import software.amazon.smithy.model.node.StringNode;
import software.amazon.smithy.model.traits.UnstableTrait;
import software.amazon.smithy.utils.SmithyBuilder;
import software.amazon.smithy.utils.SmithyInternalApi;

/**
* Generates a manifest description of the generated code, minimum go version,
* and minimum dependencies required.
*/
@SmithyInternalApi
public final class ManifestWriter {

private static final Logger LOGGER = Logger.getLogger(ManifestWriter.class.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@
import software.amazon.smithy.model.traits.StreamingTrait;
import software.amazon.smithy.utils.MapUtils;
import software.amazon.smithy.utils.SetUtils;
import software.amazon.smithy.utils.SmithyInternalApi;

/**
* Renders structures.
*/
final class StructureGenerator implements Runnable {
@SmithyInternalApi
public final class StructureGenerator implements Runnable {
private static final Map<String, String> STANDARD_ERROR_MEMBERS = MapUtils.of(
"ErrorCode", "string",
"ErrorMessage", "string",
Expand All @@ -48,7 +50,7 @@ final class StructureGenerator implements Runnable {
private final ServiceShape service;
private final ProtocolGenerator protocolGenerator;

StructureGenerator(
public StructureGenerator(
Model model,
SymbolProvider symbolProvider,
GoWriter writer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import software.amazon.smithy.model.traits.EnumTrait;
import software.amazon.smithy.model.traits.ErrorTrait;
import software.amazon.smithy.model.traits.StreamingTrait;
import software.amazon.smithy.utils.SmithyInternalApi;
import software.amazon.smithy.utils.StringUtils;

/**
Expand All @@ -70,7 +71,8 @@
* <p>Reserved words for Go are automatically escaped so that they are
* suffixed with "_". See "reserved-words.txt" for the list of words.
*/
final class SymbolVisitor implements SymbolProvider, ShapeVisitor<Symbol> {
@SmithyInternalApi
public final class SymbolVisitor implements SymbolProvider, ShapeVisitor<Symbol> {
private static final Logger LOGGER = Logger.getLogger(SymbolVisitor.class.getName());

private final Model model;
Expand All @@ -82,7 +84,7 @@ final class SymbolVisitor implements SymbolProvider, ShapeVisitor<Symbol> {
private final GoPointableIndex pointableIndex;
private final GoSettings settings;

SymbolVisitor(Model model, GoSettings settings) {
public SymbolVisitor(Model model, GoSettings settings) {
this.model = model;
this.settings = settings;
this.rootModuleName = settings.getModuleName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@
import software.amazon.smithy.model.shapes.UnionShape;
import software.amazon.smithy.model.traits.ErrorTrait;
import software.amazon.smithy.model.traits.StreamingTrait;
import software.amazon.smithy.utils.SmithyInternalApi;

/**
* Renders unions and type aliases for all their members.
*/
@SmithyInternalApi
public class UnionGenerator {
public static final String UNKNOWN_MEMBER_NAME = "UnknownUnionMember";

Expand All @@ -41,7 +43,7 @@ public class UnionGenerator {
private final UnionShape shape;
private final boolean isEventStream;

UnionGenerator(Model model, SymbolProvider symbolProvider, UnionShape shape) {
public UnionGenerator(Model model, SymbolProvider symbolProvider, UnionShape shape) {
this.model = model;
this.symbolProvider = symbolProvider;
this.shape = shape;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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.GoSettings.ArtifactType;
import software.amazon.smithy.go.codegen.GoWriter;
import software.amazon.smithy.go.codegen.TriConsumer;
import software.amazon.smithy.model.Model;
Expand Down Expand Up @@ -49,6 +50,10 @@ default byte getOrder() {
return 0;
}

default ArtifactType getArtifactType() {
return ArtifactType.CLIENT;
}

/**
* Preprocess the model before code generation.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* 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.logging.Logger;
import software.amazon.smithy.build.PluginContext;
import software.amazon.smithy.build.SmithyBuildPlugin;
import software.amazon.smithy.codegen.core.SymbolProvider;
import software.amazon.smithy.go.codegen.GoSettings;
import software.amazon.smithy.go.codegen.GoSettings.ArtifactType;
import software.amazon.smithy.go.codegen.SymbolVisitor;
import software.amazon.smithy.model.Model;

/**
* Plugin to trigger Go code generation.
*/
public final class GoServerCodegenPlugin implements SmithyBuildPlugin {
private static final Logger LOGGER = Logger.getLogger(GoServerCodegenPlugin.class.getName());

@Override
public String getName() {
return "go-server-codegen";
}

@Override
public void execute(PluginContext context) {
String onlyBuild = System.getenv("SMITHY_GO_BUILD_API");
if (onlyBuild != null && !onlyBuild.isEmpty()) {
String targetServiceId =
GoSettings.from(context.getSettings(), ArtifactType.SERVER).getService().toString();

boolean found = false;
for (String includeServiceId : onlyBuild.split(",")) {
if (targetServiceId.startsWith(includeServiceId)) {
found = true;
break;
}
}
if (!found) {
LOGGER.info("skipping " + targetServiceId);
return;
}
}

new ServerCodegenVisitor(context).execute();
}

/**
* Creates a Go symbol provider.
*
* @param model The model to generate symbols for.
* @param settings The Gosettings to use to create symbol provider
* @return Returns the created provider.
*/
public static SymbolProvider createSymbolProvider(Model model, GoSettings settings) {
return new SymbolVisitor(model, settings);
}
}
Loading

0 comments on commit 7d82b5a

Please sign in to comment.