From ebce2c4b839c0b2f8be78bc31c1ce12c45a0164c Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Sat, 13 Oct 2018 03:37:40 +0200 Subject: Major refactoring of the multi-platform components. Dependencies upgrade. A lot of code style fixes. --- .../kotlin/pl/treksoft/kvision/remote/KVServer.kt | 7 +-- .../pl/treksoft/kvision/remote/ServiceManager.kt | 40 +++++++++------ .../main/kotlin/pl/treksoft/kvision/types/Date.kt | 46 +++++++++++++++++ .../main/kotlin/pl/treksoft/kvision/types/KDate.kt | 58 ---------------------- 4 files changed, 76 insertions(+), 75 deletions(-) create mode 100644 kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/types/Date.kt delete mode 100644 kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/types/KDate.kt (limited to 'kvision-modules/kvision-server-jooby') diff --git a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServer.kt b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServer.kt index 76f1ee30..38edc1b8 100644 --- a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServer.kt +++ b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServer.kt @@ -19,8 +19,6 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -@file:Suppress("EXPERIMENTAL_FEATURE_WARNING") - package pl.treksoft.kvision.remote import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper @@ -31,6 +29,7 @@ import org.jooby.Kooby import org.jooby.Session import org.jooby.json.Jackson import org.pac4j.core.profile.CommonProfile +import java.text.SimpleDateFormat import kotlinx.coroutines.async as coroutinesAsync /** @@ -42,7 +41,9 @@ actual open class KVServer(init: KVServer.() -> Unit) : Kooby() { assets("/", "index.html") @Suppress("LeakingThis") assets("/**").onMissing(0) - val mapper = jacksonObjectMapper() + val mapper = jacksonObjectMapper().apply { + dateFormat = SimpleDateFormat("YYYY-MM-DD HH:mm:ss") + } @Suppress("LeakingThis") use(Jackson(mapper)) @Suppress("LeakingThis") diff --git a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/ServiceManager.kt b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/ServiceManager.kt index b37d7319..3091ce26 100644 --- a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/ServiceManager.kt +++ b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/ServiceManager.kt @@ -28,11 +28,11 @@ import org.jooby.Response import org.jooby.Status import org.slf4j.Logger import org.slf4j.LoggerFactory +import java.text.SimpleDateFormat /** * Multiplatform service manager. */ -@Suppress("EXPERIMENTAL_FEATURE_WARNING") actual open class ServiceManager actual constructor(val service: T) { companion object { @@ -40,7 +40,9 @@ actual open class ServiceManager actual constructor(val service: T) { } protected val routes: MutableList Unit> = mutableListOf() - val mapper = jacksonObjectMapper() + val mapper = jacksonObjectMapper().apply { + dateFormat = SimpleDateFormat("YYYY-MM-DD HH:mm:ss") + } var counter: Int = 0 /** @@ -50,12 +52,13 @@ actual open class ServiceManager actual constructor(val service: T) { * @param method a HTTP method * @param prefix an URL address prefix */ + @Suppress("TooGenericExceptionCaught") protected actual inline fun bind( noinline function: T.(Request?) -> Deferred, route: String?, method: RpcHttpMethod, prefix: String ) { val routeDef = route ?: "route${this::class.simpleName}${counter++}" - routes.add({ + routes.add { call(method, "$prefix$routeDef") { req, res -> if (service != null) { val jsonRpcRequest = req.body(JsonRpcRequest::class.java) @@ -75,7 +78,7 @@ actual open class ServiceManager actual constructor(val service: T) { res.status(Status.SERVER_ERROR) } }.invoke(this) - }) + } } /** @@ -85,12 +88,13 @@ actual open class ServiceManager actual constructor(val service: T) { * @param method a HTTP method * @param prefix an URL address prefix */ + @Suppress("TooGenericExceptionCaught") protected actual inline fun bind( noinline function: T.(PAR, Request?) -> Deferred, route: String?, method: RpcHttpMethod, prefix: String ) { val routeDef = route ?: "route${this::class.simpleName}${counter++}" - routes.add({ + routes.add { call(method, "$prefix$routeDef") { req, res -> if (service != null) { val jsonRpcRequest = req.body(JsonRpcRequest::class.java) @@ -115,7 +119,7 @@ actual open class ServiceManager actual constructor(val service: T) { res.status(Status.SERVER_ERROR) } }.invoke(this) - }) + } } /** @@ -125,12 +129,13 @@ actual open class ServiceManager actual constructor(val service: T) { * @param method a HTTP method * @param prefix an URL address prefix */ + @Suppress("TooGenericExceptionCaught") protected actual inline fun bind( noinline function: T.(PAR1, PAR2, Request?) -> Deferred, route: String?, method: RpcHttpMethod, prefix: String ) { val routeDef = route ?: "route${this::class.simpleName}${counter++}" - routes.add({ + routes.add { call(method, "$prefix$routeDef") { req, res -> if (service != null) { val jsonRpcRequest = req.body(JsonRpcRequest::class.java) @@ -156,7 +161,7 @@ actual open class ServiceManager actual constructor(val service: T) { res.status(Status.SERVER_ERROR) } }.invoke(this) - }) + } } /** @@ -166,15 +171,17 @@ actual open class ServiceManager actual constructor(val service: T) { * @param method a HTTP method * @param prefix an URL address prefix */ + @Suppress("TooGenericExceptionCaught") protected actual inline fun bind( noinline function: T.(PAR1, PAR2, PAR3, Request?) -> Deferred, route: String?, method: RpcHttpMethod, prefix: String ) { val routeDef = route ?: "route${this::class.simpleName}${counter++}" - routes.add({ + routes.add { call(method, "$prefix$routeDef") { req, res -> if (service != null) { val jsonRpcRequest = req.body(JsonRpcRequest::class.java) + @Suppress("MagicNumber") if (jsonRpcRequest.params.size == 3) { val param1 = getParameter(jsonRpcRequest.params[0]) val param2 = getParameter(jsonRpcRequest.params[1]) @@ -198,7 +205,7 @@ actual open class ServiceManager actual constructor(val service: T) { res.status(Status.SERVER_ERROR) } }.invoke(this) - }) + } } /** @@ -208,15 +215,17 @@ actual open class ServiceManager actual constructor(val service: T) { * @param method a HTTP method * @param prefix an URL address prefix */ + @Suppress("TooGenericExceptionCaught") protected actual inline fun bind( noinline function: T.(PAR1, PAR2, PAR3, PAR4, Request?) -> Deferred, route: String?, method: RpcHttpMethod, prefix: String ) { val routeDef = route ?: "route${this::class.simpleName}${counter++}" - routes.add({ + routes.add { call(method, "$prefix$routeDef") { req, res -> if (service != null) { val jsonRpcRequest = req.body(JsonRpcRequest::class.java) + @Suppress("MagicNumber") if (jsonRpcRequest.params.size == 4) { val param1 = getParameter(jsonRpcRequest.params[0]) val param2 = getParameter(jsonRpcRequest.params[1]) @@ -242,7 +251,7 @@ actual open class ServiceManager actual constructor(val service: T) { res.status(Status.SERVER_ERROR) } }.invoke(this) - }) + } } /** @@ -252,6 +261,7 @@ actual open class ServiceManager actual constructor(val service: T) { * @param method a HTTP method * @param prefix an URL address prefix */ + @Suppress("TooGenericExceptionCaught") protected actual inline fun bind( noinline function: T.(PAR1, PAR2, PAR3, PAR4, PAR5, Request?) -> Deferred, @@ -260,10 +270,11 @@ actual open class ServiceManager actual constructor(val service: T) { prefix: String ) { val routeDef = route ?: "route${this::class.simpleName}${counter++}" - routes.add({ + routes.add { call(method, "$prefix$routeDef") { req, res -> if (service != null) { val jsonRpcRequest = req.body(JsonRpcRequest::class.java) + @Suppress("MagicNumber") if (jsonRpcRequest.params.size == 5) { val param1 = getParameter(jsonRpcRequest.params[0]) val param2 = getParameter(jsonRpcRequest.params[1]) @@ -292,7 +303,7 @@ actual open class ServiceManager actual constructor(val service: T) { res.status(Status.SERVER_ERROR) } }.invoke(this) - }) + } } fun call( @@ -310,6 +321,7 @@ actual open class ServiceManager actual constructor(val service: T) { } } + @Suppress("TooGenericExceptionCaught") protected inline fun getParameter(str: String?): T { return str?.let { if (T::class == String::class) { diff --git a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/types/Date.kt b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/types/Date.kt new file mode 100644 index 00000000..32c8923e --- /dev/null +++ b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/types/Date.kt @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2017-present Robert Jaros + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package pl.treksoft.kvision.types + +import com.github.andrewoma.kwery.mapper.SimpleConverter +import com.github.andrewoma.kwery.mapper.TableConfiguration +import com.github.andrewoma.kwery.mapper.reifiedConverter +import com.github.andrewoma.kwery.mapper.standardConverters +import com.github.andrewoma.kwery.mapper.util.camelToLowerUnderscore +import java.sql.Timestamp +import java.text.SimpleDateFormat + +actual typealias Date = java.util.Date + +actual fun String.toDateF(format: String): Date = SimpleDateFormat(format).parse(this) + +actual fun Date.toStringF(format: String): String = SimpleDateFormat(format).format(this) + +object DateConverter : SimpleConverter( + { row, c -> Date(row.timestamp(c).time) }, + { Timestamp(it.time) } +) + +val kvTableConfig = TableConfiguration( + converters = standardConverters + reifiedConverter(DateConverter), + namingConvention = camelToLowerUnderscore +) diff --git a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/types/KDate.kt b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/types/KDate.kt deleted file mode 100644 index 9fc534c4..00000000 --- a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/types/KDate.kt +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright (c) 2017-present Robert Jaros - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package pl.treksoft.kvision.types - -import com.github.andrewoma.kwery.mapper.SimpleConverter -import com.github.andrewoma.kwery.mapper.TableConfiguration -import com.github.andrewoma.kwery.mapper.reifiedConverter -import com.github.andrewoma.kwery.mapper.standardConverters -import com.github.andrewoma.kwery.mapper.util.camelToLowerUnderscore -import java.sql.Timestamp -import java.text.SimpleDateFormat -import java.util.* - -/** - * A serializable wrapper for a multiplatform Date type. - */ -@Suppress("MayBeConstant") -actual val KDATE_FORMAT = "yyyy-MM-dd HH:mm:ss" - -actual fun nowDate(): KDate = - KDate(Date().time) - -actual fun String.toKDateF(format: String): KDate = - KDate(SimpleDateFormat(format).parse(this).time) - -actual fun KDate.toStringF(format: String) = - SimpleDateFormat(format).format(this.toJava()) - -fun KDate.toJava(): java.util.Date = java.util.Date(this.time) - -object KDateConverter : SimpleConverter( - { row, c -> KDate(row.timestamp(c).time) }, - { Timestamp(it.time) } -) - -val kvTableConfig = TableConfiguration( - converters = standardConverters + reifiedConverter(KDateConverter), - namingConvention = camelToLowerUnderscore -) -- cgit