Skip to content

Commit

Permalink
Merge pull request #9 from respawn-app/1.1.2
Browse files Browse the repository at this point in the history
1.1.2: Fixes for overload resolution and docs
  • Loading branch information
Nek-12 authored Aug 7, 2023
2 parents c4c3936 + 3d46ce1 commit d28a5ee
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,19 @@ public inline fun <T> ApiResult<ApiResult<T>>.unwrap(): ApiResult<T> = fold(
* @see mapError
* @see mapLoading
*/
public inline infix fun <T, R> ApiResult<T>.mapWrapping(block: (T) -> R): ApiResult<R> =
public inline infix fun <T, R> ApiResult<T>.tryMap(block: (T) -> R): ApiResult<R> =
map { ApiResult { block(it) } }.unwrap()

/**
* Change the type of successful result to [R], also wrapping [block]
* in another result then folding it (handling exceptions)
* @see map
* @see mapError
* @see mapLoading
*/
@Deprecated("use tryMap", ReplaceWith("this.tryMap<T,R>(block)"))
public inline infix fun <T, R> ApiResult<T>.mapWrapping(block: (T) -> R): ApiResult<R> = tryMap(block)

/**
* Make this result an [Error] if [Success] value was null.
* @see errorIfNot
Expand All @@ -368,40 +378,29 @@ public inline fun <T> ApiResult<T>.nullOnError(): ApiResult<T?> = if (this is Er

/**
* Recover from an exception of type [R], else no-op.
* Does not affect [Loading]
* @see recoverIf
*/
public inline infix fun <reified T : Exception, R> ApiResult<R>.recover(block: (T) -> R): ApiResult<R> {
contract {
callsInPlace(block, InvocationKind.AT_MOST_ONCE)
}
return when (this) {
is Success, is Loading -> this
is Error -> if (e is T) Success(block(e)) else this
}
}

/**
* Recover from an exception of type [R], else no-op.
* Does not affect [Loading]
* Does not affect [Loading].
*
* Overload for a lambda that already returns an [ApiResult].
* @see recover
*/
@JvmName("recoverResulting")
@OverloadResolutionByLambdaReturnType
public inline infix fun <reified T : Exception, R> ApiResult<R>.recover(block: (T) -> ApiResult<R>): ApiResult<R> =
public inline infix fun <reified T : Exception, R> ApiResult<R>.recover(another: (e: T) -> ApiResult<R>): ApiResult<R> =
when (this) {
is Success, is Loading -> this
is Error -> if (e is T) block(e) else this
is Error -> if (e is T) another(e) else this
}

@Deprecated("use tryRecover", ReplaceWith("this.tryRecover<T, R>(block)"))
public inline infix fun <reified T : Exception, R> ApiResult<R>.recoverWrapping(
block: (T) -> R
): ApiResult<R> = tryRecover<T, R>(block)

/**
* calls [recover] catching and wrapping any exceptions thrown inside [block].
*/
public inline infix fun <reified T : Exception, R> ApiResult<R>.recoverWrapping(block: (T) -> R): ApiResult<R> =
public inline infix fun <reified T : Exception, R> ApiResult<R>.tryRecover(block: (T) -> R): ApiResult<R> =
when (this) {
is Success, is Loading -> this
is Error -> if (e is T) ApiResult { Success(block(e)) }.unwrap() else this
is Error -> if (e is T) ApiResult { block(e) } else this
}

/**
Expand Down Expand Up @@ -430,8 +429,7 @@ public inline fun <T> ApiResult<T>.recoverIf(
* Effectively, requires for another [ApiResult] to succeed before proceeding with this one.
* @see [ApiResult.then]
*/
@OverloadResolutionByLambdaReturnType
public inline infix fun <T, R> ApiResult<T>.chain(another: (T) -> ApiResult<R>): ApiResult<T> {
public inline infix fun <T> ApiResult<T>.chain(another: (T) -> ApiResult<*>): ApiResult<T> {
contract {
callsInPlace(another, InvocationKind.AT_MOST_ONCE)
}
Expand All @@ -449,12 +447,12 @@ public inline infix fun <T, R> ApiResult<T>.chain(another: (T) -> ApiResult<R>):
*
* If the result is success, continue (**the result of calling [block] is discarded**).
* If the result is an error, propagate it to [this].
* Alias for chain() for calls that do not return an ApiResult already
*
* Alias for [chain] for calls that do not return an ApiResult already.
* @see [ApiResult.chain]
* @see [ApiResult.then]
*/
@JvmName("chainResulting")
public inline fun <T> ApiResult<T>.chain(block: (T) -> Unit): ApiResult<T> =
public inline fun <T> ApiResult<T>.tryChain(block: (T) -> Unit): ApiResult<T> =
chain(another = { ApiResult { block(it) } })

/**
Expand Down
21 changes: 9 additions & 12 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,22 @@ subprojects {
}

tasks {
// TODO: https://github.com/Kotlin/dokka/issues/2977
val taskClass =
"org.jetbrains.kotlin.gradle.targets.native.internal.CInteropMetadataDependencyTransformationTask"
withType(Class.forName(taskClass) as Class<Task>) {
onlyIf {
val executed = gradle.taskGraph.allTasks.none { it is AbstractDokkaTask }
if (!executed) println("w: Disabling CInteropCommonization")
executed
withType<AbstractDokkaTask> {
val className =
"org.jetbrains.kotlin.gradle.targets.native.internal.CInteropMetadataDependencyTransformationTask"

@Suppress("UNCHECKED_CAST")
val taskClass = Class.forName(className) as Class<Task>
parent?.subprojects?.forEach {
dependsOn(it.tasks.withType(taskClass))
}
}

register<org.gradle.jvm.tasks.Jar>("dokkaJavadocJar") {
dependsOn(dokkaJavadoc)
from(dokkaJavadoc.flatMap { it.outputDirectory })
archiveClassifier.set("javadoc")
}
register<org.gradle.jvm.tasks.Jar>("emptyJavadocJar") {
archiveClassifier.set("javadoc")
}
}
}

Expand Down Expand Up @@ -90,7 +88,6 @@ versionCatalogUpdate {
tasks {
dokkaHtmlMultiModule.configure {
moduleName.set(rootProject.name)
outputDirectory.set(buildDir.resolve("dokka"))
}
// needed to generate compose compiler reports. See /scripts
withType<io.gitlab.arturbosch.detekt.Detekt>().configureEach {
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ object Config {

const val majorRelease = 1
const val minorRelease = 1
const val patch = 1
const val patch = 2
const val versionName = "$majorRelease.$minorRelease.$patch"

// kotlin
Expand Down
3 changes: 1 addition & 2 deletions buildSrc/src/main/kotlin/ConfigurePublication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ import org.gradle.plugins.signing.Sign
fun Project.publishMultiplatform() {
val properties = gradleLocalProperties(rootDir)
val isReleaseBuild = properties["release"]?.toString().toBoolean()
// TODO: Dokka does not support javadocs for multiplatform dependencies
val javadocJar = tasks.named("emptyJavadocJar")
val javadocJar = tasks.named("dokkaJavadocJar")

afterEvaluate {
requireNotNull(extensions.findByType<PublishingExtension>()).apply {
Expand Down

0 comments on commit d28a5ee

Please sign in to comment.