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 String Array parameters in Endpoints #1376

Merged
merged 5 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -18,6 +18,7 @@
import java.util.AbstractMap;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import software.amazon.smithy.model.node.BooleanNode;
import software.amazon.smithy.model.node.Node;
import software.amazon.smithy.model.node.ObjectNode;
Expand Down Expand Up @@ -64,6 +65,10 @@ public ParameterGenerator(String key, Node param, boolean isInputKey) {
case "boolean":
tsParamType = "boolean";
break;
case "StringArray":
trivikr marked this conversation as resolved.
Show resolved Hide resolved
case "stringArray":
tsParamType = "string[]";
break;
default:
// required by linter
}
Expand Down Expand Up @@ -102,6 +107,12 @@ public String defaultAsCodeString() {
case "boolean":
buffer += paramNode.expectBooleanMember("default").getValue() ? "true" : "false";
break;
case "StringArray":
trivikr marked this conversation as resolved.
Show resolved Hide resolved
case "stringArray":
buffer += paramNode.expectArrayMember("default").getElements().stream()
.map(element -> "'" + element.expectStringNode().getValue() + "'")
.collect(Collectors.joining(", ", "[", "]"));
break;
default:
throw new RuntimeException("Unhandled endpoint param type: " + type.getValue());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import software.amazon.smithy.model.node.ArrayNode;
import software.amazon.smithy.model.node.Node;
import software.amazon.smithy.model.node.NodeVisitor;
import software.amazon.smithy.model.node.ObjectNode;
Expand All @@ -26,6 +28,7 @@
import software.amazon.smithy.model.shapes.OperationShape;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.shapes.ShapeType;
import software.amazon.smithy.rulesengine.traits.ClientContextParamsTrait;
import software.amazon.smithy.rulesengine.traits.ContextParamTrait;
import software.amazon.smithy.rulesengine.traits.EndpointRuleSetTrait;
Expand Down Expand Up @@ -66,10 +69,23 @@ public Map<String, String> getClientContextParams() {
if (trait.isPresent()) {
ClientContextParamsTrait clientContextParamsTrait = trait.get();
clientContextParamsTrait.getParameters().forEach((name, definition) -> {
map.put(
name,
definition.getType().toString().toLowerCase() // "boolean" and "string" are directly usable in TS.
);
ShapeType shapeType = definition.getType();
if (shapeType.isShapeType(ShapeType.STRING) || shapeType.isShapeType(ShapeType.BOOLEAN)) {
map.put(
name,
// "boolean" and "string" are directly usable in TS.
definition.getType().toString().toLowerCase()
);
} else if (shapeType.isShapeType(ShapeType.LIST)) {
map.put(
name,
"string[]" // Only string lists are supported.
);
} else {
throw new RuntimeException("unexpected type "
+ definition.getType().toString()
+ " received as clientContextParam.");
}
});
}
return map;
Expand All @@ -90,6 +106,11 @@ public Map<String, String> getStaticContextParamValues(OperationShape operation)
value = "`" + definition.getValue().expectStringNode().toString() + "`";
} else if (definition.getValue().isBooleanNode()) {
value = definition.getValue().expectBooleanNode().toString();
} else if (definition.getValue().isArrayNode()) {
ArrayNode arrayNode = definition.getValue().expectArrayNode();
value = arrayNode.getElements().stream()
.map(element -> "'" + element.expectStringNode().getValue() + "'")
trivikr marked this conversation as resolved.
Show resolved Hide resolved
.collect(Collectors.joining(", ", "[", "]"));
} else {
throw new RuntimeException("unexpected type "
+ definition.getValue().getType().toString()
Expand Down
Loading