-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'kotlin-1.6.10' into kotlin-1.6.10-release
v0.1.2-kotlin-1.6.10 release
- Loading branch information
Showing
23 changed files
with
448 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,18 @@ | ||
# Huanshankeji Kotlin Common | ||
|
||
[](https://search.maven.org/search?q=g:com.huanshankeji%20a:kotlin-common-*) | ||
|
||
Huanshankeji's common code libraries in Kotlin | ||
|
||
These include a core library to extend the Kotlin language and its standard library, and extension libraries for various Kotlin and Java libraries such as [Λrrow](https://arrow-kt.io/), Coroutines ([docs here](https://kotlinlang.org/docs/coroutines-overview.html) and [repository here](https://github.com/Kotlin/kotlinx.coroutines)), [Exposed](https://github.com/JetBrains/Exposed), [Ktor](https://ktor.io/), Serialization([docs here](https://kotlinlang.org/docs/serialization.html) and [repository here](https://github.com/Kotlin/kotlinx.serialization)), [Vert.x](https://vertx.io/), etc. | ||
These include a core library to extend the Kotlin language and its standard library, and extension libraries for various | ||
Kotlin and Java libraries such as [Λrrow](https://arrow-kt.io/), | ||
Coroutines ([docs here](https://kotlinlang.org/docs/coroutines-overview.html) | ||
and [repository here](https://github.com/Kotlin/kotlinx.coroutines)), [Exposed](https://github.com/JetBrains/Exposed) | ||
, [Ktor](https://ktor.io/), Serialization([docs here](https://kotlinlang.org/docs/serialization.html) | ||
and [repository here](https://github.com/Kotlin/kotlinx.serialization)), [Vert.x](https://vertx.io/), etc. | ||
|
||
## Maven coordinates | ||
|
||
```kotlin | ||
"com.huanshankeji:kotlin-common-$module:$version" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import com.huanshankeji.DefaultVersions | ||
|
||
val projectVersion = "0.1.1-kotlin-1.6.10" | ||
val projectVersion = "0.1.2-kotlin-1.6.10" | ||
|
||
val kotlinVersion = DefaultVersions.kotlin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
vertx/src/main/kotlin/com/huanshankeji/vertx/ext/web/RoutingContext.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.huanshankeji.vertx.ext.web | ||
|
||
import io.vertx.ext.web.RoutingContext | ||
|
||
/** | ||
* Runs the [block] and calls [RoutingContext.fail] if a [Throwable] is thrown. | ||
*/ | ||
inline fun RoutingContext.checkedRun(block: () -> Unit): Unit = | ||
try { | ||
block() | ||
} catch (t: Throwable) { | ||
fail(t) | ||
} |
15 changes: 14 additions & 1 deletion
15
vertx/src/main/kotlin/com/huanshankeji/vertx/ext/web/VirtualHostCombinable.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,20 @@ | ||
package com.huanshankeji.vertx.ext.web | ||
|
||
import io.vertx.core.Verticle | ||
import io.vertx.ext.web.Router | ||
|
||
interface VirtualHostCombinable { | ||
fun Router.routesOnVirtualHost() | ||
} | ||
} | ||
|
||
interface SingleVirtualHostVerticle : VirtualHostCombinable, Verticle { | ||
override fun Router.routesOnVirtualHost() { | ||
mountSubRouter("/", subRouter()).virtualHost(virtualHost) | ||
} | ||
|
||
val virtualHost: String | ||
|
||
fun subRouter(): Router = Router.router(vertx).apply { virtualHostSubRouter() } | ||
|
||
fun Router.virtualHostSubRouter() | ||
} |
7 changes: 0 additions & 7 deletions
7
vertx/src/main/kotlin/com/huanshankeji/vertx/kotlin/RoutingContext.kt
This file was deleted.
Oops, something went wrong.
13 changes: 0 additions & 13 deletions
13
vertx/src/main/kotlin/com/huanshankeji/vertx/kotlin/coroutines/CoroutineHandler.kt
This file was deleted.
Oops, something went wrong.
25 changes: 0 additions & 25 deletions
25
vertx/src/main/kotlin/com/huanshankeji/vertx/kotlin/coroutines/Coroutines.kt
This file was deleted.
Oops, something went wrong.
44 changes: 43 additions & 1 deletion
44
vertx/src/main/kotlin/com/huanshankeji/vertx/kotlin/coroutines/VertxCoroutine.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,64 @@ | ||
package com.huanshankeji.vertx.kotlin.coroutines | ||
|
||
import com.huanshankeji.kotlin.use | ||
import io.vertx.core.Future | ||
import io.vertx.core.Promise | ||
import io.vertx.core.Vertx | ||
import io.vertx.kotlin.coroutines.await | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.CoroutineStart | ||
import kotlinx.coroutines.coroutineScope | ||
import kotlinx.coroutines.launch | ||
import kotlin.coroutines.CoroutineContext | ||
import kotlin.coroutines.EmptyCoroutineContext | ||
|
||
/** | ||
* Execute the [block] code and close the [Vertx] instance like [kotlin.use] on an [AutoCloseable]. | ||
*/ | ||
suspend inline fun <R> Vertx.use(block: (Vertx) -> R): R = | ||
use(block) { close().await() } | ||
@Suppress("MoveLambdaOutsideParentheses") | ||
use(block, { close().await() }) | ||
|
||
/** | ||
* Execute [blockingCode] that returns the a [T] instance with [Vertx.executeBlocking] | ||
* and awaits its completion. | ||
* | ||
* Compared to [Vertx.executeBlocking]'s `blockingCodeHandler` argument, | ||
* [blockingCode] returns the result when the operation completes instead of calling [Promise.complete]. | ||
*/ | ||
suspend fun <T> Vertx.awaitExecuteBlocking(blockingCode: () -> T): T = | ||
executeBlocking<T> { | ||
it.complete(blockingCode()) | ||
}.await() | ||
|
||
/** | ||
* Like [awaitExecuteBlocking] but [blockingCode] is a suspend function. | ||
*/ | ||
@Suppress("DeprecatedCallableAddReplaceWith") | ||
@Deprecated("This implementation is buggy. See https://github.com/vert-x3/vertx-lang-kotlin/pull/222/commits/fc3c5c5cc0c572eaddb3c2c37d07c696f75b4443#diff-162b76dc534138518a237d9a8ed527f1b3ecaca67385ea7d4357b6eff203f699R138-R217 for a fixed proposed version.") | ||
suspend fun <T> Vertx.awaitSuspendExecuteBlocking(blockingCode: suspend () -> T): T = | ||
coroutineScope { | ||
executeBlocking<T> { | ||
launch { it.complete(blockingCode()) } | ||
}.await() | ||
} | ||
|
||
/** | ||
* Launch a coroutine and converts it into a [Future] | ||
* that completes when the suspended function returns and fails if it throws. | ||
*/ | ||
fun <T> CoroutineScope.coroutineToFuture( | ||
context: CoroutineContext = EmptyCoroutineContext, | ||
start: CoroutineStart = CoroutineStart.DEFAULT, | ||
block: suspend CoroutineScope.() -> T | ||
): Future<T> { | ||
val promise = Promise.promise<T>() | ||
launch(context, start) { | ||
try { | ||
promise.complete(block()) | ||
} catch (t: Throwable) { | ||
promise.fail(t) | ||
} | ||
} | ||
return promise.future() | ||
} |
23 changes: 19 additions & 4 deletions
23
...n/coroutines/ExtendedCoroutineVerticle.kt → ...s/ext/web/ExtendedWebCoroutineVerticle.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
vertx/src/main/kotlin/com/huanshankeji/vertx/kotlin/coroutines/ext/web/RoutingContext.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.huanshankeji.vertx.kotlin.coroutines.ext.web | ||
|
||
import com.huanshankeji.vertx.ext.web.checkedRun | ||
import io.vertx.ext.web.RoutingContext | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.CoroutineStart | ||
import kotlinx.coroutines.launch | ||
import kotlin.coroutines.CoroutineContext | ||
import kotlin.coroutines.EmptyCoroutineContext | ||
|
||
/** | ||
* Launches a coroutine like [launch], and calls [RoutingContext.fail] on [ctx] if a [Throwable] is thrown in [block]. | ||
*/ | ||
inline fun CoroutineScope.launchChecked( | ||
ctx: RoutingContext, | ||
context: CoroutineContext = EmptyCoroutineContext, | ||
start: CoroutineStart = CoroutineStart.DEFAULT, | ||
crossinline block: suspend CoroutineScope.() -> Unit | ||
) = | ||
launch(context, start) { | ||
ctx.checkedRun { block() } | ||
} |
12 changes: 12 additions & 0 deletions
12
vertx/src/main/kotlin/com/huanshankeji/vertx/kotlin/ext/web/RoutingContext.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.huanshankeji.vertx.kotlin.ext.web | ||
|
||
import io.vertx.ext.web.RoutingContext | ||
|
||
/** | ||
* A `set` operator overloading extension function | ||
* as idiomatic Kotlin syntactic sugar to [RoutingContext.put]. | ||
*/ | ||
@Suppress("NOTHING_TO_INLINE") | ||
inline operator fun RoutingContext.set(key: String, value: Any?) { | ||
put(key, value) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
vertx/src/test/kotlin/com/huanshankeji/kotlinx/coroutines/test/TestScope.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.huanshankeji.kotlinx.coroutines.test | ||
|
||
import kotlinx.coroutines.test.TestScope | ||
import kotlinx.coroutines.test.currentTime | ||
import kotlin.system.measureTimeMillis | ||
|
||
/** | ||
* @see measureTimeMillis | ||
* @see TestScope.currentTime | ||
*/ | ||
inline fun TestScope.measureVirtualTime(block: () -> Unit): Long { | ||
val start = currentTime | ||
block() | ||
return currentTime - start | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package com.huanshankeji.test | ||
|
||
const val DEFAULT_SLEEP_OR_DELAY_DURATION = 1000L |
19 changes: 19 additions & 0 deletions
19
vertx/src/test/kotlin/com/huanshankeji/vertx/VertxBaseTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.huanshankeji.vertx | ||
|
||
import io.vertx.core.Vertx | ||
import io.vertx.ext.unit.junit.RunTestOnContext | ||
import org.junit.Rule | ||
import kotlin.test.BeforeTest | ||
|
||
abstract class VertxBaseTest { | ||
@Rule | ||
@JvmField | ||
val rule = RunTestOnContext() | ||
|
||
protected lateinit var vertx: Vertx | ||
|
||
@BeforeTest | ||
fun initVertx() { | ||
vertx = rule.vertx() | ||
} | ||
} |
Oops, something went wrong.