Skip to content

Support MultiSelect List in OperationContextParams #1536

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

Merged
merged 5 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -277,39 +277,7 @@ public Map<String, String> getOperationContextParamValues(OperationShape operati
String separator = "?.";
String value = "input";
String path = definition.getPath();

// Split JMESPath expression string on separator and add JavaScript equivalent.
for (String part : path.split("[" + separator + "]")) {
if (value.endsWith(")")) {
// The value is an object, which needs to run on map.
value += ".map((obj: any) => obj";
}

// 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 = value + separator + key + separator + "map((obj: any) => obj";
continue;
}

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

// Remove no-op map, if it exists.
final String noOpMap = "map((obj: any) => obj";
Expand All @@ -328,6 +296,74 @@ public Map<String, String> getOperationContextParamValues(OperationShape operati
return map;
}

private String getJmesPathExpression(String separator, String value, String path) {
// Split JMESPath expression string on separator and add JavaScript equivalent.
while (path.length() > 0) {
if (path.startsWith("[") && !path.startsWith("[*]")) {
// Process MultiSelect List https://jmespath.org/specification.html#multiselect-list
if (value.endsWith("obj")) {
value = value.substring(0, value.length() - 3);
}

value += "[";
path = path.substring(1);

while (true) {
int commaIndex = path.indexOf(",");
int sepIndex = (commaIndex == -1) ? path.indexOf("]") : commaIndex;
String part = path.substring(0, sepIndex);
path = path.substring(sepIndex + 1).trim();
value += getJmesPathExpression(separator, "obj", part) + ",";
if (commaIndex == -1) {
// Remove trailing comma and close bracket.
value = value.substring(0, value.length() - 1) + "].filter((i) => i))";
break;
}
}
continue;
}

int dotIndex = path.indexOf(".");
String part = dotIndex == -1 ? path : path.substring(0, dotIndex);
path = dotIndex == -1 ? "" : path.substring(dotIndex + 1);
value = getJmesPathExpressionSection(separator, value, part);
}
return value;
}

private String getJmesPathExpressionSection(String separator, String value, String part) {
if (value.endsWith(")")) {
// The value is an object, which needs to run on map.
value += ".map((obj: any) => obj";
}

// 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 + " ?? {})";
return value;
}

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

// 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 = value + separator + key + separator + "map((obj: any) => obj";
return value;
}

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public void writesOperationContextParamValues() {
"opContextParamWildcardExpressionList: { type: \"operationContextParams\", get: (input?: any) => input?.fooList }",
"opContextParamWildcardExpressionListObj: { type: \"operationContextParams\", get: (input?: any) => input?.fooListObj?.map((obj: any) => obj?.key) }",
"opContextParamWildcardExpressionHash: { type: \"operationContextParams\", get: (input?: any) => Object.values(input?.fooObjObj ?? {}).map((obj: any) => obj?.bar) }",
"opContextParamMultiSelectList: { type: \"operationContextParams\", get: (input?: any) => input?.fooListObjObj?.map((obj: any) => [obj?.fooList[0],obj?.fooObject?.bar,obj?.fooString].filter((i) => i)) }",
"opContextParamKeys: { type: \"operationContextParams\", get: (input?: any) => Object.keys(input?.fooKeys ?? {}) }",
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ namespace smithy.example
opContextParamWildcardExpressionHash: {
type: "stringArray",
},
opContextParamMultiSelectList: {
type: "stringArray",
},
opContextParamKeys: {
type: "stringArray",
},
Expand All @@ -37,6 +40,7 @@ service Example {
"opContextParamWildcardExpressionList": { path: "fooList[*]" }
"opContextParamWildcardExpressionListObj": { path: "fooListObj[*].key" }
"opContextParamWildcardExpressionHash": { path: "fooObjObj.*.bar" }
"opContextParamMultiSelectList": { path: "fooListObjObj[*].[fooList[0], fooObject.bar, fooString]" }
"opContextParamKeys": { path: "keys(fooKeys)" }
)
operation GetFoo {
Expand All @@ -48,7 +52,8 @@ operation GetFoo {
structure GetFooInput {
fooKeys: FooObject,
fooList: FooList,
fooListObj: FooListObj,
fooListObj: FooListObject,
fooListObjObj: FooListObjectObject,
fooObj: FooObject,
fooObjObj: FooObjectObject,
fooString: String,
Expand All @@ -58,6 +63,16 @@ structure FooObject {
bar: String
}

list FooListObjectObject {
member: FooMultiSelectObjectObject
}

structure FooMultiSelectObjectObject {
fooList: FooList
fooObject: FooObject
fooString: String
}

structure FooObjectObject {
baz: FooObject
}
Expand All @@ -66,11 +81,11 @@ list FooList {
member: String
}

list FooListObj {
member: FooListObjMember
list FooListObject {
member: FooListObjectMember
}

structure FooListObjMember {
structure FooListObjectMember {
key: String
}

Expand Down
Loading