Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup Go Server codegen #494

Merged
merged 2 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any kind of future refactoring that removes the need to add these kinds of conditionals inside of codegen?

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.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 @@ -241,4 +255,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 @@ -47,6 +47,7 @@
import software.amazon.smithy.model.traits.StringTrait;
import software.amazon.smithy.utils.AbstractCodeWriter;
import software.amazon.smithy.utils.ListUtils;
import software.amazon.smithy.utils.SmithyInternalApi;
import software.amazon.smithy.utils.StringUtils;

/**
Expand All @@ -56,6 +57,7 @@
*
* <p>Use the {@code $P} formatter to refer to {@link Symbol}s using pointers where appropriate.
*/
@SmithyInternalApi
public final class GoWriter extends AbstractCodeWriter<GoWriter> {

private static final Logger LOGGER = Logger.getLogger(GoWriter.class.getName());
Expand Down Expand Up @@ -773,7 +775,8 @@ public GoWriter writeRawPackageDocs(String docs) {
* @param shape Shape to write the documentation of.
* @return Returns true if docs were written.
*/
boolean writeShapeDocs(Shape shape) {
@SmithyInternalApi
public boolean writeShapeDocs(Shape shape) {
return shape.getTrait(DocumentationTrait.class)
.map(DocumentationTrait::getValue)
.map(docs -> {
Expand All @@ -788,7 +791,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> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit - is this really a SymbolVisitor or a ShapeVisitor?

@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,17 +21,20 @@
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;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.utils.SmithyUnstableApi;

/**
* Java SPI for customizing Go code generation, registering
* new protocol code generators, renaming shapes, modifying the model,
* adding custom code, etc.
*/
@SmithyUnstableApi
public interface GoIntegration {
/**
* Gets the sort order of the customization from -128 to 127.
Expand All @@ -49,6 +52,10 @@ default byte getOrder() {
return 0;
}

default ArtifactType getArtifactType() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible for a plugin to work for either artifact type? What's this used for?

return ArtifactType.CLIENT;
}

/**
* Preprocess the model before code generation.
*
Expand Down
Loading
Loading