Skip to content

Commit

Permalink
fix: create JMESPATH flattenIfPossible functions for Lists (#1169)
Browse files Browse the repository at this point in the history
  • Loading branch information
0marperez authored Dec 16, 2024
1 parent 4886087 commit a4734c7
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,20 @@ public fun Any?.type(): String = when (this) {
else -> throw Exception("Undetected type for: $this")
}

// Collection `flattenIfPossible` functions
@InternalApi
@JvmName("noOpUnnestedCollection")
public inline fun <reified T> Collection<T>.flattenIfPossible(): Collection<T> = this

@InternalApi
@JvmName("flattenNestedCollection")
public inline fun <reified T> Collection<Collection<T>>.flattenIfPossible(): Collection<T> = flatten()

// List `flattenIfPossible` functions
@InternalApi
@JvmName("noOpUnnestedCollection")
public inline fun <reified T> List<T>.flattenIfPossible(): List<T> = this

@InternalApi
@JvmName("flattenNestedCollection")
public inline fun <reified T> List<List<T>>.flattenIfPossible(): List<T> = flatten()
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package aws.smithy.kotlin.runtime.util

import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class JmesPathTest {
@Test
fun flattenNestedLists() {
val nestedList = listOf(
listOf(1, 2, 3),
listOf(4, 5),
listOf(6),
)
val flattenedList = nestedList.flattenIfPossible()
assertEquals(listOf(1, 2, 3, 4, 5, 6), flattenedList)
}

@Test
fun flattenEmptyNestedLists() {
val nestedList = listOf(
listOf<Int>(),
listOf(),
listOf(),
)
val flattenedList = nestedList.flattenIfPossible()
assertTrue(flattenedList.isEmpty())
}

@Test
fun flattenNestedEmptyAndNonEmptyNestedLists() {
val nestedList = listOf(
listOf(1, 2),
listOf(),
listOf(3, 4, 5),
)
val flattenedList = nestedList.flattenIfPossible()
assertEquals(listOf(1, 2, 3, 4, 5), flattenedList)
}

@Test
fun flattenList() {
val nestedList = listOf(
listOf(1, 2, 3),
)
val flattenedList = nestedList.flattenIfPossible()
assertEquals(listOf(1, 2, 3), flattenedList)
}
}

0 comments on commit a4734c7

Please sign in to comment.