Skip to content

Commit 131a09f

Browse files
author
Steven Yuan
committed
Add matchSettings() to RuntimeClientPlugins
Enable `RuntimeClientPlugin`s to be filtered based on settings, and filter at the top-level of `DirectedTypeScriptCodegen`.
1 parent 2a17508 commit 131a09f

File tree

3 files changed

+71
-3
lines changed

3 files changed

+71
-3
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,12 @@ public TypeScriptCodegenContext createContext(CreateContextDirective<TypeScriptS
9292
directive.integrations().forEach(integration -> {
9393
LOGGER.info(() -> "Adding TypeScriptIntegration: " + integration.getClass().getName());
9494
integration.getClientPlugins().forEach(runtimePlugin -> {
95-
LOGGER.info(() -> "Adding TypeScript runtime plugin: " + runtimePlugin);
96-
runtimePlugins.add(runtimePlugin);
95+
if (runtimePlugin.matchesSettings(directive.model(), directive.service(), directive.settings())) {
96+
LOGGER.info(() -> "Adding TypeScript runtime plugin: " + runtimePlugin);
97+
runtimePlugins.add(runtimePlugin);
98+
} else {
99+
LOGGER.info(() -> "Skipping TypeScript runtime plugin based on settings: " + runtimePlugin);
100+
}
97101
});
98102
});
99103

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/RuntimeClientPlugin.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import software.amazon.smithy.model.shapes.OperationShape;
2929
import software.amazon.smithy.model.shapes.ServiceShape;
3030
import software.amazon.smithy.typescript.codegen.TypeScriptDependency;
31+
import software.amazon.smithy.typescript.codegen.TypeScriptSettings;
3132
import software.amazon.smithy.utils.SmithyBuilder;
3233
import software.amazon.smithy.utils.SmithyUnstableApi;
3334
import software.amazon.smithy.utils.StringUtils;
@@ -54,6 +55,7 @@ public final class RuntimeClientPlugin implements ToSmithyBuilder<RuntimeClientP
5455
private final SymbolReference destroyFunction;
5556
private final BiPredicate<Model, ServiceShape> servicePredicate;
5657
private final OperationPredicate operationPredicate;
58+
private final SettingsPredicate settingsPredicate;
5759

5860
private RuntimeClientPlugin(Builder builder) {
5961
inputConfig = builder.inputConfig;
@@ -65,6 +67,7 @@ private RuntimeClientPlugin(Builder builder) {
6567
destroyFunction = builder.destroyFunction;
6668
operationPredicate = builder.operationPredicate;
6769
servicePredicate = builder.servicePredicate;
70+
settingsPredicate = builder.settingsPredicate;
6871

6972
boolean allNull = (inputConfig == null) && (resolvedConfig == null) && (resolveFunction == null);
7073
boolean allSet = (inputConfig != null) && (resolvedConfig != null) && (resolveFunction != null);
@@ -93,6 +96,19 @@ public interface OperationPredicate {
9396
boolean test(Model model, ServiceShape service, OperationShape operation);
9497
}
9598

99+
@FunctionalInterface
100+
public interface SettingsPredicate {
101+
/**
102+
* Tests if runtime client plugin should be applied based on settings.
103+
*
104+
* @param model Model the operation belongs to.
105+
* @param service Service the operation belongs to.
106+
* @param settings Settings from smithy-build configuration.
107+
* @return Returns true if runtime client plugin should be applied.
108+
*/
109+
boolean test(Model model, ServiceShape service, TypeScriptSettings settings);
110+
}
111+
96112
@FunctionalInterface
97113
public interface FunctionParamsSupplier {
98114
/**
@@ -298,6 +314,18 @@ public boolean matchesOperation(Model model, ServiceShape service, OperationShap
298314
return operationPredicate.test(model, service, operation);
299315
}
300316

317+
/**
318+
* Returns true if this plugin applies given a smithy-build configuration.
319+
*
320+
* @param model Model the operation belongs to.
321+
* @param service Service the operation belongs to.
322+
* @param settings Settings from smithy-build configuration to test against.
323+
* @return Returns true if the plugin is applied given a smithy-build configuration.
324+
*/
325+
public boolean matchesSettings(Model model, ServiceShape service, TypeScriptSettings settings) {
326+
return settingsPredicate.test(model, service, settings);
327+
}
328+
301329
public static Builder builder() {
302330
return new Builder();
303331
}
@@ -369,6 +397,7 @@ public static final class Builder implements SmithyBuilder<RuntimeClientPlugin>
369397
private SymbolReference destroyFunction;
370398
private BiPredicate<Model, ServiceShape> servicePredicate = (model, service) -> true;
371399
private OperationPredicate operationPredicate = (model, service, operation) -> false;
400+
private SettingsPredicate settingsPredicate = (model, service, settings) -> true;
372401

373402
@Override
374403
public RuntimeClientPlugin build() {
@@ -676,6 +705,22 @@ public Builder appliesOnlyToOperations(Set<String> operationNames) {
676705
return servicePredicate((model, service) -> false);
677706
}
678707

708+
/**
709+
* Configures a predicate that applies the plugin to a service if the
710+
* predicate matches a given model and service and settings.
711+
*
712+
* <p>Setting a custom settings predicate is useful for plugins
713+
* that should only be applied based on certain smithy-build
714+
* configurations.
715+
*
716+
* @param settingsPredicate Settings predicate.
717+
* @return Returns the builder.
718+
*/
719+
public Builder settingsPredicate(SettingsPredicate settingsPredicate) {
720+
this.settingsPredicate = Objects.requireNonNull(settingsPredicate);
721+
return this;
722+
}
723+
679724
/**
680725
* Configures a predicate that applies the plugin to a service if the
681726
* predicate matches a given model and service.

smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/integration/RuntimeClientPluginTest.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import software.amazon.smithy.model.Model;
1313
import software.amazon.smithy.model.shapes.OperationShape;
1414
import software.amazon.smithy.model.shapes.ServiceShape;
15-
import software.amazon.smithy.utils.ListUtils;
15+
import software.amazon.smithy.typescript.codegen.TypeScriptSettings;
1616

1717
public class RuntimeClientPluginTest {
1818
@Test
@@ -68,6 +68,25 @@ public void allowsConfigurableServicePredicate() {
6868
assertThat(plugin.matchesOperation(model, service2, operation), equalTo(false));
6969
}
7070

71+
@Test
72+
public void allowsConfigurableSettingsPredicate() {
73+
ServiceShape service = ServiceShape.builder().id("a.b#C").version("123").build();
74+
Model model = Model.assembler()
75+
.addShapes(service)
76+
.assemble()
77+
.unwrap();
78+
RuntimeClientPlugin createDefaultReadmeFlagPlugin = RuntimeClientPlugin.builder()
79+
.settingsPredicate((m, s, settings) -> settings.createDefaultReadme())
80+
.build();
81+
82+
TypeScriptSettings createDefaultReadmeTrueSettings = new TypeScriptSettings();
83+
createDefaultReadmeTrueSettings.setCreateDefaultReadme(true);
84+
assertThat(createDefaultReadmeFlagPlugin.matchesSettings(model, service, createDefaultReadmeTrueSettings), equalTo(true));
85+
86+
TypeScriptSettings createDefaultReadmeFalseSettings = new TypeScriptSettings();
87+
assertThat(createDefaultReadmeFlagPlugin.matchesSettings(model, service, createDefaultReadmeFalseSettings), equalTo(false));
88+
}
89+
7190
@Test
7291
public void configuresWithDefaultConventions() {
7392
Map<String, Object> resolveFunctionParams = new HashMap<String, Object>() {{

0 commit comments

Comments
 (0)