Skip to content

Commit e18086e

Browse files
committed
Process OperationContextParams in RuleSetParameterFinder
1 parent 2dad138 commit e18086e

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/endpointsV2/RuleSetParameterFinder.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import software.amazon.smithy.rulesengine.traits.ClientContextParamsTrait;
3333
import software.amazon.smithy.rulesengine.traits.ContextParamTrait;
3434
import software.amazon.smithy.rulesengine.traits.EndpointRuleSetTrait;
35+
import software.amazon.smithy.rulesengine.traits.OperationContextParamsTrait;
3536
import software.amazon.smithy.rulesengine.traits.StaticContextParamsTrait;
3637
import software.amazon.smithy.utils.SmithyInternalApi;
3738

@@ -152,6 +153,54 @@ public Map<String, String> getContextParams(Shape operationInput) {
152153
return map;
153154
}
154155

156+
/**
157+
* Get map of params to JavaScript equivalent of provided JMESPath expressions.
158+
*/
159+
public Map<String, String> getOperationContextParamValues(Shape operationInput) {
160+
Map<String, String> map = new HashMap<>();
161+
162+
Optional<OperationContextParamsTrait> trait = operationInput.getTrait(OperationContextParamsTrait.class);
163+
if (trait.isPresent()) {
164+
OperationContextParamsTrait operationContextParamsTrait = trait.get();
165+
operationContextParamsTrait.getParameters().forEach((name, definition) -> {
166+
String separator = ".";
167+
String value = "this" + separator + "input";
168+
String path = definition.getPath();
169+
170+
// Split JMESPath expression string on separator and add JavaScript equivalent.
171+
for (String part : path.split("[" + separator + "]")) {
172+
// Process keys https://jmespath.org/specification.html#keys
173+
if (part.startsWith("keys(")) {
174+
// Get provided object for which keys are to be extracted.
175+
String object = part.substring(5, part.length() - 1);
176+
value = "Object.keys(" + value + separator + object + ")";
177+
continue;
178+
}
179+
180+
// Process list wildcard expression https://jmespath.org/specification.html#wildcard-expressions
181+
if (part.equals("*") || part.equals("[*]")) {
182+
value = "Object.values(" + value + ")";
183+
continue;
184+
}
185+
186+
// Process hash wildcard expression https://jmespath.org/specification.html#wildcard-expressions
187+
if (part.endsWith("[*]")) {
188+
// Get key to run hash wildcard on.
189+
String key = part.substring(0, part.length() - 3);
190+
value = "Object.values(" + value + separator + key + ")";
191+
continue;
192+
}
193+
194+
// Treat remaining part as identifier without spaces https://jmespath.org/specification.html#identifiers
195+
value += separator + part;
196+
}
197+
198+
});
199+
}
200+
201+
return map;
202+
}
203+
155204
private static class RuleSetParameterFinderVisitor extends NodeVisitor.Default<Void> {
156205
private final Map<String, String> map;
157206

0 commit comments

Comments
 (0)