15
15
16
16
package software .amazon .smithy .typescript .codegen .integration ;
17
17
18
- import java .util .ArrayList ;
19
- import java .util .List ;
18
+ import java .util .HashMap ;
19
+ import java .util .Map ;
20
20
import java .util .Objects ;
21
21
import java .util .Optional ;
22
22
import java .util .Set ;
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 .utils .ListUtils ;
32
31
import software .amazon .smithy .utils .SmithyBuilder ;
33
32
import software .amazon .smithy .utils .SmithyUnstableApi ;
34
33
import software .amazon .smithy .utils .StringUtils ;
@@ -49,9 +48,9 @@ public final class RuntimeClientPlugin implements ToSmithyBuilder<RuntimeClientP
49
48
private final SymbolReference inputConfig ;
50
49
private final SymbolReference resolvedConfig ;
51
50
private final SymbolReference resolveFunction ;
52
- private final List < String > additionalResolveFunctionParameters ;
51
+ private final FunctionParamsSupplier resolveFunctionParamsSupplier ;
53
52
private final SymbolReference pluginFunction ;
54
- private final List < String > additionalPluginFunctionParameters ;
53
+ private final FunctionParamsSupplier pluginFunctionParamsSupplier ;
55
54
private final SymbolReference destroyFunction ;
56
55
private final BiPredicate <Model , ServiceShape > servicePredicate ;
57
56
private final OperationPredicate operationPredicate ;
@@ -60,21 +59,13 @@ private RuntimeClientPlugin(Builder builder) {
60
59
inputConfig = builder .inputConfig ;
61
60
resolvedConfig = builder .resolvedConfig ;
62
61
resolveFunction = builder .resolveFunction ;
63
- additionalResolveFunctionParameters = ListUtils . copyOf ( builder .additionalResolveFunctionParameters ) ;
62
+ resolveFunctionParamsSupplier = builder .resolveFunctionParamsSupplier ;
64
63
pluginFunction = builder .pluginFunction ;
65
- additionalPluginFunctionParameters = ListUtils . copyOf ( builder .additionalPluginFunctionParameters ) ;
64
+ pluginFunctionParamsSupplier = builder .pluginFunctionParamsSupplier ;
66
65
destroyFunction = builder .destroyFunction ;
67
66
operationPredicate = builder .operationPredicate ;
68
67
servicePredicate = builder .servicePredicate ;
69
68
70
- if (!additionalResolveFunctionParameters .isEmpty () && resolveFunction == null ) {
71
- throw new IllegalStateException ("Additional parameters can only be set if a resolve function is set." );
72
- }
73
-
74
- if (!additionalPluginFunctionParameters .isEmpty () && pluginFunction == null ) {
75
- throw new IllegalStateException ("Additional parameters can only be set if a plugin function is set." );
76
- }
77
-
78
69
boolean allNull = (inputConfig == null ) && (resolvedConfig == null ) && (resolveFunction == null );
79
70
boolean allSet = (inputConfig != null ) && (resolvedConfig != null ) && (resolveFunction != null );
80
71
if (!(allNull || allSet )) {
@@ -102,6 +93,19 @@ public interface OperationPredicate {
102
93
boolean test (Model model , ServiceShape service , OperationShape operation );
103
94
}
104
95
96
+ @ FunctionalInterface
97
+ public interface FunctionParamsSupplier {
98
+ /**
99
+ * Returns dynamic parameters for resolve function.
100
+ *
101
+ * @param model Model the operation belongs to.
102
+ * @param service Service the operation belongs to.
103
+ * @param operation Operation to test.
104
+ * @return Returns true if middleware should be applied to the operation.
105
+ */
106
+ Map <String , Object > apply (Model model , ServiceShape service , OperationShape operation );
107
+ }
108
+
105
109
/**
106
110
* Gets the optionally present symbol reference that points to the
107
111
* <em>Input configuration interface</em> for the plugin.
@@ -163,7 +167,6 @@ public Optional<SymbolReference> getResolvedConfig() {
163
167
*
164
168
* @return Returns the optionally present resolve function.
165
169
* @see #getInputConfig()
166
- * @see #getAdditionalResolveFunctionParameters()
167
170
* @see #getResolvedConfig()
168
171
*/
169
172
public Optional <SymbolReference > getResolveFunction () {
@@ -173,14 +176,23 @@ public Optional<SymbolReference> getResolveFunction() {
173
176
/**
174
177
* Gets a list of additional parameters to be supplied to the
175
178
* resolve function. These parameters are to be supplied to resolve
176
- * function as Nth(N > 1) positional arguments . The list is empty if
177
- * there are no additional parameters.
179
+ * function as second argument . The map is empty if there are
180
+ * no additional parameters.
178
181
*
179
- * @return Returns the optionally present list of parameters.
180
- * @see #getResolveFunction()
182
+ * @param model Model the operation belongs to.
183
+ * @param service Service the operation belongs to.
184
+ * @param operation Operation to test against.
185
+ * @return Returns the optionally present map of parameters.
181
186
*/
182
- public List <String > getAdditionalResolveFunctionParameters () {
183
- return additionalResolveFunctionParameters ;
187
+ public Map <String , Object > getResolveFunctionParameters (
188
+ Model model ,
189
+ ServiceShape service ,
190
+ OperationShape operation
191
+ ) {
192
+ if (resolveFunctionParamsSupplier != null ) {
193
+ return resolveFunctionParamsSupplier .apply (model , service , operation );
194
+ }
195
+ return new HashMap <String , Object >();
184
196
}
185
197
186
198
/**
@@ -211,14 +223,23 @@ public Optional<SymbolReference> getPluginFunction() {
211
223
/**
212
224
* Gets a list of additional parameters to be supplied to the
213
225
* plugin function. These parameters are to be supplied to plugin
214
- * function as an options hash . The list is empty if
215
- * there are no additional parameters.
226
+ * function as second argument . The map is empty if there are
227
+ * no additional parameters.
216
228
*
217
- * @return Returns the optionally present list of parameters.
218
- * @see #getPluginFunction()
229
+ * @param model Model the operation belongs to.
230
+ * @param service Service the operation belongs to.
231
+ * @param operation Operation to test against.
232
+ * @return Returns the optionally present map of parameters.
219
233
*/
220
- public List <String > getAdditionalPluginFunctionParameters () {
221
- return additionalPluginFunctionParameters ;
234
+ public Map <String , Object > getPluginFunctionParameters (
235
+ Model model ,
236
+ ServiceShape service ,
237
+ OperationShape operation
238
+ ) {
239
+ if (pluginFunctionParamsSupplier != null ) {
240
+ return pluginFunctionParamsSupplier .apply (model , service , operation );
241
+ }
242
+ return new HashMap <String , Object >();
222
243
}
223
244
224
245
/**
@@ -284,7 +305,9 @@ public Builder toBuilder() {
284
305
.inputConfig (inputConfig )
285
306
.resolvedConfig (resolvedConfig )
286
307
.resolveFunction (resolveFunction )
308
+ .resolveFunctionParamsSupplier (resolveFunctionParamsSupplier )
287
309
.pluginFunction (pluginFunction )
310
+ .pluginFunctionParamsSupplier (pluginFunctionParamsSupplier )
288
311
.destroyFunction (destroyFunction );
289
312
290
313
// Set these directly since their setters have mutual side-effects.
@@ -300,9 +323,7 @@ public String toString() {
300
323
+ "inputConfig=" + inputConfig
301
324
+ ", resolvedConfig=" + resolvedConfig
302
325
+ ", resolveFunction=" + resolveFunction
303
- + ", additionalResolveFunctionParameters=" + additionalResolveFunctionParameters
304
326
+ ", pluginFunction=" + pluginFunction
305
- + ", additionalPluginFunctionParameters=" + additionalPluginFunctionParameters
306
327
+ ", destroyFunction=" + destroyFunction
307
328
+ '}' ;
308
329
}
@@ -319,9 +340,9 @@ public boolean equals(Object o) {
319
340
return Objects .equals (inputConfig , that .inputConfig )
320
341
&& Objects .equals (resolvedConfig , that .resolvedConfig )
321
342
&& Objects .equals (resolveFunction , that .resolveFunction )
322
- && Objects .equals (additionalResolveFunctionParameters , that .additionalResolveFunctionParameters )
343
+ && Objects .equals (resolveFunctionParamsSupplier , that .resolveFunctionParamsSupplier )
323
344
&& Objects .equals (pluginFunction , that .pluginFunction )
324
- && Objects .equals (additionalPluginFunctionParameters , that .additionalPluginFunctionParameters )
345
+ && Objects .equals (pluginFunctionParamsSupplier , that .pluginFunctionParamsSupplier )
325
346
&& Objects .equals (destroyFunction , that .destroyFunction )
326
347
&& servicePredicate .equals (that .servicePredicate )
327
348
&& operationPredicate .equals (that .operationPredicate );
@@ -339,9 +360,9 @@ public static final class Builder implements SmithyBuilder<RuntimeClientPlugin>
339
360
private SymbolReference inputConfig ;
340
361
private SymbolReference resolvedConfig ;
341
362
private SymbolReference resolveFunction ;
342
- private List < String > additionalResolveFunctionParameters = new ArrayList <>() ;
363
+ private FunctionParamsSupplier resolveFunctionParamsSupplier ;
343
364
private SymbolReference pluginFunction ;
344
- private List < String > additionalPluginFunctionParameters = new ArrayList <>() ;
365
+ private FunctionParamsSupplier pluginFunctionParamsSupplier ;
345
366
private SymbolReference destroyFunction ;
346
367
private BiPredicate <Model , ServiceShape > servicePredicate = (model , service ) -> true ;
347
368
private OperationPredicate operationPredicate = (model , service , operation ) -> false ;
@@ -433,13 +454,16 @@ public Builder resolveFunction(SymbolReference resolveFunction) {
433
454
* {@link #inputConfig} must also be set.
434
455
*
435
456
* @param resolveFunction Function used to convert input to resolved.
436
- * @param additionalParameters Additional parameters to be generated as resolve function input.
457
+ * @param resolveFunctionParamsSupplier Function which returns params to be passed as resolve function input.
437
458
* @return Returns the builder.
438
459
* @see #getResolveFunction()
439
460
*/
440
- public Builder resolveFunction (SymbolReference resolveFunction , String ... additionalParameters ) {
461
+ public Builder resolveFunction (
462
+ SymbolReference resolveFunction ,
463
+ FunctionParamsSupplier resolveFunctionParamsSupplier
464
+ ) {
441
465
this .resolveFunction = resolveFunction ;
442
- this .additionalResolveFunctionParameters = ListUtils . of ( additionalParameters ) ;
466
+ this .resolveFunctionParamsSupplier = resolveFunctionParamsSupplier ;
443
467
return this ;
444
468
}
445
469
@@ -466,27 +490,33 @@ public Builder resolveFunction(Symbol resolveFunction) {
466
490
* {@link #inputConfig} must also be set.
467
491
*
468
492
* @param resolveFunction Function used to convert input to resolved.
469
- * @param additionalParameters Additional parameters to be generated as resolve function input.
493
+ * @param resolveFunctionParamsSupplier Function which returns params to be passed as resolve function input.
470
494
* @return Returns the builder.
471
495
* @see #getResolveFunction()
472
496
*/
473
- public Builder resolveFunction (Symbol resolveFunction , String ... additionalParameters ) {
474
- return resolveFunction (SymbolReference .builder ().symbol (resolveFunction ).build (), additionalParameters );
497
+ public Builder resolveFunction (
498
+ Symbol resolveFunction ,
499
+ FunctionParamsSupplier resolveFunctionParamsSupplier
500
+ ) {
501
+ return resolveFunction (
502
+ SymbolReference .builder ().symbol (resolveFunction ).build (),
503
+ resolveFunctionParamsSupplier
504
+ );
475
505
}
476
506
477
507
/**
478
- * Set additional positional input parameters to resolve function. Set
479
- * this with no arguments to remove the current parameters.
508
+ * Set function which returns input parameters to resolve function. Set
509
+ * function to return empty map to remove the current parameters.
480
510
*
481
511
* <p>If this is set, then all of {@link #resolveFunction},
482
512
* {@link #resolvedConfig} and {@link #inputConfig} must also be set.
483
513
*
484
- * @param additionalParameters Additional parameters to be generated as resolve function input.
514
+ * @param resolveFunctionParamsSupplier Function which returns params to be passed as resolve function input.
485
515
* @return Returns the builder.
486
516
* @see #getResolveFunction()
487
517
*/
488
- public Builder additionalResolveFunctionParameters ( String ... additionalParameters ) {
489
- this .additionalResolveFunctionParameters = ListUtils . of ( additionalParameters ) ;
518
+ public Builder resolveFunctionParamsSupplier ( FunctionParamsSupplier resolveFunctionParamsSupplier ) {
519
+ this .resolveFunctionParamsSupplier = resolveFunctionParamsSupplier ;
490
520
return this ;
491
521
}
492
522
@@ -508,13 +538,16 @@ public Builder pluginFunction(SymbolReference pluginFunction) {
508
538
* commands to use a specific middleware function.
509
539
*
510
540
* @param pluginFunction Plugin function symbol to invoke.
511
- * @param additionalParameters Additional parameters to be generated as plugin function input.
541
+ * @param pluginFunctionParamsSupplier Function which returns params to be passed as plugin function input.
512
542
* @return Returns the builder.
513
543
* @see #getPluginFunction()
514
544
*/
515
- public Builder pluginFunction (SymbolReference pluginFunction , String ... additionalParameters ) {
545
+ public Builder pluginFunction (
546
+ SymbolReference pluginFunction ,
547
+ FunctionParamsSupplier pluginFunctionParamsSupplier
548
+ ) {
516
549
this .pluginFunction = pluginFunction ;
517
- this .additionalPluginFunctionParameters = ListUtils . of ( additionalParameters ) ;
550
+ this .pluginFunctionParamsSupplier = pluginFunctionParamsSupplier ;
518
551
return this ;
519
552
}
520
553
@@ -535,24 +568,30 @@ public Builder pluginFunction(Symbol pluginFunction) {
535
568
* use a specific middleware function.
536
569
*
537
570
* @param pluginFunction Plugin function symbol to invoke.
538
- * @param additionalParameters Additional parameters to be generated as plugin function input.
571
+ * @param pluginFunctionParamsSupplier Function which returns params to be passed as plugin function input.
539
572
* @return Returns the builder.
540
573
* @see #getPluginFunction()
541
574
*/
542
- public Builder pluginFunction (Symbol pluginFunction , String ... additionalParameters ) {
543
- return pluginFunction (SymbolReference .builder ().symbol (pluginFunction ).build (), additionalParameters );
575
+ public Builder pluginFunction (
576
+ Symbol pluginFunction ,
577
+ FunctionParamsSupplier pluginFunctionParamsSupplier
578
+ ) {
579
+ return pluginFunction (
580
+ SymbolReference .builder ().symbol (pluginFunction ).build (),
581
+ pluginFunctionParamsSupplier
582
+ );
544
583
}
545
584
546
585
/**
547
- * Set additional positional input parameters to plugin function. Set
548
- * this with no arguments to remove the current parameters.
586
+ * Set function which returns input parameters to plugin function. Set
587
+ * function to return empty map to remove the current parameters.
549
588
*
550
- * @param additionalParameters Additional parameters to be generated as plugin function input.
589
+ * @param pluginFunctionParamsSupplier Function which returns params to be passed as plugin function input.
551
590
* @return Returns the builder.
552
591
* @see #getPluginFunction()
553
592
*/
554
- public Builder additionalPluginFunctionParameters ( String ... additionalParameters ) {
555
- this .additionalPluginFunctionParameters = ListUtils . of ( additionalParameters ) ;
593
+ public Builder pluginFunctionParamsSupplier ( FunctionParamsSupplier pluginFunctionParamsSupplier ) {
594
+ this .pluginFunctionParamsSupplier = pluginFunctionParamsSupplier ;
556
595
return this ;
557
596
}
558
597
0 commit comments