28
28
import software .amazon .smithy .model .shapes .OperationShape ;
29
29
import software .amazon .smithy .model .shapes .ServiceShape ;
30
30
import software .amazon .smithy .typescript .codegen .TypeScriptDependency ;
31
+ import software .amazon .smithy .typescript .codegen .TypeScriptSettings ;
31
32
import software .amazon .smithy .utils .SmithyBuilder ;
32
33
import software .amazon .smithy .utils .SmithyUnstableApi ;
33
34
import software .amazon .smithy .utils .StringUtils ;
@@ -54,6 +55,7 @@ public final class RuntimeClientPlugin implements ToSmithyBuilder<RuntimeClientP
54
55
private final SymbolReference destroyFunction ;
55
56
private final BiPredicate <Model , ServiceShape > servicePredicate ;
56
57
private final OperationPredicate operationPredicate ;
58
+ private final SettingsPredicate settingsPredicate ;
57
59
58
60
private RuntimeClientPlugin (Builder builder ) {
59
61
inputConfig = builder .inputConfig ;
@@ -65,6 +67,7 @@ private RuntimeClientPlugin(Builder builder) {
65
67
destroyFunction = builder .destroyFunction ;
66
68
operationPredicate = builder .operationPredicate ;
67
69
servicePredicate = builder .servicePredicate ;
70
+ settingsPredicate = builder .settingsPredicate ;
68
71
69
72
boolean allNull = (inputConfig == null ) && (resolvedConfig == null ) && (resolveFunction == null );
70
73
boolean allSet = (inputConfig != null ) && (resolvedConfig != null ) && (resolveFunction != null );
@@ -93,6 +96,19 @@ public interface OperationPredicate {
93
96
boolean test (Model model , ServiceShape service , OperationShape operation );
94
97
}
95
98
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
+
96
112
@ FunctionalInterface
97
113
public interface FunctionParamsSupplier {
98
114
/**
@@ -298,6 +314,18 @@ public boolean matchesOperation(Model model, ServiceShape service, OperationShap
298
314
return operationPredicate .test (model , service , operation );
299
315
}
300
316
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
+
301
329
public static Builder builder () {
302
330
return new Builder ();
303
331
}
@@ -369,6 +397,7 @@ public static final class Builder implements SmithyBuilder<RuntimeClientPlugin>
369
397
private SymbolReference destroyFunction ;
370
398
private BiPredicate <Model , ServiceShape > servicePredicate = (model , service ) -> true ;
371
399
private OperationPredicate operationPredicate = (model , service , operation ) -> false ;
400
+ private SettingsPredicate settingsPredicate = (model , service , settings ) -> true ;
372
401
373
402
@ Override
374
403
public RuntimeClientPlugin build () {
@@ -676,6 +705,22 @@ public Builder appliesOnlyToOperations(Set<String> operationNames) {
676
705
return servicePredicate ((model , service ) -> false );
677
706
}
678
707
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
+
679
724
/**
680
725
* Configures a predicate that applies the plugin to a service if the
681
726
* predicate matches a given model and service.
0 commit comments