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

allow TypeScriptIntegration to write prior to the config object literal #1054

Merged
merged 2 commits into from
Oct 24, 2023
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
2 changes: 2 additions & 0 deletions .changeset/great-schools-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,17 @@ void generate(LanguageTarget target) {
.replaceFirst(CodegenUtils.SOURCE_FOLDER + "/", ""))
.replace("${clientConfigName}", symbolProvider.toSymbol(service).getName() + "Config")
.replace("${apiVersion}", service.getVersion())
.replace("$", "$$") // sanitize template place holders.
.replace("$${customizations}", "${L@customizations}");
.replace("${", "$${") // sanitize template place holders.
.replace("$${customizations}", "${L@customizations}")
.replace("$${prepareCustomizations}", "${L@prepareCustomizations}");

delegator.useFileWriter(target.getTargetFilename(), writer -> {
// Inject customizations into the ~template.
writer.onSection("prepareCustomizations", original -> {
for (TypeScriptIntegration integration : integrations) {
integration.prepareCustomizations(writer, target, settings, model);
}
});
Comment on lines +178 to +182
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not in the scope of this PR, but I feel we should move away from using templates and instead migrate to writers and CodeSections.

By using DirectedCodegen, CodeInterceptors can be used. This would allow state to be accessible to the interceptors (e.g. target) and avoid adding a new integration point in TypeScriptIntegration, e.g.

public record PrepareRuntimeConfigCodeSection(LanguageTarget target) implements CodeSection {}

public final class ExamplePrepareRuntimeConfigCodeInterceptorIntegration implements TypeScriptIntegration {
    @Override
    public List<? extends CodeInterceptor<? extends CodeSection, TypeScriptWriter>> interceptors(
        TypeScriptCodegenContext codegenContext) {
        return List.of(CodeInterceptor.appender(PrepareRuntimeConfigCodeSection.class, (w, s) -> {
            // ...
        }));
    }
}

writer.indent().onSection("customizations", original -> {
// Start with defaults, use a TreeMap for keeping entries sorted.
Map<String, Consumer<TypeScriptWriter>> configs =
Expand All @@ -198,7 +204,7 @@ void generate(LanguageTarget target) {
});
});
writer.dedent();
writer.write(contents, "");
writer.write(contents, "", "");
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,4 +253,17 @@ default Map<String, Consumer<TypeScriptWriter>> getRuntimeConfigWriters(
default List<ExtensionConfigurationInterface> getExtensionConfigurationInterfaces() {
return Collections.emptyList();
}

/**
* Allows the customization to write arbitrary preparatory code prior to the returned config object.
*/
@SmithyInternalApi
default void prepareCustomizations(
TypeScriptWriter writer,
LanguageTarget target,
TypeScriptSettings settings,
Model model
) {
return;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const getRuntimeConfig = (config: ${clientConfigName}) => {
const defaultsMode = resolveDefaultsModeConfig(config);
const defaultConfigProvider = () => defaultsMode().then(loadConfigsForDefaultMode);
const clientSharedValues = getSharedRuntimeConfig(config);
return {
${prepareCustomizations}return {
...clientSharedValues,
...config,
runtime: "browser",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { getRuntimeConfig as getBrowserRuntimeConfig } from "./runtimeConfig.bro
*/
export const getRuntimeConfig = (config: ${clientConfigName}) => {
const browserDefaults = getBrowserRuntimeConfig(config);
return {
${prepareCustomizations}return {
...browserDefaults,
...config,
runtime: "react-native",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { ${clientConfigName} } from "${clientModuleName}";
/**
* @internal
*/
export const getRuntimeConfig = (config: ${clientConfigName}) => ({
apiVersion: "${apiVersion}",
${customizations}
});
export const getRuntimeConfig = (config: ${clientConfigName}) => {
${prepareCustomizations}return {
apiVersion: "${apiVersion}",
${customizations}
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const getRuntimeConfig = (config: ${clientConfigName}) => {
const defaultsMode = resolveDefaultsModeConfig(config);
const defaultConfigProvider = () => defaultsMode().then(loadConfigsForDefaultMode);
const clientSharedValues = getSharedRuntimeConfig(config);
return {
${prepareCustomizations}return {
...clientSharedValues,
...config,
runtime: "node",
Expand Down