From 0ebb89cfa86ca758ae66157d69fdfa9840f1faaf Mon Sep 17 00:00:00 2001 From: nikky Date: Thu, 28 Jun 2018 07:21:05 +0200 Subject: [PATCH] add more extensions for Jankson --- core/src/main/kotlin/matterlink/Util.kt | 5 ++++ .../kotlin/matterlink/config/BaseConfig.kt | 23 ++++++++----------- .../kotlin/matterlink/config/CommandConfig.kt | 6 +++-- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/core/src/main/kotlin/matterlink/Util.kt b/core/src/main/kotlin/matterlink/Util.kt index 873adee..97c6928 100644 --- a/core/src/main/kotlin/matterlink/Util.kt +++ b/core/src/main/kotlin/matterlink/Util.kt @@ -1,5 +1,6 @@ package matterlink +import blue.endless.jankson.Jankson import blue.endless.jankson.JsonObject import matterlink.config.cfg import java.io.PrintWriter @@ -60,3 +61,7 @@ fun JsonObject.getOrDefault(key: String, default: T, comment: String? // instance.info("type: ${default.javaClass.name} key: $key json: >>>${this.getObject(key)?.toJson()}<<< default: $default") return putDefault(key, default, comment)!! } + +inline fun Jankson.Builder.registerTypeAdapter(noinline adapter: (JsonObject) -> T) = this.registerTypeAdapter(T::class.java, adapter) + +inline fun Jankson.Builder.registerPrimitiveTypeAdapter(noinline adapter: (Any) -> T) = this.registerPrimitiveTypeAdapter(T::class.java, adapter) diff --git a/core/src/main/kotlin/matterlink/config/BaseConfig.kt b/core/src/main/kotlin/matterlink/config/BaseConfig.kt index ab5b945..77a1e07 100644 --- a/core/src/main/kotlin/matterlink/config/BaseConfig.kt +++ b/core/src/main/kotlin/matterlink/config/BaseConfig.kt @@ -7,6 +7,7 @@ import blue.endless.jankson.impl.SyntaxError import matterlink.bridge.MessageHandlerInst import matterlink.getOrDefault import matterlink.instance +import matterlink.registerTypeAdapter import matterlink.stackTraceString import java.io.File import java.io.FileNotFoundException @@ -119,7 +120,7 @@ data class BaseConfig(val rootDir: File) { companion object { val jankson = Jankson .builder() - .registerTypeAdapter(MatterLinkConfig::class.java) { + .registerTypeAdapter { MatterLinkConfig( command = it.getOrDefault( "command", @@ -161,7 +162,7 @@ data class BaseConfig(val rootDir: File) { ) ) } - .registerTypeAdapter(CommandOptions::class.java) { + .registerTypeAdapter { with(CommandOptions()) { CommandOptions( enable = it.getOrDefault( @@ -182,7 +183,7 @@ data class BaseConfig(val rootDir: File) { ) } } - .registerTypeAdapter(ConnectOptions::class.java) { + .registerTypeAdapter { with(ConnectOptions()) { ConnectOptions( url = it.getOrDefault( @@ -208,14 +209,14 @@ data class BaseConfig(val rootDir: File) { ) } } - .registerTypeAdapter(DebugOptions::class.java) { + .registerTypeAdapter { with(DebugOptions()) { DebugOptions( logLevel = it.getOrDefault("loglevel", logLevel, "MatterLink log level") ) } } - .registerTypeAdapter(IncomingOptions::class.java) { + .registerTypeAdapter { with(IncomingOptions()) { IncomingOptions( chat = it.getOrDefault( @@ -241,7 +242,7 @@ data class BaseConfig(val rootDir: File) { ) } } - .registerTypeAdapter(OutgoingOptions::class.java) { + .registerTypeAdapter { with(OutgoingOptions()) { OutgoingOptions( systemUser = it.getOrDefault( @@ -282,7 +283,7 @@ data class BaseConfig(val rootDir: File) { ) } } - .registerTypeAdapter(DeathOptions::class.java) { jsonObj -> + .registerTypeAdapter { jsonObj -> with(DeathOptions()) { DeathOptions( enable = jsonObj.getOrDefault( @@ -312,9 +313,7 @@ data class BaseConfig(val rootDir: File) { ) } } - .registerTypeAdapter(JoinPartOptions:: - class.java) - { + .registerTypeAdapter { with(JoinPartOptions()) { JoinPartOptions( enable = it.getOrDefault( @@ -337,9 +336,7 @@ data class BaseConfig(val rootDir: File) { ) } } - .registerTypeAdapter(UpdateOptions:: - class.java) - { + .registerTypeAdapter { with(UpdateOptions()) { UpdateOptions( enable = it.getOrDefault( diff --git a/core/src/main/kotlin/matterlink/config/CommandConfig.kt b/core/src/main/kotlin/matterlink/config/CommandConfig.kt index cb3734a..a7993c4 100644 --- a/core/src/main/kotlin/matterlink/config/CommandConfig.kt +++ b/core/src/main/kotlin/matterlink/config/CommandConfig.kt @@ -8,6 +8,8 @@ import matterlink.bridge.command.CommandType import matterlink.bridge.command.CustomCommand import matterlink.getOrDefault import matterlink.instance +import matterlink.registerPrimitiveTypeAdapter +import matterlink.registerTypeAdapter import java.io.File import java.io.FileNotFoundException @@ -74,7 +76,7 @@ object CommandConfig { fun readConfig(): Boolean { val jankson = Jankson .builder() - .registerTypeAdapter(CustomCommand::class.java) { jsonObj -> + .registerTypeAdapter { jsonObj -> with(CustomCommand.DEFAULT) { CustomCommand( type = jsonObj.get(CommandType::class.java, "type") ?: type, @@ -89,7 +91,7 @@ object CommandConfig { ) } } - .registerPrimitiveTypeAdapter(Regex::class.java) { + .registerPrimitiveTypeAdapter { it.toString().toRegex() } .build()