Skip to content

Commit a4734c7

Browse files
authored
fix: create JMESPATH flattenIfPossible functions for Lists (#1169)
1 parent 4886087 commit a4734c7

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/util/JMESPath.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,20 @@ public fun Any?.type(): String = when (this) {
6666
else -> throw Exception("Undetected type for: $this")
6767
}
6868

69+
// Collection `flattenIfPossible` functions
6970
@InternalApi
7071
@JvmName("noOpUnnestedCollection")
7172
public inline fun <reified T> Collection<T>.flattenIfPossible(): Collection<T> = this
7273

7374
@InternalApi
7475
@JvmName("flattenNestedCollection")
7576
public inline fun <reified T> Collection<Collection<T>>.flattenIfPossible(): Collection<T> = flatten()
77+
78+
// List `flattenIfPossible` functions
79+
@InternalApi
80+
@JvmName("noOpUnnestedCollection")
81+
public inline fun <reified T> List<T>.flattenIfPossible(): List<T> = this
82+
83+
@InternalApi
84+
@JvmName("flattenNestedCollection")
85+
public inline fun <reified T> List<List<T>>.flattenIfPossible(): List<T> = flatten()
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package aws.smithy.kotlin.runtime.util
2+
3+
import kotlin.test.Test
4+
import kotlin.test.assertEquals
5+
import kotlin.test.assertTrue
6+
7+
class JmesPathTest {
8+
@Test
9+
fun flattenNestedLists() {
10+
val nestedList = listOf(
11+
listOf(1, 2, 3),
12+
listOf(4, 5),
13+
listOf(6),
14+
)
15+
val flattenedList = nestedList.flattenIfPossible()
16+
assertEquals(listOf(1, 2, 3, 4, 5, 6), flattenedList)
17+
}
18+
19+
@Test
20+
fun flattenEmptyNestedLists() {
21+
val nestedList = listOf(
22+
listOf<Int>(),
23+
listOf(),
24+
listOf(),
25+
)
26+
val flattenedList = nestedList.flattenIfPossible()
27+
assertTrue(flattenedList.isEmpty())
28+
}
29+
30+
@Test
31+
fun flattenNestedEmptyAndNonEmptyNestedLists() {
32+
val nestedList = listOf(
33+
listOf(1, 2),
34+
listOf(),
35+
listOf(3, 4, 5),
36+
)
37+
val flattenedList = nestedList.flattenIfPossible()
38+
assertEquals(listOf(1, 2, 3, 4, 5), flattenedList)
39+
}
40+
41+
@Test
42+
fun flattenList() {
43+
val nestedList = listOf(
44+
listOf(1, 2, 3),
45+
)
46+
val flattenedList = nestedList.flattenIfPossible()
47+
assertEquals(listOf(1, 2, 3), flattenedList)
48+
}
49+
}

0 commit comments

Comments
 (0)