Skip to content

Commit

Permalink
Support stringArray type in endpoints params (#3742)
Browse files Browse the repository at this point in the history
## Motivation and Context
<!--- Why is this change required? What problem does it solve? -->
<!--- If it fixes an open issue, please link to the issue here -->

## Description
<!--- Describe your changes in detail -->
Updated our endpoint rule generation to support the new `stringArray`
parameter type

## Testing
<!--- Please describe in detail how you tested your changes -->
<!--- Include details of your testing environment, and the tests you ran
to -->
<!--- see how your change affects other areas of the code, etc. -->
Updated the existing `EndpointsDecoratorTest` with tests for
`stringArray`

## Checklist
<!--- If a checkbox below is not applicable, then please DELETE it
rather than leaving it unchecked -->
- [x] I have updated `CHANGELOG.next.toml` if I made changes to the
smithy-rs codegen or runtime crates


----

_By submitting this pull request, I confirm that you can use, modify,
copy, and redistribute this contribution, under the terms of your
choice._

---------

Co-authored-by: Zelda Hessler <[email protected]>
  • Loading branch information
landonxjames and Velfi authored Jul 8, 2024
1 parent 20ad888 commit 2295d5b
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 8 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.next.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,10 @@
# message = "Fix typos in module documentation for generated crates"
# references = ["smithy-rs#920"]
# meta = { "breaking" = false, "tada" = false, "bug" = false, "target" = "client | server | all"}
# author = "rcoh"
# author = "rcoh"

[[smithy-rs]]
message = "Support `stringArray` type in endpoints params"
references = ["smithy-rs#3742"]
meta = { "breaking" = false, "tada" = false, "bug" = false, "target" = "client"}
author = "landonxjames"
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ fun Model.sdkConfigSetter(
when (builtinType) {
ParameterType.STRING -> writable { rust("|s|s.to_string()") }
ParameterType.BOOLEAN -> null
// No builtins currently map to stringArray
else -> PANIC("needs to handle unimplemented endpoint parameter builtin type: $builtinType")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package software.amazon.smithy.rust.codegen.client.smithy.endpoint

import software.amazon.smithy.codegen.core.Symbol
import software.amazon.smithy.model.shapes.BooleanShape
import software.amazon.smithy.model.shapes.ListShape
import software.amazon.smithy.model.shapes.ShapeType
import software.amazon.smithy.model.shapes.StringShape
import software.amazon.smithy.rulesengine.traits.ClientContextParamDefinition
Expand Down Expand Up @@ -51,6 +52,7 @@ class ClientContextConfigCustomization(ctx: ClientCodegenContext) : ConfigCustom
when (shapeType) {
ShapeType.STRING -> StringShape.builder().id("smithy.api#String").build()
ShapeType.BOOLEAN -> BooleanShape.builder().id("smithy.api#Boolean").build()
ShapeType.LIST -> ListShape.builder().id("smithy.api#List").build()
else -> TODO("unsupported type")
},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ fun Parameter.symbol(): Symbol {
when (this.type) {
ParameterType.STRING -> RustType.String
ParameterType.BOOLEAN -> RustType.Bool
ParameterType.STRING_ARRAY -> RustType.Vec(RustType.String)
else -> TODO("unexpected type: ${this.type}")
}
// Parameter return types are always optional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package software.amazon.smithy.rust.codegen.client.smithy.endpoint.generators

import software.amazon.smithy.model.node.ArrayNode
import software.amazon.smithy.model.node.BooleanNode
import software.amazon.smithy.model.node.Node
import software.amazon.smithy.model.node.StringNode
Expand Down Expand Up @@ -156,6 +157,12 @@ class EndpointParamsInterceptorGenerator(
when (node) {
is StringNode -> rust("Some(${node.value.dq()}.to_string())")
is BooleanNode -> rust("Some(${node.value})")
is ArrayNode -> {
// Cast the elements to a StringNode so this will fail if non-string values are provided
val elms = node.elements.map { "${(it as StringNode).value.dq()}.to_string()" }.joinToString(",")
rust("Some(vec![$elms])")
}

else -> PANIC("unsupported default value: $node")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,13 @@ class EndpointsDecoratorTest {
}
}],
"parameters": {
"Bucket": { "required": false, "type": "String" },
"Region": { "required": false, "type": "String", "builtIn": "AWS::Region" },
"BuiltInWithDefault": { "required": true, "type": "String", "builtIn": "AWS::DefaultBuiltIn", "default": "some-default" },
"BoolBuiltInWithDefault": { "required": true, "type": "Boolean", "builtIn": "AWS::FooBar", "default": true },
"AStringParam": { "required": false, "type": "String" },
"ABoolParam": { "required": false, "type": "Boolean" }
"Bucket": { "required": false, "type": "string" },
"Region": { "required": false, "type": "string", "builtIn": "AWS::Region" },
"BuiltInWithDefault": { "required": true, "type": "string", "builtIn": "AWS::DefaultBuiltIn", "default": "some-default" },
"BoolBuiltInWithDefault": { "required": true, "type": "boolean", "builtIn": "AWS::FooBar", "default": true },
"AStringParam": { "required": false, "type": "string" },
"ABoolParam": { "required": false, "type": "boolean" },
"AStringArrayParam": { "required": false, "type": "stringArray" }
}
})
@clientContextParams(
Expand Down Expand Up @@ -107,7 +108,10 @@ class EndpointsDecoratorTest {
operations: [TestOperation]
}
@staticContextParams(Region: { value: "us-east-2" })
@staticContextParams(
Region: { value: "us-east-2" },
AStringArrayParam: {value: ["a", "b", "c"]}
)
operation TestOperation {
input: TestOperationInput
}
Expand Down Expand Up @@ -184,6 +188,12 @@ class EndpointsDecoratorTest {
.a_bool_param(false)
.a_string_param("hello".to_string())
.region("us-east-2".to_string())
.a_string_array_param(
vec!["a", "b", "c"]
.iter()
.map(ToString::to_string)
.collect::<Vec<_>>()
)
.build()
.unwrap()
);
Expand All @@ -198,6 +208,7 @@ class EndpointsDecoratorTest {
let interceptor = TestInterceptor::default();
let config = Config::builder()
.behavior_version_latest()
.http_client(NeverClient::new())
.interceptor(interceptor.clone())
.timeout_config(
Expand Down

0 comments on commit 2295d5b

Please sign in to comment.