Skip to content

Commit

Permalink
Process OperationContextParams in RuleSetParameterFinder
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed Aug 28, 2024
1 parent 2dad138 commit e18086e
Showing 1 changed file with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import software.amazon.smithy.rulesengine.traits.ClientContextParamsTrait;
import software.amazon.smithy.rulesengine.traits.ContextParamTrait;
import software.amazon.smithy.rulesengine.traits.EndpointRuleSetTrait;
import software.amazon.smithy.rulesengine.traits.OperationContextParamsTrait;
import software.amazon.smithy.rulesengine.traits.StaticContextParamsTrait;
import software.amazon.smithy.utils.SmithyInternalApi;

Expand Down Expand Up @@ -152,6 +153,54 @@ public Map<String, String> getContextParams(Shape operationInput) {
return map;
}

/**
* Get map of params to JavaScript equivalent of provided JMESPath expressions.
*/
public Map<String, String> getOperationContextParamValues(Shape operationInput) {
Map<String, String> map = new HashMap<>();

Optional<OperationContextParamsTrait> trait = operationInput.getTrait(OperationContextParamsTrait.class);
if (trait.isPresent()) {
OperationContextParamsTrait operationContextParamsTrait = trait.get();
operationContextParamsTrait.getParameters().forEach((name, definition) -> {
String separator = ".";
String value = "this" + separator + "input";
String path = definition.getPath();

// Split JMESPath expression string on separator and add JavaScript equivalent.
for (String part : path.split("[" + separator + "]")) {
// Process keys https://jmespath.org/specification.html#keys
if (part.startsWith("keys(")) {
// Get provided object for which keys are to be extracted.
String object = part.substring(5, part.length() - 1);
value = "Object.keys(" + value + separator + object + ")";
continue;
}

// Process list wildcard expression https://jmespath.org/specification.html#wildcard-expressions
if (part.equals("*") || part.equals("[*]")) {
value = "Object.values(" + value + ")";
continue;
}

// Process hash wildcard expression https://jmespath.org/specification.html#wildcard-expressions
if (part.endsWith("[*]")) {
// Get key to run hash wildcard on.
String key = part.substring(0, part.length() - 3);
value = "Object.values(" + value + separator + key + ")";
continue;
}

// Treat remaining part as identifier without spaces https://jmespath.org/specification.html#identifiers
value += separator + part;
}

});
}

return map;
}

private static class RuleSetParameterFinderVisitor extends NodeVisitor.Default<Void> {
private final Map<String, String> map;

Expand Down

0 comments on commit e18086e

Please sign in to comment.