Skip to content

Commit

Permalink
feat: Adds local service generators
Browse files Browse the repository at this point in the history
  • Loading branch information
Shubham Chaturvedi committed Nov 4, 2024
1 parent e72cf37 commit 0800e40
Show file tree
Hide file tree
Showing 5 changed files with 2,671 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package software.amazon.polymorph.smithygo.localservice;

import software.amazon.polymorph.smithygo.utils.GoCodegenUtils;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.traits.RequiredTrait;

public class DafnyGoPointableIndex {

public static boolean isNillable(final Model model, final Shape shape) {
return (
!shape.hasTrait(RequiredTrait.class) &&
!GoCodegenUtils.isOperationStruct(model, shape)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package software.amazon.polymorph.smithygo.localservice;

import java.util.Map;
import software.amazon.polymorph.smithygo.codegen.GenerationContext;
import software.amazon.polymorph.smithygo.codegen.GoSettings;
import software.amazon.polymorph.smithygo.codegen.GoWriter;
import software.amazon.polymorph.smithygo.codegen.integration.GoIntegration;
import software.amazon.polymorph.smithygo.localservice.nameresolver.SmithyNameResolver;
import software.amazon.smithy.build.PluginContext;
import software.amazon.smithy.build.SmithyBuildPlugin;
import software.amazon.smithy.codegen.core.directed.CodegenDirector;

public class DafnyLocalServiceCodegenPlugin implements SmithyBuildPlugin {

public DafnyLocalServiceCodegenPlugin(
final Map<String, String> smithyNamespaceToPythonModuleNameMap
) {
super();
SmithyNameResolver.setSmithyNamespaceToGoModuleNameMap(
smithyNamespaceToPythonModuleNameMap
);
}

public void run(PluginContext context) {
CodegenDirector<
GoWriter,
GoIntegration,
GenerationContext,
GoSettings
> runner = new CodegenDirector<>();

runner.directedCodegen(new DafnyLocalServiceDirectedCodegen());

// Set the SmithyIntegration class to look for and apply using SPI.
runner.integrationClass(GoIntegration.class);

// Set the FileManifest and Model from the plugin.
runner.fileManifest(context.getFileManifest());
runner.model(context.getModel());

// Create the GoSettings object from the plugin settings.
GoSettings settings = GoSettings.from(context.getSettings());
runner.settings(settings);

runner.service(settings.getService());

// Configure the director to perform some common model transforms.
runner.performDefaultCodegenTransforms();

// TODO: Not using below because it would break existing AWS SDKs. Maybe it should be configurable
// so generic SDKs call this by default, but AWS SDKs can opt-out of it via a setting.
// runner.createDedicatedInputsAndOutputs();

// Run it!
runner.run();
}

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

@Override
public void execute(PluginContext context) {
this.run(context);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
package software.amazon.polymorph.smithygo.localservice;

import java.util.logging.Logger;
import software.amazon.polymorph.smithygo.codegen.EnumGenerator;
import software.amazon.polymorph.smithygo.codegen.GenerationContext;
import software.amazon.polymorph.smithygo.codegen.GoDelegator;
import software.amazon.polymorph.smithygo.codegen.GoSettings;
import software.amazon.polymorph.smithygo.codegen.IntEnumGenerator;
import software.amazon.polymorph.smithygo.codegen.StructureGenerator;
import software.amazon.polymorph.smithygo.codegen.SymbolVisitor;
import software.amazon.polymorph.smithygo.codegen.integration.GoIntegration;
import software.amazon.smithy.codegen.core.SymbolProvider;
import software.amazon.smithy.codegen.core.directed.CreateContextDirective;
import software.amazon.smithy.codegen.core.directed.CreateSymbolProviderDirective;
import software.amazon.smithy.codegen.core.directed.DirectedCodegen;
import software.amazon.smithy.codegen.core.directed.GenerateEnumDirective;
import software.amazon.smithy.codegen.core.directed.GenerateErrorDirective;
import software.amazon.smithy.codegen.core.directed.GenerateIntEnumDirective;
import software.amazon.smithy.codegen.core.directed.GenerateResourceDirective;
import software.amazon.smithy.codegen.core.directed.GenerateServiceDirective;
import software.amazon.smithy.codegen.core.directed.GenerateStructureDirective;
import software.amazon.smithy.codegen.core.directed.GenerateUnionDirective;

public class DafnyLocalServiceDirectedCodegen
implements DirectedCodegen<GenerationContext, GoSettings, GoIntegration> {

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

@Override
public SymbolProvider createSymbolProvider(
CreateSymbolProviderDirective<GoSettings> directive
) {
return new SymbolVisitor(directive.model(), directive.settings());
}

@Override
public GenerationContext createContext(
CreateContextDirective<GoSettings, GoIntegration> directive
) {
return GenerationContext
.builder()
.model(directive.model())
.settings(directive.settings())
.symbolProvider(directive.symbolProvider())
.fileManifest(directive.fileManifest())
.integrations(directive.integrations())
.writerDelegator(
new GoDelegator(directive.fileManifest(), directive.symbolProvider())
)
.protocolGenerator(new DafnyLocalServiceTypeConversionProtocol())
.build();
}

@Override
public void generateService(
GenerateServiceDirective<GenerationContext, GoSettings> directive
) {
if (
!directive
.shape()
.getId()
.getNamespace()
.equals(directive.context().settings().getService().getNamespace())
) {
return;
}
new DafnyLocalServiceGenerator(directive.context(), directive.service())
.run();

var protocolGenerator = directive.context().protocolGenerator();
if (protocolGenerator == null) {
return;
}

protocolGenerator.generateSerializers(directive.context());

protocolGenerator.generateDeserializers(directive.context());
}

@Override
public void generateStructure(
GenerateStructureDirective<GenerationContext, GoSettings> directive
) {
if (
!directive
.shape()
.getId()
.getNamespace()
.equals(directive.context().settings().getService().getNamespace())
) {
return;
}
directive
.context()
.writerDelegator()
.useShapeWriter(
directive.shape(),
writer -> {
StructureGenerator generator = new StructureGenerator(
directive.context(),
writer,
directive.shape()
);
generator.run();
}
);
}

@Override
public void generateError(
GenerateErrorDirective<GenerationContext, GoSettings> directive
) {
if (
!directive
.shape()
.getId()
.getNamespace()
.equals(directive.context().settings().getService().getNamespace())
) {
return;
}
directive
.context()
.writerDelegator()
.useShapeWriter(
directive.shape(),
writer -> {
StructureGenerator generator = new StructureGenerator(
directive.context(),
writer,
directive.shape()
);
generator.run();
}
);
}

@Override
public void generateUnion(
GenerateUnionDirective<GenerationContext, GoSettings> directive
) {}

@Override
public void generateEnumShape(
GenerateEnumDirective<GenerationContext, GoSettings> directive
) {
if (
!directive
.shape()
.getId()
.getNamespace()
.equals(directive.context().settings().getService().getNamespace())
) {
return;
}
directive
.context()
.writerDelegator()
.useShapeWriter(
directive.shape(),
writer -> {
EnumGenerator enumGenerator = new EnumGenerator(
directive.symbolProvider(),
writer,
directive.shape()
);
enumGenerator.run();
}
);
}

@Override
public void generateIntEnumShape(
GenerateIntEnumDirective<GenerationContext, GoSettings> directive
) {
if (
!directive
.shape()
.getId()
.getNamespace()
.equals(directive.context().settings().getService().getNamespace())
) {
return;
}
directive
.context()
.writerDelegator()
.useShapeWriter(
directive.shape(),
writer -> {
IntEnumGenerator intEnumGenerator = new IntEnumGenerator(
directive.symbolProvider(),
writer,
directive.shape().asIntEnumShape().get()
);
intEnumGenerator.run();
}
);
}

@Override
public void generateResource(
GenerateResourceDirective<GenerationContext, GoSettings> directive
) {
// System.out.println("##############" + directive.shape());
// directive.context().writerDelegator().useShapeWriter(directive.shape(), writer -> {
// });
}
}
Loading

0 comments on commit 0800e40

Please sign in to comment.