Skip to content

Commit 4fa6596

Browse files
committed
fix: account ID based endpoints
1 parent 612c39b commit 4fa6596

File tree

4 files changed

+52
-17
lines changed

4 files changed

+52
-17
lines changed

codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/waiters/KotlinJmespathExpressionVisitor.kt

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,11 @@ import software.amazon.smithy.kotlin.codegen.model.isEnum
1919
import software.amazon.smithy.kotlin.codegen.model.targetOrSelf
2020
import software.amazon.smithy.kotlin.codegen.model.traits.OperationInput
2121
import software.amazon.smithy.kotlin.codegen.model.traits.OperationOutput
22+
import software.amazon.smithy.kotlin.codegen.utils.doubleQuote
2223
import software.amazon.smithy.kotlin.codegen.utils.dq
2324
import software.amazon.smithy.kotlin.codegen.utils.getOrNull
2425
import software.amazon.smithy.kotlin.codegen.utils.toCamelCase
25-
import software.amazon.smithy.model.shapes.ListShape
26-
import software.amazon.smithy.model.shapes.MapShape
27-
import software.amazon.smithy.model.shapes.MemberShape
28-
import software.amazon.smithy.model.shapes.Shape
26+
import software.amazon.smithy.model.shapes.*
2927

3028
private val suffixSequence = sequenceOf("") + generateSequence(2) { it + 1 }.map(Int::toString) // "", "2", "3", etc.
3129

@@ -526,11 +524,20 @@ class KotlinJmespathExpressionVisitor(
526524
".$expr"
527525
}
528526

529-
private fun VisitedExpression.getKeys(): String {
530-
val keys = this.shape?.targetOrSelf(ctx.model)?.allMembers
531-
?.keys?.joinToString(", ", "listOf(", ")") { "\"$it\"" }
532-
return keys ?: "listOf<String>()"
533-
}
527+
// TODO: Support maps as objects throughout the visitor
528+
// This visitor assumes JMESPath 'objects' will be classes. DDB assumes JMESPath 'objects' will be maps
529+
private fun VisitedExpression.getKeys(): String =
530+
if (this.shape?.targetOrSelf(ctx.model)?.type == ShapeType.MAP) {
531+
"${this.identifier}?.keys?.toList()"
532+
} else {
533+
this
534+
.shape
535+
?.targetOrSelf(ctx.model)
536+
?.allMembers
537+
?.keys
538+
?.joinToString(", ", "listOf(", ")") { it.doubleQuote() }
539+
?: "listOf<String>()"
540+
}
534541

535542
private fun VisitedExpression.getValues(): String {
536543
val values = this.shape?.targetOrSelf(ctx.model)?.allMembers?.keys

codegen/smithy-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/OperationContextParamsTest.kt

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class OperationContextParamsTest {
8888
}
8989

9090
@Test
91-
fun testKeysFunctionPath() {
91+
fun testKeysFunctionPathWithStructure() {
9292
val input = """
9393
structure TestOperationRequest {
9494
Object: Object
@@ -114,4 +114,31 @@ class OperationContextParamsTest {
114114

115115
codegen(pathResultType, path, input).shouldContainOnlyOnceWithDiff(expected)
116116
}
117+
118+
@Test
119+
fun testKeysFunctionPathWithMap() {
120+
val input = """
121+
structure TestOperationRequest {
122+
Object: StringMap
123+
}
124+
125+
map StringMap {
126+
key: String
127+
value: String
128+
}
129+
""".trimIndent()
130+
131+
val path = "keys(Object)"
132+
val pathResultType = "stringArray"
133+
134+
val expected = """
135+
@Suppress("UNCHECKED_CAST")
136+
val input = request.context[HttpOperationContext.OperationInput] as TestOperationRequest
137+
val object = input.object
138+
val keys = object?.keys?.toList()
139+
builder.foo = keys
140+
""".formatForTest(" ")
141+
142+
codegen(pathResultType, path, input).shouldContainOnlyOnceWithDiff(expected)
143+
}
117144
}

tests/codegen/waiter-tests/model/function-keys.smithy

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ use smithy.waiters#waitable
1818
}
1919
]
2020
},
21-
KeysFunctionPrimitivesIntegerEquals: {
21+
KeysFunctionMapStringEquals: {
2222
acceptors: [
2323
{
2424
state: "success",
2525
matcher: {
2626
output: {
27-
path: "keys(primitives)",
28-
expected: "integer",
27+
path: "keys(maps.strings)",
28+
expected: "key",
2929
comparator: "anyStringEquals"
3030
}
3131
}

tests/codegen/waiter-tests/src/test/kotlin/com/test/FunctionKeysTest.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55

66
package com.test
77

8+
import com.test.model.EntityMaps
89
import com.test.model.EntityPrimitives
910
import com.test.model.GetFunctionKeysEqualsRequest
1011
import com.test.model.GetFunctionKeysEqualsResponse
1112
import com.test.utils.successTest
12-
import com.test.waiters.waitUntilKeysFunctionPrimitivesIntegerEquals
13+
import com.test.waiters.waitUntilKeysFunctionMapStringEquals
1314
import com.test.waiters.waitUntilKeysFunctionPrimitivesStringEquals
1415
import kotlin.test.Test
1516

@@ -22,9 +23,9 @@ class FunctionKeysTest {
2223
)
2324

2425
@Test
25-
fun testKeysFunctionPrimitivesIntegerEquals() = successTest(
26+
fun testKeysFunctionMapStringEquals() = successTest(
2627
GetFunctionKeysEqualsRequest { name = "test" },
27-
WaitersTestClient::waitUntilKeysFunctionPrimitivesIntegerEquals,
28-
GetFunctionKeysEqualsResponse { primitives = EntityPrimitives { } },
28+
WaitersTestClient::waitUntilKeysFunctionMapStringEquals,
29+
GetFunctionKeysEqualsResponse { maps = EntityMaps { strings = mapOf("key" to "value") } },
2930
)
3031
}

0 commit comments

Comments
 (0)