|
32 | 32 | import software.amazon.smithy.rulesengine.traits.ClientContextParamsTrait;
|
33 | 33 | import software.amazon.smithy.rulesengine.traits.ContextParamTrait;
|
34 | 34 | import software.amazon.smithy.rulesengine.traits.EndpointRuleSetTrait;
|
| 35 | +import software.amazon.smithy.rulesengine.traits.OperationContextParamsTrait; |
35 | 36 | import software.amazon.smithy.rulesengine.traits.StaticContextParamsTrait;
|
36 | 37 | import software.amazon.smithy.utils.SmithyInternalApi;
|
37 | 38 |
|
@@ -152,6 +153,54 @@ public Map<String, String> getContextParams(Shape operationInput) {
|
152 | 153 | return map;
|
153 | 154 | }
|
154 | 155 |
|
| 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 | + |
155 | 204 | private static class RuleSetParameterFinderVisitor extends NodeVisitor.Default<Void> {
|
156 | 205 | private final Map<String, String> map;
|
157 | 206 |
|
|
0 commit comments