Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Process Operation Context Params in Endpoints #1379

Merged
merged 13 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,12 @@ private void generateEndpointParameterInstructionProvider() {
}
paramNames.add(name);
});

parameterFinder.getOperationContextParamValues(operationInput).forEach((name, jmesPathForInputInJs) -> {
writer.write(
"$L: { type: \"operationContextParams\", name: $L },",
trivikr marked this conversation as resolved.
Show resolved Hide resolved
name, jmesPathForInputInJs);
});
}
writer.write("})")
.dedent();
Expand Down
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) {
trivikr marked this conversation as resolved.
Show resolved Hide resolved
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 = ".";
trivikr marked this conversation as resolved.
Show resolved Hide resolved
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 + "]")) {
trivikr marked this conversation as resolved.
Show resolved Hide resolved
// 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
Loading