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

Make SmithyIntegrations available from CodegenContext #1237

Merged
merged 2 commits into from
Jun 24, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

package software.amazon.smithy.codegen.core;

import java.util.List;
import software.amazon.smithy.build.FileManifest;
import software.amazon.smithy.model.Model;

Expand All @@ -24,8 +25,9 @@
*
* @param <S> The settings object used to configure the generator.
* @param <W> The type of {@link SymbolWriter} used by the generator.
* @param <I> The type of {@link SmithyIntegration}s used by the generator.
*/
public interface CodegenContext<S, W extends SymbolWriter<W, ?>> {
public interface CodegenContext<S, W extends SymbolWriter<W, ?>, I extends SmithyIntegration<S, W, ?>> {
/**
* @return Gets the model being code generated.
*/
Expand Down Expand Up @@ -57,4 +59,9 @@ public interface CodegenContext<S, W extends SymbolWriter<W, ?>> {
* @return Returns the writer delegator used by the generator.
*/
WriterDelegator<W> writerDelegator();

/**
* @return Gets the SmithyIntegrations used for code generation.
*/
List<I> integrations();
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
* @param <C> The CodegenContext value used by the generator.
*/
@SmithyUnstableApi
public interface SmithyIntegration<S, W extends SymbolWriter<W, ?>, C extends CodegenContext<S, W>> {
public interface SmithyIntegration<S, W extends SymbolWriter<W, ?>, C extends CodegenContext<S, W, ?>> {
/**
* Gets the name of the integration.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
public final class CodegenDirector<
W extends SymbolWriter<W, ? extends ImportContainer>,
I extends SmithyIntegration<S, W, C>,
C extends CodegenContext<S, W>,
C extends CodegenContext<S, W, I>,
S> {

private static final Logger LOGGER = Logger.getLogger(DirectedCodegen.class.getName());
Expand All @@ -71,7 +71,7 @@ public final class CodegenDirector<
private S settings;
private FileManifest fileManifest;
private Supplier<Iterable<I>> integrationFinder;
private DirectedCodegen<C, S> directedCodegen;
private DirectedCodegen<C, S, I> directedCodegen;
private final List<BiFunction<Model, ModelTransformer, Model>> transforms = new ArrayList<>();

/**
Expand Down Expand Up @@ -124,7 +124,7 @@ public void service(ShapeId service) {
*
* @param directedCodegen Directed code generator to run.
*/
public void directedCodegen(DirectedCodegen<C, S> directedCodegen) {
public void directedCodegen(DirectedCodegen<C, S, I> directedCodegen) {
this.directedCodegen = directedCodegen;
}

Expand Down Expand Up @@ -269,7 +269,7 @@ public void run() {

SymbolProvider provider = createSymbolProvider(integrations, serviceShape);

C context = createContext(serviceShape, provider);
C context = createContext(serviceShape, provider, integrations);

// After the context is created, it holds the model that should be used for rest of codegen. So `model` should
// not really be used directly after this point in the flow. Setting the `model` to `context.model()` to avoid
Expand Down Expand Up @@ -356,10 +356,10 @@ private SymbolProvider createSymbolProvider(List<I> integrations, ServiceShape s
return SymbolProvider.cache(provider);
}

private C createContext(ServiceShape serviceShape, SymbolProvider provider) {
private C createContext(ServiceShape serviceShape, SymbolProvider provider, List<I> integrations) {
LOGGER.fine(() -> "Creating a codegen context for " + directedCodegen.getClass().getName());
return directedCodegen.createContext(new CreateContextDirective<>(
model, settings, serviceShape, provider, fileManifest));
model, settings, serviceShape, provider, fileManifest, integrations));
}

private void registerInterceptors(C context, List<I> integrations) {
Expand Down Expand Up @@ -399,14 +399,14 @@ private void applyIntegrationCustomizations(C context, List<I> integrations) {

private static class ShapeGenerator<
W extends SymbolWriter<W, ? extends ImportContainer>,
C extends CodegenContext<S, W>,
C extends CodegenContext<S, W, ?>,
S> extends ShapeVisitor.Default<Void> {

private final C context;
private final ServiceShape serviceShape;
private final DirectedCodegen<C, S> directedCodegen;
private final DirectedCodegen<C, S, ?> directedCodegen;

ShapeGenerator(C context, ServiceShape serviceShape, DirectedCodegen<C, S> directedCodegen) {
ShapeGenerator(C context, ServiceShape serviceShape, DirectedCodegen<C, S, ?> directedCodegen) {
this.context = context;
this.serviceShape = serviceShape;
this.directedCodegen = directedCodegen;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* @param <C> CodegenContext type.
* @param <S> Codegen settings type.
*/
public abstract class ContextualDirective<C extends CodegenContext<S, ?>, S> extends Directive<S> {
public abstract class ContextualDirective<C extends CodegenContext<S, ?, ?>, S> extends Directive<S> {

private final C context;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@

package software.amazon.smithy.codegen.core.directed;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import software.amazon.smithy.build.FileManifest;
import software.amazon.smithy.codegen.core.CodegenContext;
import software.amazon.smithy.codegen.core.SmithyIntegration;
import software.amazon.smithy.codegen.core.SymbolProvider;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.knowledge.ServiceIndex;
Expand All @@ -29,23 +32,28 @@
* Directive used to create a {@link CodegenContext}.
*
* @param <S> Codegen settings type.
* @param <I> {@link SmithyIntegration} type.

* @see DirectedCodegen#createContext
*/
public final class CreateContextDirective<S> extends Directive<S> {
public final class CreateContextDirective<S, I extends SmithyIntegration<S, ?, ?>> extends Directive<S> {

private final SymbolProvider symbolProvider;
private final FileManifest fileManifest;
private final List<I> integrations;

CreateContextDirective(
Model model,
S settings,
ServiceShape service,
SymbolProvider symbolProvider,
FileManifest fileManifest
FileManifest fileManifest,
List<I> integrations
) {
super(model, settings, service);
this.symbolProvider = symbolProvider;
this.fileManifest = fileManifest;
this.integrations = Collections.unmodifiableList(integrations);
}

/**
Expand All @@ -62,6 +70,14 @@ public FileManifest fileManifest() {
return fileManifest;
}


/**
* @return Returns the list of Integrations used during codegen.
*/
public List<I> integrations() {
return integrations;
}

/**
* Get a map of supported protocols on the service shape in the form of shape ID to
* the definition of the trait.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* @see DirectedCodegen#customizeBeforeIntegrations
* @see DirectedCodegen#customizeAfterIntegrations
*/
public final class CustomizeDirective<C extends CodegenContext<S, ?>, S> extends ContextualDirective<C, S> {
public final class CustomizeDirective<C extends CodegenContext<S, ?, ?>, S> extends ContextualDirective<C, S> {
CustomizeDirective(C context, ServiceShape service) {
super(context, service);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@
*
* @param <C> Smithy {@link CodegenContext} to use in directed methods.
* @param <S> Settings object passed to directed methods as part of the context.
* @param <I> {@link SmithyIntegration} type to use in directed methods.
*/
public interface DirectedCodegen<C extends CodegenContext<S, ?>, S> {
public interface DirectedCodegen<C extends CodegenContext<S, ?, I>, S, I extends SmithyIntegration<S, ?, C>> {

/**
* Create the {@link SymbolProvider} used to map shapes to code symbols.
Expand All @@ -47,7 +48,7 @@ public interface DirectedCodegen<C extends CodegenContext<S, ?>, S> {
* @param directive Directive context data.
* @return Returns the created context object used by the rest of the directed generation.
*/
C createContext(CreateContextDirective<S> directive);
C createContext(CreateContextDirective<S, I> directive);

/**
* Generates the code needed for a service shape.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* @param <S> Codegen settings type.
* @see DirectedCodegen#generateEnumShape
*/
public final class GenerateEnumDirective<C extends CodegenContext<S, ?>, S> extends ShapeDirective<Shape, C, S> {
public final class GenerateEnumDirective<C extends CodegenContext<S, ?, ?>, S> extends ShapeDirective<Shape, C, S> {

GenerateEnumDirective(C context, ServiceShape service, Shape shape) {
super(context, service, validateShape(shape));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* @param <S> Codegen settings type.
* @see DirectedCodegen#generateError
*/
public final class GenerateErrorDirective<C extends CodegenContext<S, ?>, S>
public final class GenerateErrorDirective<C extends CodegenContext<S, ?, ?>, S>
extends ShapeDirective<StructureShape, C, S> {

GenerateErrorDirective(C context, ServiceShape service, StructureShape shape) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* @param <S> Codegen settings type.
* @see DirectedCodegen#generateResource
*/
public final class GenerateResourceDirective<C extends CodegenContext<S, ?>, S>
public final class GenerateResourceDirective<C extends CodegenContext<S, ?, ?>, S>
extends ShapeDirective<ResourceShape, C, S> {
GenerateResourceDirective(C context, ServiceShape service, ResourceShape shape) {
super(context, service, shape);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
* @param <S> Codegen settings type.
* @see DirectedCodegen#generateService
*/
public final class GenerateServiceDirective<C extends CodegenContext<S, ?>, S>
public final class GenerateServiceDirective<C extends CodegenContext<S, ?, ?>, S>
extends ShapeDirective<ServiceShape, C, S> {

GenerateServiceDirective(C context, ServiceShape service) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* @param <S> Codegen settings type.
* @see DirectedCodegen#generateStructure
*/
public final class GenerateStructureDirective<C extends CodegenContext<S, ?>, S>
public final class GenerateStructureDirective<C extends CodegenContext<S, ?, ?>, S>
extends ShapeDirective<StructureShape, C, S> {

GenerateStructureDirective(C context, ServiceShape service, StructureShape shape) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* @param <S> Codegen settings type.
* @see DirectedCodegen#generateUnion
*/
public final class GenerateUnionDirective<C extends CodegenContext<S, ?>, S>
public final class GenerateUnionDirective<C extends CodegenContext<S, ?, ?>, S>
extends ShapeDirective<UnionShape, C, S> {

GenerateUnionDirective(C context, ServiceShape service, UnionShape shape) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* @param <C> CodegenContext type.
* @param <S> Codegen settings type.
*/
public abstract class ShapeDirective<T extends Shape, C extends CodegenContext<S, ?>, S>
public abstract class ShapeDirective<T extends Shape, C extends CodegenContext<S, ?, ?>, S>
extends ContextualDirective<C, S> {

private final T shape;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ private static final class MySettings {}
private static final class MyIntegration implements SmithyIntegration<
MySettings,
MySimpleWriter,
CodegenContext<MySettings, MySimpleWriter>
CodegenContext<MySettings, MySimpleWriter, MyIntegration>
> {
private final String name;
private final byte priority;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class SmithyIntegrationTest {

private static final class MySettings {}

private static final class MyContext implements CodegenContext<MySettings, MySimpleWriter> {
private static final class MyContext implements CodegenContext<MySettings, MySimpleWriter, MyIntegration> {
@Override
public Model model() {
return null;
Expand All @@ -54,6 +54,9 @@ public FileManifest fileManifest() {
public WriterDelegator<MySimpleWriter> writerDelegator() {
return null;
}

@Override
public List<MyIntegration> integrations() { return null; }
}

private static final class MyIntegration implements SmithyIntegration<MySettings, MySimpleWriter, MyContext> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,17 @@
import org.junit.jupiter.api.Test;
import software.amazon.smithy.build.FileManifest;
import software.amazon.smithy.build.MockManifest;
import software.amazon.smithy.codegen.core.SmithyIntegration;
import software.amazon.smithy.codegen.core.Symbol;
import software.amazon.smithy.codegen.core.SymbolProvider;
import software.amazon.smithy.codegen.core.WriterDelegator;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.node.ExpectationNotMetException;
import software.amazon.smithy.model.node.Node;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.shapes.ShapeId;

public class CodegenDirectorTest {

interface TestIntegration extends SmithyIntegration<TestSettings, TestWriter, TestContext> {}

private static final class TestDirected implements DirectedCodegen<TestContext, TestSettings> {
private static final class TestDirected implements DirectedCodegen<TestContext, TestSettings, TestIntegration> {
public final List<ShapeId> generatedShapes = new ArrayList<>();

@Override
Expand All @@ -50,7 +46,7 @@ public SymbolProvider createSymbolProvider(CreateSymbolProviderDirective<TestSet
}

@Override
public TestContext createContext(CreateContextDirective<TestSettings> directive) {
public TestContext createContext(CreateContextDirective<TestSettings, TestIntegration> directive) {
WriterDelegator<TestWriter> delegator = new WriterDelegator<>(
directive.fileManifest(),
directive.symbolProvider(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

package software.amazon.smithy.codegen.core.directed;

import java.util.ArrayList;
import java.util.List;
import software.amazon.smithy.build.FileManifest;
import software.amazon.smithy.build.MockManifest;
import software.amazon.smithy.codegen.core.CodegenContext;
Expand All @@ -25,7 +27,7 @@
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.model.shapes.ShapeId;

final class TestContext implements CodegenContext<TestSettings, TestWriter> {
final class TestContext implements CodegenContext<TestSettings, TestWriter, TestIntegration> {

private final Model model;
private final TestSettings settings;
Expand Down Expand Up @@ -86,6 +88,11 @@ public WriterDelegator<TestWriter> writerDelegator() {
return delegator;
}

@Override
public List<TestIntegration> integrations() {
return new ArrayList<>();
}

public ServiceShape service() {
return service;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package software.amazon.smithy.codegen.core.directed;

import software.amazon.smithy.codegen.core.SmithyIntegration;

interface TestIntegration extends SmithyIntegration<TestSettings, TestWriter, TestContext> {

}