Skip to content

Commit

Permalink
Bug fixes, negative indexing and more robust tests
Browse files Browse the repository at this point in the history
  • Loading branch information
0marperez committed Aug 11, 2023
1 parent 31a6db0 commit 9d2ae0a
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import software.amazon.smithy.kotlin.codegen.model.traits.OperationOutput
import software.amazon.smithy.kotlin.codegen.utils.dq
import software.amazon.smithy.kotlin.codegen.utils.getOrNull
import software.amazon.smithy.kotlin.codegen.utils.toCamelCase
import software.amazon.smithy.model.knowledge.NullableIndex
import software.amazon.smithy.model.shapes.ListShape
import software.amazon.smithy.model.shapes.MapShape
import software.amazon.smithy.model.shapes.MemberShape
Expand Down Expand Up @@ -51,8 +50,6 @@ class KotlinJmespathExpressionVisitor(
) : ExpressionVisitor<VisitedExpression> {
private val tempVars = mutableSetOf<String>()

private val nullableIndex = NullableIndex(ctx.model)

// tracks the current shape on which the visitor is operating
private val shapeCursor = ArrayDeque(listOf(shape))

Expand Down Expand Up @@ -335,8 +332,11 @@ class KotlinJmespathExpressionVisitor(
}
}

private fun index(expression: IndexExpression, parentName: String): VisitedExpression =
VisitedExpression(addTempVar("index", "$parentName?.get(${expression.index})"))
private fun index(expression: IndexExpression, parentName: String): VisitedExpression {
shapeCursor.addLast(currentShape.targetOrSelf(ctx.model).targetMemberOrSelf)
val index = if (expression.index < 0) "$parentName.size${expression.index}" else expression.index
return VisitedExpression(addTempVar("index", "$parentName?.get($index)"))
}

private val Shape.isEnumList: Boolean
get() = this is ListShape && ctx.model.expectShape(member.target).isEnum
Expand All @@ -356,8 +356,7 @@ class KotlinJmespathExpressionVisitor(

private val Shape.isNullable: Boolean
get() = this is MemberShape &&
ctx.model.expectShape(target).let { !it.hasTrait<OperationInput>() && !it.hasTrait<OperationOutput>() } &&
nullableIndex.isMemberNullable(this, NullableIndex.CheckMode.CLIENT_ZERO_VALUE_V1_NO_INPUT)
ctx.model.expectShape(target).let { !it.hasTrait<OperationInput>() && !it.hasTrait<OperationOutput>() }

private val Shape.targetMemberOrSelf: Shape
get() = when (val target = targetOrSelf(ctx.model)) {
Expand Down
42 changes: 42 additions & 0 deletions tests/codegen/waiter-tests/model/waiter-operations.smithy
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,20 @@ service WaitersTestService {
}
]
},
BooleanListIndexNegativeTwoEquals: {
acceptors: [
{
state: "success",
matcher: {
output: {
path: "lists.booleans[-2]",
expected: "true",
comparator: "booleanEquals"
}
}
}
]
},
TwoDimensionalBooleanListIndexZeroZeroEquals: {
acceptors: [
{
Expand All @@ -236,6 +250,34 @@ service WaitersTestService {
}
]
},
StructListIndexOneStringsIndexZeroEquals: {
acceptors: [
{
state: "success",
matcher: {
output: {
path: "lists.structs[1].strings[0]",
expected: "foo",
comparator: "stringEquals"
}
}
}
]
},
StructListIndexOneSubStructsIndexZeroSubStructPrimitivesBooleanEquals: {
acceptors: [
{
state: "success",
matcher: {
output: {
path: "lists.structs[1].subStructs[0].subStructPrimitives.boolean",
expected: "true",
comparator: "booleanEquals"
}
}
}
]
},

// anyStringEquals
StringListAnyStringEquals: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,15 @@ class WaiterTest {
@Test fun testBooleanListIndexOneEquals() = successTest(
WaitersTestClient::waitUntilBooleanListIndexOneEquals,
GetEntityResponse { lists = EntityLists { booleans = listOf(false, false) } },
GetEntityResponse { lists = EntityLists { booleans = listOf(true, true) } },
GetEntityResponse { lists = EntityLists { booleans = listOf(true, false) } },
GetEntityResponse { lists = EntityLists { booleans = listOf(false, true) } },
)

@Test fun testBooleanListIndexNegativeTwoEquals() = successTest(
WaitersTestClient::waitUntilBooleanListIndexNegativeTwoEquals,
GetEntityResponse { lists = EntityLists { booleans = listOf(false, false) } },
GetEntityResponse { lists = EntityLists { booleans = listOf(false, true) } },
GetEntityResponse { lists = EntityLists { booleans = listOf(true, false) } },
)

@Test fun testTwoDimensionalBooleanListIndexZeroZeroEquals() = successTest(
Expand All @@ -127,6 +135,18 @@ class WaiterTest {
GetEntityResponse { twoDimensionalLists = TwoDimensionalEntityLists { booleansList = listOf(listOf(true)) } },
)

@Test fun testStructListIndexOneStringsIndexZeroEquals() = successTest(
WaitersTestClient::waitUntilStructListIndexOneStringsIndexZeroEquals,
GetEntityResponse { lists = EntityLists { structs = listOf(Struct { strings = listOf("bar") }, Struct { strings = listOf("bar") }) } },
GetEntityResponse { lists = EntityLists { structs = listOf(Struct { strings = listOf("bar") }, Struct { strings = listOf("foo") }) } },
)

@Test fun testStructListIndexOneSubStructsIndexZeroSubStructPrimitivesBooleanEquals() = successTest(
WaitersTestClient::waitUntilStructListIndexOneSubStructsIndexZeroSubStructPrimitivesBooleanEquals,
GetEntityResponse { lists = EntityLists { structs = listOf(Struct { }, Struct { subStructs = listOf(SubStruct { subStructPrimitives = EntityPrimitives { boolean = false } }) }) } },
GetEntityResponse { lists = EntityLists { structs = listOf(Struct { }, Struct { subStructs = listOf(SubStruct { subStructPrimitives = EntityPrimitives { boolean = true } }) }) } },
)

// anyStringEquals
@Test fun testStringListAnyListStringEquals() = successTest(
WaitersTestClient::waitUntilStringListAnyStringEquals,
Expand Down

0 comments on commit 9d2ae0a

Please sign in to comment.