Skip to content

Commit

Permalink
Setup Go Server Codegen plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Yuan committed Jan 29, 2024
1 parent fd6b7b4 commit 154fbae
Show file tree
Hide file tree
Showing 24 changed files with 726 additions and 71 deletions.
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
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> {
@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() {
return ArtifactType.CLIENT;
}

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

0 comments on commit 154fbae

Please sign in to comment.