Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add safeCast operator #181

Merged
merged 8 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

### Changed

### Added

- Add `Flow.safeCast` operator.
hoangchungk53qx1 marked this conversation as resolved.
Show resolved Hide resolved

hoangchungk53qx1 marked this conversation as resolved.
Show resolved Hide resolved
- Update dependencies
- `Gradle` to `8.3`.

Expand Down
34 changes: 30 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,10 @@ dependencies {
- Intermediate operators
- [`bufferCount`](#buffercount)
- [`combine`](#combine)
- [`cast`](#cast--castnotnull--castnullable)
- [`castNotNull`](#cast--castnotnull--castnullable)
- [`castNullable`](#cast--castnotnull--castnullable)
- [`cast`](#cast--castnotnull--castnullable--safeCast)
- [`castNotNull`](#cast--castnotnull--castnullable--safeCast)
- [`castNullable`](#cast--castnotnull--castnullable--safeCast)
- [`safeCast`](#cast--castnotnull--castnullable--safeCast)
- [`concatWith`](#concatwith)
- [`startWith`](#startwith)
- [`flatMapFirst`](#flatmapfirst--exhaustmap)
Expand Down Expand Up @@ -432,7 +433,7 @@ timer: kotlin.Unit

----

#### cast / castNotNull / castNullable
#### cast / castNotNull / castNullable / safeCast

- Similar
to [RxJava cast](http://reactivex.io/RxJava/3.x/javadoc/io/reactivex/rxjava3/core/Flowable.html#cast-java.lang.Class-)
Expand Down Expand Up @@ -487,6 +488,31 @@ castNotNull: 3

----


##### safeCast

Adapt this `Flow<*>` to be a `Flow<R?>`.

At the collection time, if this `Flow` has any value that is not an instance of R, null will be emitted.

```kotlin
flowOf<Any?>(1, 2, 3, "Kotlin", null)
.safeCast<Int?>()
.collect { v: Int? -> println("safeCast: $v") }
```

Output:

```none
safeCast: 1
safeCast: 2
safeCast: 3
safeCast: null
safeCast: null
```

----

#### concatWith

- Similar to [RxJS concatWith](https://rxjs.dev/api/operators/concatWith)
Expand Down
6 changes: 6 additions & 0 deletions src/commonMain/kotlin/com/hoc081098/flowext/cast.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,9 @@ public inline fun <reified T : Any> Flow<T?>.castNotNull(): Flow<T> = map { it!!
*/
@Suppress("NOTHING_TO_INLINE")
public inline fun <T> Flow<T>.castNullable(): Flow<T?> = this

/**
* Adapt this `Flow<*>` to be a `Flow<R?>`.
hoangchungk53qx1 marked this conversation as resolved.
Show resolved Hide resolved
*/
hoangchungk53qx1 marked this conversation as resolved.
Show resolved Hide resolved
@Suppress("NOTHING_TO_INLINE")
public inline fun <reified R> Flow<*>.safeCast(): Flow<R?> = map { it as? R }
46 changes: 46 additions & 0 deletions src/commonTest/kotlin/com/hoc081098/flowext/CastTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ import com.hoc081098.flowext.utils.BaseTest
import com.hoc081098.flowext.utils.assertFailsWith
import com.hoc081098.flowext.utils.test
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertIs
import kotlin.test.assertSame
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.toList

@ExperimentalCoroutinesApi
class CastTest : BaseTest() {
Expand Down Expand Up @@ -96,4 +98,48 @@ class CastTest : BaseTest() {
val flow = flowOf(1, 2, 3)
assertSame(flow.castNullable(), flow)
}

@Test
fun testSafeCastSuccess() = runTest {
assertIs<Flow<Int?>>(
flowOf<Any?>(1, 2, 3, "hello").safeCast<Int?>(),
)

assertIs<Flow<Int?>>(
flowOf<Any?>(1, 2, 3, "kotlin", null)
.safeCast<Int?>(),
)
.test(
listOf(
Event.Value(1),
Event.Value(2),
Event.Value(3),
Event.Value(null),
Event.Value(null),
Event.Complete,
),
)
}

@Test
fun testSafeCast() {
val stringFlow: Flow<String?> = flowOf("Hello", 42, "World", 123, "Kotlin").safeCast()
assertIs<Flow<String?>>(stringFlow)

kotlinx.coroutines.test.runTest {
assertContentEquals(
listOf("Hello", null, "World", null, "Kotlin"),
stringFlow.toList(),
)
}
val intFlow: Flow<Int?> = flowOf(1, 2, 3, null).safeCast()
assertIs<Flow<Int?>>(intFlow)

kotlinx.coroutines.test.runTest {
assertContentEquals(
listOf(1, 2, 3, null),
intFlow.toList(),
)
}
}
hoangchungk53qx1 marked this conversation as resolved.
Show resolved Hide resolved
}