Skip to content

Commit 7be3985

Browse files
Move symbol visitor factory to artifact enum
1 parent 91c6b12 commit 7be3985

File tree

4 files changed

+27
-39
lines changed

4 files changed

+27
-39
lines changed

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CodegenVisitor.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import java.util.ServiceLoader;
2626
import java.util.Set;
2727
import java.util.TreeSet;
28-
import java.util.function.BiFunction;
2928
import java.util.logging.Logger;
3029
import software.amazon.smithy.build.FileManifest;
3130
import software.amazon.smithy.build.PluginContext;
@@ -46,6 +45,7 @@
4645
import software.amazon.smithy.model.shapes.UnionShape;
4746
import software.amazon.smithy.model.traits.EnumTrait;
4847
import software.amazon.smithy.model.traits.PaginatedTrait;
48+
import software.amazon.smithy.typescript.codegen.TypeScriptSettings.ArtifactType;
4949
import software.amazon.smithy.typescript.codegen.integration.ProtocolGenerator;
5050
import software.amazon.smithy.typescript.codegen.integration.RuntimeClientPlugin;
5151
import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration;
@@ -78,11 +78,7 @@ class CodegenVisitor extends ShapeVisitor.Default<Void> {
7878
private final ProtocolGenerator protocolGenerator;
7979
private final ApplicationProtocol applicationProtocol;
8080

81-
CodegenVisitor(
82-
PluginContext context,
83-
BiFunction<Model, TypeScriptSettings, SymbolProvider> createSymbolProvider,
84-
TypeScriptSettings.ArtifactType artifactType
85-
) {
81+
CodegenVisitor(PluginContext context, ArtifactType artifactType) {
8682
// Load all integrations.
8783
ClassLoader loader = context.getPluginClassLoader().orElse(getClass().getClassLoader());
8884
LOGGER.info("Attempting to discover TypeScriptIntegration from the classpath...");
@@ -117,7 +113,7 @@ class CodegenVisitor extends ShapeVisitor.Default<Void> {
117113
settings.generateClient() ? "client" : "server", service.getId()));
118114

119115
// Decorate the symbol provider using integrations.
120-
SymbolProvider resolvedProvider = createSymbolProvider.apply(model, settings);
116+
SymbolProvider resolvedProvider = artifactType.createSymbolProvider(model, settings);
121117
for (TypeScriptIntegration integration : integrations) {
122118
resolvedProvider = integration.decorateSymbolProvider(settings, model, resolvedProvider);
123119
}

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptCodegenPlugin.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,10 @@ public String getName() {
3333

3434
@Override
3535
public void execute(PluginContext context) {
36-
new CodegenVisitor(
37-
context,
38-
TypeScriptCodegenPlugin::createSymbolProvider,
39-
ArtifactType.CLIENT
40-
).execute();
36+
new CodegenVisitor(context, ArtifactType.CLIENT).execute();
4137
}
4238

43-
/**
44-
* Creates a TypeScript symbol provider.
45-
*
46-
* @param model Model to generate symbols for.
47-
* @param settings Settings used by the plugin.
48-
* @return Returns the created provider.
49-
*/
39+
@Deprecated
5040
public static SymbolProvider createSymbolProvider(Model model, TypeScriptSettings settings) {
5141
return new SymbolVisitor(model, settings);
5242
}

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptServerCodegenPlugin.java

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717

1818
import software.amazon.smithy.build.PluginContext;
1919
import software.amazon.smithy.build.SmithyBuildPlugin;
20-
import software.amazon.smithy.codegen.core.SymbolProvider;
21-
import software.amazon.smithy.model.Model;
2220
import software.amazon.smithy.typescript.codegen.TypeScriptSettings.ArtifactType;
2321

2422
/**
@@ -33,21 +31,6 @@ public String getName() {
3331

3432
@Override
3533
public void execute(PluginContext context) {
36-
new CodegenVisitor(
37-
context,
38-
TypeScriptServerCodegenPlugin::createSymbolProvider,
39-
ArtifactType.SSDK
40-
).execute();
41-
}
42-
43-
/**
44-
* Creates a TypeScript symbol provider.
45-
*
46-
* @param model Model to generate symbols for.
47-
* @param settings Settings used by the plugin.
48-
* @return Returns the created provider.
49-
*/
50-
public static SymbolProvider createSymbolProvider(Model model, TypeScriptSettings settings) {
51-
return new ServerSymbolVisitor(model, new SymbolVisitor(model, settings));
34+
new CodegenVisitor(context, ArtifactType.SSDK).execute();
5235
}
5336
}

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptSettings.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
import java.util.List;
2020
import java.util.Objects;
2121
import java.util.Set;
22+
import java.util.function.BiFunction;
2223
import java.util.logging.Logger;
2324
import java.util.stream.Collectors;
2425
import software.amazon.smithy.codegen.core.CodegenException;
26+
import software.amazon.smithy.codegen.core.SymbolProvider;
2527
import software.amazon.smithy.model.Model;
2628
import software.amazon.smithy.model.knowledge.ServiceIndex;
2729
import software.amazon.smithy.model.node.BooleanNode;
@@ -317,7 +319,24 @@ public void setProtocol(ShapeId protocol) {
317319
* An enum indicating the type of artifact the code generator will produce.
318320
*/
319321
public enum ArtifactType {
320-
CLIENT,
321-
SSDK
322+
CLIENT(SymbolVisitor::new),
323+
SSDK((m, s) -> new ServerSymbolVisitor(m, new SymbolVisitor(m, s)));
324+
325+
private final BiFunction<Model, TypeScriptSettings, SymbolProvider> symbolProviderFactory;
326+
327+
ArtifactType(BiFunction<Model, TypeScriptSettings, SymbolProvider> symbolProviderFactory) {
328+
this.symbolProviderFactory = symbolProviderFactory;
329+
}
330+
331+
/**
332+
* Creates a TypeScript symbol provider suited to the artifact type.
333+
*
334+
* @param model Model to generate symbols for.
335+
* @param settings Settings used by the symbol provider.
336+
* @return Returns the created provider.
337+
*/
338+
public SymbolProvider createSymbolProvider(Model model, TypeScriptSettings settings) {
339+
return symbolProviderFactory.apply(model, settings);
340+
}
322341
}
323342
}

0 commit comments

Comments
 (0)