Skip to content

Commit a0aa496

Browse files
committed
misc: pr feedback v1
1 parent 4fa6596 commit a0aa496

File tree

4 files changed

+43
-7
lines changed

4 files changed

+43
-7
lines changed

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ import software.amazon.smithy.model.shapes.*
2727

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

30+
/*
31+
JMESPath has the concept of objects.
32+
This visitor assumes JMESPath objects will be Kotlin objects/classes.
33+
The smithy spec contains an instance where it's assumed a JMESPath object will be a Kotlin map.
34+
35+
Specifically it's the keys function
36+
Smithy spec: https://smithy.io/2.0/additional-specs/rules-engine/parameters.html#smithy-rules-operationcontextparams-trait
37+
JMESPath spec: https://jmespath.org/specification.html#keys
38+
39+
TODO: Proactively support JMESPath objects as Kotlin maps throughout thr entire visitor if more instances of this behavior start popping up
40+
*/
41+
3042
/**
3143
* An [ExpressionVisitor] used for traversing a JMESPath expression to generate code for traversing an equivalent
3244
* modeled object. This visitor is passed to [JmespathExpression.accept], at which point specific expression methods
@@ -524,14 +536,16 @@ class KotlinJmespathExpressionVisitor(
524536
".$expr"
525537
}
526538

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
539+
/*
540+
Smithy spec expects a map, JMESPath spec expects an object
541+
Smithy spec: https://smithy.io/2.0/additional-specs/rules-engine/parameters.html#smithy-rules-operationcontextparams-trait
542+
JMESPath spec: https://jmespath.org/specification.html#keys
543+
*/
529544
private fun VisitedExpression.getKeys(): String =
530-
if (this.shape?.targetOrSelf(ctx.model)?.type == ShapeType.MAP) {
531-
"${this.identifier}?.keys?.toList()"
545+
if (shape?.targetOrSelf(ctx.model)?.type == ShapeType.MAP) {
546+
"$identifier?.keys?.map { it.toString() }?.toList()"
532547
} else {
533-
this
534-
.shape
548+
shape
535549
?.targetOrSelf(ctx.model)
536550
?.allMembers
537551
?.keys

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ class OperationContextParamsTest {
135135
@Suppress("UNCHECKED_CAST")
136136
val input = request.context[HttpOperationContext.OperationInput] as TestOperationRequest
137137
val object = input.object
138-
val keys = object?.keys?.toList()
138+
val keys = object?.keys?.map { it.toString() }?.toList()
139139
builder.foo = keys
140140
""".formatForTest(" ")
141141

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@ use smithy.waiters#waitable
1818
}
1919
]
2020
},
21+
KeysFunctionPrimitivesIntegerEquals: {
22+
acceptors: [
23+
{
24+
state: "success",
25+
matcher: {
26+
output: {
27+
path: "keys(primitives)",
28+
expected: "integer",
29+
comparator: "anyStringEquals"
30+
}
31+
}
32+
}
33+
]
34+
},
2135
KeysFunctionMapStringEquals: {
2236
acceptors: [
2337
{

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import com.test.model.GetFunctionKeysEqualsRequest
1111
import com.test.model.GetFunctionKeysEqualsResponse
1212
import com.test.utils.successTest
1313
import com.test.waiters.waitUntilKeysFunctionMapStringEquals
14+
import com.test.waiters.waitUntilKeysFunctionPrimitivesIntegerEquals
1415
import com.test.waiters.waitUntilKeysFunctionPrimitivesStringEquals
1516
import kotlin.test.Test
1617

@@ -22,6 +23,13 @@ class FunctionKeysTest {
2223
GetFunctionKeysEqualsResponse { primitives = EntityPrimitives { } },
2324
)
2425

26+
@Test
27+
fun testKeysFunctionPrimitivesIntegerEquals() = successTest(
28+
GetFunctionKeysEqualsRequest { name = "test" },
29+
WaitersTestClient::waitUntilKeysFunctionPrimitivesIntegerEquals,
30+
GetFunctionKeysEqualsResponse { primitives = EntityPrimitives { } },
31+
)
32+
2533
@Test
2634
fun testKeysFunctionMapStringEquals() = successTest(
2735
GetFunctionKeysEqualsRequest { name = "test" },

0 commit comments

Comments
 (0)