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 4 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 @@ -2,6 +2,10 @@

## [Unreleased] - TODO

### 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
### Changed

- Update dependencies
Expand Down
40 changes: 36 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,37 @@ castNotNull: 3

----


##### safeCast

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

At the collection time, converts a Flow<*> to a Flow<R?>, if not consistent with R, returns null.
hoangchungk53qx1 marked this conversation as resolved.
Show resolved Hide resolved

```kotlin
flowOf<Any?>(1, 2, 3, "Kotlin", null)
.safeCast<Int?>()
.collect { v: Int? ->
if (v != null) {
println("safeCast: $v")
} else {
println("Null value encountered.")
}
}
hoangchungk53qx1 marked this conversation as resolved.
Show resolved Hide resolved
```

Output:

```none
safeCast: 1
safeCast: 2
safeCast: 3
Null value encountered.
Null value encountered.
hoangchungk53qx1 marked this conversation as resolved.
Show resolved Hide resolved
```

----

#### 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 }
29 changes: 29 additions & 0 deletions src/commonTest/kotlin/com/hoc081098/flowext/CastTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,33 @@ class CastTest : BaseTest() {
val flow = flowOf(1, 2, 3)
assertSame(flow.castNullable(), flow)
}

@Test
fun testSafeCastSuccess() = runTest {
assertIs<Flow<Int>>(
hoangchungk53qx1 marked this conversation as resolved.
Show resolved Hide resolved
flowOf<Any?>(1, 2, 3, "hello").safeCast<Int?>(),
)

assertIs<Flow<Int>>(
hoangchungk53qx1 marked this conversation as resolved.
Show resolved Hide resolved
flowOf<Int?>(1, 2, 3)
hoangchungk53qx1 marked this conversation as resolved.
Show resolved Hide resolved
.safeCast<Int?>(),
)
.test(
listOf(
Event.Value(1),
Event.Value(2),
Event.Value(3),
Event.Complete,
),
)
}

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

val intFlow: Flow<Int?> = flowOf(1, 2, 3, null).safeCast()
assertIs<Flow<Int?>>(intFlow)
hoangchungk53qx1 marked this conversation as resolved.
Show resolved Hide resolved
}
}