Skip to content

Commit

Permalink
Update codegen to use defaultExtensionConfiguration
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewFossAWS committed Aug 22, 2023
1 parent 8be96c8 commit b805825
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ public void customizeBeforeIntegrations(
}
}

new ClientConfigurationGenerator(directive.model(), directive.service(), directive.symbolProvider(),
new ExtensionConfigurationGenerator(directive.model(), directive.service(), directive.symbolProvider(),
directive.context().writerDelegator(), directive.context().integrations()).generate();

new RuntimeExtensionsGenerator(directive.model(), directive.service(), directive.symbolProvider(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration;

public class ClientConfigurationGenerator {
public class ExtensionConfigurationGenerator {

private static final String CLIENT_CONFIGURATION_TEMPLATE = "clientConfiguration.template";
private static final String FILENAME = "clientConfiguration.ts";
private static final String CLIENT_CONFIGURATION_TEMPLATE = "extensionConfiguration.template";
private static final String FILENAME = "extensionConfiguration.ts";

private final Model model;
private final ServiceShape service;
private final SymbolProvider symbolProvider;
private final TypeScriptDelegator delegator;
private final List<TypeScriptIntegration> integrations;

public ClientConfigurationGenerator(
public ExtensionConfigurationGenerator(
Model model,
ServiceShape service,
SymbolProvider symbolProvider,
Expand All @@ -53,18 +53,20 @@ void generate() {
Map<String, Dependency> interfaces = new HashMap<>();

for (TypeScriptIntegration integration : integrations) {
integration.getClientConfigurationInterfaces().forEach(configurationInterface -> {
interfaces.put(configurationInterface.name(),
configurationInterface.dependency());
integration.getExtensionConfigurationInterfaces().forEach(configurationInterface -> {
interfaces.put(configurationInterface.name().left,
configurationInterface.name().right);
});
}

String clientName = symbolProvider.toSymbol(service).getName();
String clientName = symbolProvider.toSymbol(service).getName()
.replace("Client", "")
.replace("client", "");

String clientConfigurationContent = TypeScriptUtils
.loadResourceAsString(CLIENT_CONFIGURATION_TEMPLATE)
.replace("${clientConfigName}", clientName + "Configuration")
.replace("${clientConfigInterfaces}", String.join(", ", interfaces.keySet()));
.replace("${extensionConfigName}", clientName + "ExtensionConfiguration")
.replace("${extensionConfigInterfaces}", String.join(", ", interfaces.keySet()));

delegator.useFileWriter(Paths.get(CodegenUtils.SOURCE_FOLDER, FILENAME).toString(), writer -> {
interfaces.entrySet().forEach(entry -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@

public class RuntimeExtensionsGenerator {

private static final String TEMPLATE_1 = "runtimeExtensions1.template";
private static final String TEMPLATE_2 = "runtimeExtensions2.template";
private static final String TEMPLATE_1 = "resolveRuntimeExtensions1.template";
private static final String TEMPLATE_2 = "resolveRuntimeExtensions2.template";
private static final String FILENAME = "runtimeExtensions.ts";

private final Model model;
Expand All @@ -48,33 +48,37 @@ public RuntimeExtensionsGenerator(
}

void generate() {
String clientName = symbolProvider.toSymbol(service).getName();
String clientName = symbolProvider.toSymbol(service).getName()
.replace("Client", "")
.replace("client", "");

String template1Contents = TypeScriptUtils.loadResourceAsString(TEMPLATE_1)
.replace("${clientConfigName}", clientName + "Configuration")
.replace("${extensionConfigName}", clientName + "ExtensionConfiguration")
.replace("$", "$$") // sanitize template place holders.
.replace("$${getPartialClientConfigurations}", "${L@getPartialClientConfigurations}");
.replace("$${getPartialExtensionConfigurations}", "${L@getPartialExtensionConfigurations}");

String template2Contents = TypeScriptUtils.loadResourceAsString(TEMPLATE_2)
.replace("$", "$$") // sanitize template place holders.
.replace("$${resolvePartialRuntimeConfigs}", "${L@resolvePartialRuntimeConfigs}");

delegator.useFileWriter(Paths.get(CodegenUtils.SOURCE_FOLDER, FILENAME).toString(), writer -> {
for (TypeScriptIntegration integration : integrations) {
integration.getClientConfigurationInterfaces().forEach(configurationInterface -> {
writer.addDependency(configurationInterface.dependency());
writer.addImport(configurationInterface.getClientConfigurationFn(), null,
configurationInterface.dependency());
writer.addImport(configurationInterface.resolveRuntimeConfigFn(), null,
configurationInterface.dependency());
integration.getExtensionConfigurationInterfaces().forEach(configurationInterface -> {
writer.addDependency(configurationInterface.getExtensionConfigurationFn().right);
writer.addDependency(configurationInterface.resolveRuntimeConfigFn().right);

writer.addImport(configurationInterface.getExtensionConfigurationFn().left, null,
configurationInterface.getExtensionConfigurationFn().right);
writer.addImport(configurationInterface.resolveRuntimeConfigFn().left, null,
configurationInterface.resolveRuntimeConfigFn().right);
});
}

writer.indent().onSection("getPartialClientConfigurations", original -> {
writer.indent().onSection("getPartialExtensionConfigurations", original -> {
for (TypeScriptIntegration integration : integrations) {
integration.getClientConfigurationInterfaces().forEach(configurationInterface -> {
integration.getExtensionConfigurationInterfaces().forEach(configurationInterface -> {
writer.indent(2).write("...asPartial($L(runtimeConfig)),",
configurationInterface.getClientConfigurationFn());
configurationInterface.getExtensionConfigurationFn().left);
writer.dedent(2);
});
}
Expand All @@ -83,9 +87,9 @@ void generate() {

writer.indent().onSection("resolvePartialRuntimeConfigs", original -> {
for (TypeScriptIntegration integration : integrations) {
integration.getClientConfigurationInterfaces().forEach(configurationInterface -> {
writer.indent(2).write("...$L(clientConfiguration),",
configurationInterface.resolveRuntimeConfigFn());
integration.getExtensionConfigurationInterfaces().forEach(configurationInterface -> {
writer.indent(2).write("...$L(extensionConfiguration),",
configurationInterface.resolveRuntimeConfigFn().left);
writer.dedent(2);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,24 @@

package software.amazon.smithy.typescript.codegen.extensions;

import software.amazon.smithy.typescript.codegen.Dependency;
import software.amazon.smithy.typescript.codegen.TypeScriptDependency;
import software.amazon.smithy.utils.Pair;

public class DefaultClientConfigurationInterface implements ClientConfigurationInterface {
@Override
public TypeScriptDependency dependency() {
return TypeScriptDependency.SMITHY_TYPES;
}
public class DefaultExtensionConfigurationInterface implements ExtensionConfigurationInterface {

@Override
public String name() {
return "DefaultClientConfiguration";
public Pair<String, Dependency> name() {
return Pair.of("DefaultExtensionConfiguration", TypeScriptDependency.SMITHY_TYPES);
}

@Override
public String getClientConfigurationFn() {
return "getDefaultClientConfiguration";
public Pair<String, Dependency> getExtensionConfigurationFn() {
return Pair.of("getDefaultExtensionConfiguration", TypeScriptDependency.AWS_SMITHY_CLIENT);
}

@Override
public String resolveRuntimeConfigFn() {
return "resolveDefaultRuntimeConfig";
public Pair<String, Dependency> resolveRuntimeConfigFn() {
return Pair.of("resolveDefaultRuntimeConfig", TypeScriptDependency.AWS_SMITHY_CLIENT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,20 @@
package software.amazon.smithy.typescript.codegen.extensions;

import software.amazon.smithy.typescript.codegen.Dependency;
import software.amazon.smithy.utils.Pair;
import software.amazon.smithy.utils.SmithyInternalApi;

/**
* An interface class for defining the service client configuration.
*/
@SmithyInternalApi
public interface ClientConfigurationInterface {
/**
* Define the dependency package where the interface and its related functions are defined in.
*
* @return Returns a dependency instance
*/
Dependency dependency();

public interface ExtensionConfigurationInterface {
/**
* Define the interface name.
*
* @return Returns an interface name
* @return Returns the interface name and the corresponding dependency package
*/
String name();
Pair<String, Dependency> name();

/**
* Define a function that returns an object instance that implements the interface.
Expand Down Expand Up @@ -69,7 +63,7 @@ public interface ClientConfigurationInterface {
*
* @return Returns a typescript function name
*/
String getClientConfigurationFn();
Pair<String, Dependency> getExtensionConfigurationFn();

/**
* Define a function that returns an object instance that implements the interface.
Expand All @@ -92,5 +86,5 @@ public interface ClientConfigurationInterface {
*
* @return Returns a typescript function name
*/
String resolveRuntimeConfigFn();
Pair<String, Dependency> resolveRuntimeConfigFn();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
import software.amazon.smithy.typescript.codegen.TypeScriptDependency;
import software.amazon.smithy.typescript.codegen.TypeScriptSettings;
import software.amazon.smithy.typescript.codegen.TypeScriptWriter;
import software.amazon.smithy.typescript.codegen.extensions.ClientConfigurationInterface;
import software.amazon.smithy.typescript.codegen.extensions.DefaultClientConfigurationInterface;
import software.amazon.smithy.typescript.codegen.extensions.DefaultExtensionConfigurationInterface;
import software.amazon.smithy.typescript.codegen.extensions.ExtensionConfigurationInterface;
import software.amazon.smithy.utils.MapUtils;
import software.amazon.smithy.utils.SmithyInternalApi;

Expand Down Expand Up @@ -137,7 +137,7 @@ public Map<String, Consumer<TypeScriptWriter>> getRuntimeConfigWriters(
}

@Override
public List<ClientConfigurationInterface> getClientConfigurationInterfaces() {
return List.of(new DefaultClientConfigurationInterface());
public List<ExtensionConfigurationInterface> getExtensionConfigurationInterfaces() {
return List.of(new DefaultExtensionConfigurationInterface());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import software.amazon.smithy.typescript.codegen.TypeScriptCodegenContext;
import software.amazon.smithy.typescript.codegen.TypeScriptSettings;
import software.amazon.smithy.typescript.codegen.TypeScriptWriter;
import software.amazon.smithy.typescript.codegen.extensions.ClientConfigurationInterface;
import software.amazon.smithy.typescript.codegen.extensions.ExtensionConfigurationInterface;
import software.amazon.smithy.utils.SmithyUnstableApi;

/**
Expand Down Expand Up @@ -236,7 +236,7 @@ default Map<String, Consumer<TypeScriptWriter>> getRuntimeConfigWriters(
*
* @return list of client configuration interface
*/
default List<ClientConfigurationInterface> getClientConfigurationInterfaces() {
default List<ExtensionConfigurationInterface> getExtensionConfigurationInterfaces() {
return Collections.emptyList();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* @internal
*/
export interface ${extensionConfigName} extends ${extensionConfigInterfaces} {}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ${clientConfigName}, ${clientConfigType} } from "./clientConfiguration";
import { ${extensionConfigName} } from "./extensionConfiguration";

/**
* @public
*/
export interface RuntimeExtension {
configureClient(clientConfiguration: ${clientConfigType}): void;
configure(clientConfiguration: ${extensionConfigName}): void;
}

/**
Expand All @@ -14,7 +14,7 @@ export interface RuntimeExtensionsConfig {
extensions: RuntimeExtension[]
}

const asPartial = <T extends Partial<${clientConfigName}>>(t: T) => t;
const asPartial = <T extends Partial<${extensionConfigName}>>(t: T) => t;

/**
* @internal
Expand All @@ -23,8 +23,8 @@ export const resolveRuntimeExtensions = (
runtimeConfig: any,
extensions: RuntimeExtension[]
) => {
const clientConfiguration: ${clientConfigName} = {
${getPartialClientConfigurations}
const extensionConfiguration: ${extensionConfigName} = {
${getPartialExtensionConfigurations}
};

extensions.forEach(extension => extension.configureClient(clientConfiguration));
extensions.forEach(extension => extension.configure(extensionConfiguration));

0 comments on commit b805825

Please sign in to comment.